一 MySQL 安裝及配置
1.下載
首先上MySql的官網下載 https://dev.mysql.com/downloads/mysql/ ,本人下載的是 mysql-5.7.24-winx64.zip版。
2.解壓存放目錄
下載完解壓到你想要存放的位置 我的是解壓到D:\Program Files\MySQL 。
3.配置環境變量
在環境變量path中追加一句:;D:\Program Files\MySQL\bin 切記前面有其他環境變量時一定加 ;
4.自己配置mysql的配置文件my.ini
在D:\Program Files\MySQL\ 目錄下新建my.ini,打開加入如下代碼:
[mysql] # 設置mysql客戶端默認字符集 default-character-set=utf8 [mysqld] #設置3306端口 port = 3306 # 設置mysql的安裝目錄 basedir=D:\Program Files\MySQL # 設置mysql數據庫的數據的存放目錄 datadir=D:\Program Files\MySQL\data # 允許最大連接數 max_connections=200 # 服務端使用的字符集默認為8比特編碼的latin1字符集 character-set-server=utf8 # 創建新表時將使用的默認存儲引擎 default-storage-engine=INNODB
5.在cmd中配置
以管理員身份打開cmd命令窗口,進入D:\Program Files\MySQL\bin>目錄下,運行mysqld --initialize --user=mysql --console ,見下圖所示:
6.驗證安裝是否成功
完成上面兩個文件的創建后,還是在D:\mysql-5.7.20-winx64\bin下用管理員運行cmd 輸入命令 mysqld -install 如果出現Service successfully installed 說明注冊成功了
7.啟動和關閉mysql服務
啟動服務:net start mysql
關閉服務:net stop mysql
到此mysql數據庫安裝和配置完畢。
二 MySQL的基礎知識
1.創建、刪除及顯示數據庫
C:\Users\Dell>mysql -u root -p Enter password: ******* Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.7.24 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database db; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> drop database db; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
注意:一次只能刪除一個數據庫,不能同時刪除多個庫
2.創建數據庫db1,並設置數據庫字符編碼為utf8
mysql> create database db1 character set utf8; Query OK, 1 row affected (0.03 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db | | db1 | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec)
3.創建數據庫db2,設置編碼utf8和校對規則utf8_general_ci (校對規則:可以理解成排序規則;默認使用utf8_general_ci )
mysql> create database db2 character set utf8 collate utf8_general_ci; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db | | db1 | | db2 | | mysql | | performance_schema | | sys | +--------------------+ 7 rows in set (0.00 sec)
4.顯示數據庫創建語句
mysql> show create database db; +----------+-------------------------------------------------------------+ | Database | Create Database | +----------+-------------------------------------------------------------+ | db | CREATE DATABASE `db` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+-------------------------------------------------------------+ 1 row in set (0.00 sec)
5.mysql數據類型
需要重點掌握的有以下幾種:
數值型: int(整型,不指定代表有符號) float(存儲小數) decimal(存儲小數,精度高)
字符型: char(存儲字符,范圍 0~255字符) varchar(存儲字符,范圍 0~65532 字節) text
日期型:data (存儲日期類型)
字符數據類型解釋說明如下圖
6.創建數據表
基本語法
create table 表名 ( 字段1 數據類型, 字段2 數據類型, 字段n 數據類型, )character set 字符編碼 collate 校對規則 engine 存儲引擎方式 field:指定列名 datatype: 指定列類型
注意:表后面的各項設置以創建表時設置的為准,如表沒有設置,以數據庫為准。
創建數據表user並顯示其歸屬哪個數據庫(顯示歸屬的數據庫命令:show tables; )
mysql> use db; Database changed mysql> create table user(id int,salary float(4,2),bonus decimal(4,2)); Query OK, 0 rows affected (0.05 sec) mysql> select * from user; Empty set (0.01 sec) mysql> show tables; +--------------+ | Tables_in_db | +--------------+ | user | +--------------+ 1 row in set (0.00 sec)
注:float(4,2) 表示存儲范圍在 -99.99~99.99 ; decimal(6,2)表示存儲范圍在 -9999.99~9999.99 ;即 float (n,m)表示共有 n 位數字,m表示保留 m 位小數。精度測試數據為:(10,2)
7.創建案例:雇員表 employee
mysql> create table employee( -> id int unsigned, -> name varchar(100) not null default '', -> sex char(1) not null default '', -> brithday date , -> job varchar(30) not null default '', -> salary decimal(10,2) not null default 0, -> introduction text)character set utf8 engine MyISAM; Query OK, 0 rows affected (0.03 sec) mysql> show tables; +--------------+ | Tables_in_db | +--------------+ | employee | | user | +--------------+ 2 rows in set (0.00 sec)
表employee說明如下:
8.查看表結構的詳細信息
mysql> desc employee; +--------------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+------------------+------+-----+---------+-------+ | id | int(10) unsigned | YES | | NULL | | | name | varchar(100) | NO | | | | | sex | char(1) | NO | | | | | brithday | date | YES | | NULL | | | job | varchar(30) | NO | | | | | salary | decimal(10,2) | NO | | 0.00 | | | introduction | text | YES | | NULL | | +--------------+------------------+------+-----+---------+-------+ 7 rows in set (0.00 sec)
9.數據表CRUD操作
- insert語句(插入數據)
insert into 表名(字段1,字段2,...字段n) values (字段 值1,字段值2,...字段值n);
示例代碼
mysql> insert into employee(id,name,sex,brithday,job,salary,introduction) -> values(001,'張國鋒','男','1981-12-28','software engineer',12000,'工作態度端正,認真負責本職工作,技術能力強,是不可多得的人才!'); Query OK, 1 row affected (0.00 sec) mysql> select * from employee; +------+-----------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | id | name | sex | brithday | job | salary | introduction | +------+-----------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | 1 | 張國鋒 | 男 | 1981-12-28 | software engineer | 12000.00 | 工作態度端正,認真負責本職工作,技術能力強,是不可多得的人才! | +------+-----------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
注:在插入數據時,sql代碼工具一定要與表設置的字符編碼相同,否則插入的數據會出現亂碼;也可在mysql命令提示符下輸入:mysql> set names utf8 或 gbk;
- update語句(更新數據)
update 表名 set 字段1=新值,字段2=新值 where 條件;
示例代碼
mysql> select * from employee; +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | id | name | sex | brithday | job | salary | introduction | +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | 1 | 張國鋒 | 男 | 1981-12-28 | software engineer | 12000.00 | 工作態度端正,認真負責本職工作,技術能力強,是不可多得的人才! | | 2 | 李建林 | 男 | 1972-10-21 | teacher | 5200.00 | 工作認真,講課生動有趣,在學校評為優秀講師 | +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> update employee set salary=6000 where id=2; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from employee; +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | id | name | sex | brithday | job | salary | introduction | +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | 1 | 張國鋒 | 男 | 1981-12-28 | software engineer | 12000.00 | 工作態度端正,認真負責本職工作,技術能力強,是不可多得的人才! | | 2 | 李建林 | 男 | 1972-10-21 | teacher | 6000.00 | 工作認真,講課生動有趣,在學校評為優秀講師 | +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> update employee set salary=salary+1000 where id=2; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from employee; +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | id | name | sex | brithday | job | salary | introduction | +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | 1 | 張國鋒 | 男 | 1981-12-28 | software engineer | 12000.00 | 工作態度端正,認真負責本職工作,技術能力強,是不可多得的人才! | | 2 | 李建林 | 男 | 1972-10-21 | teacher | 7000.00 | 工作認真,講課生動有趣,在學校評為優秀講師 | +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
注:如果不加wherer條件,所有數據的指定字段值全部修改!
- delete語句(刪除數據)
delete from 表名 where 條件;
示例代碼
mysql> select * from employee; +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | id | name | sex | brithday | job | salary | introduction | +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | 1 | 張國鋒 | 男 | 1981-12-28 | software engineer | 12000.00 | 工作態度端正,認真負責本職工作,技術能力強,是不可多得的人才! | | 2 | 李建林 | 男 | 1972-10-21 | teacher | 7000.00 | 工作認真,講課生動有趣,在學校評為優秀講師 | +------+------------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> delete from employee where id=2; Query OK, 1 row affected (0.00 sec) mysql> select * from employee; +------+-----------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | id | name | sex | brithday | job | salary | introduction | +------+-----------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ | 1 | 張國鋒 | 男 | 1981-12-28 | software engineer | 12000.00 | 工作態度端正,認真負責本職工作,技術能力強,是不可多得的人才! | +------+-----------+-----+------------+-------------------+----------+-----------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
- select語句(查詢數據)
#按指定字段查詢 select 字段1,字段2,... 字段n from 表名 where 條件; #對表進行所有字段的查詢 select * from 表名 where 條件;
注:在mysql中,表名和字段名不區分大小寫
- order by語句(對查詢結果進行排序)
select 字段1,字段2,... 字段n from 表名 where 條件 order by 字段 [asc|desc];
代碼示例