MySQL基礎(chǔ)1——命令行操作MySQL

1. 連接MySQL數(shù)據(jù)庫
  1. 打開命令行終端程序,windows CMD(首先要在系統(tǒng)環(huán)境變量中配置好MySQL的路徑,具體方法請(qǐng)自行百度)或者使用cmder軟件,或者在Navicat for MySQL中F6打開終端
  1. 輸入命令 mysql -uroot -p 輸入mysql的密碼回車進(jìn)入數(shù)據(jù)庫
    或者輸入mysql -uroot -pxxx or mysql -uroot -pxxx -A 直接進(jìn)入數(shù)據(jù)庫。
    root表示你進(jìn)入數(shù)據(jù)庫使用的用戶名,xxx表示對(duì)應(yīng)的密碼,-A表示切換數(shù)據(jù)庫的時(shí)候不會(huì)預(yù)讀取數(shù)據(jù)庫信息,這樣能夠更快地切換數(shù)據(jù)庫。
2.查看mysql里面都有哪些數(shù)據(jù)庫

show databases;//顯示MySQL/data/目錄下所有文件夾

3. 創(chuàng)建數(shù)據(jù)庫test

create database <tablename> charset utf8;//注意創(chuàng)建的數(shù)據(jù)庫的名稱只能以字母或下劃線開頭(推薦使用小寫字母,windows下不區(qū)分大小寫,即使以大寫字母命名,創(chuàng)建之后還是小寫) Linux中區(qū)分大小寫

4. 刪除數(shù)據(jù)庫

drop database test;//注意,刪除數(shù)據(jù)庫會(huì)把里面的表也一起刪除

5. 切換數(shù)據(jù)庫

use test;//切換到test數(shù)據(jù)庫

6. 查看本數(shù)據(jù)庫下所有表

show tables;

7. 查看tag表中的所有數(shù)據(jù)

select * from tag;

8. 查詢表結(jié)構(gòu)

desc tag;//查詢tag表的表結(jié)構(gòu)

查看系統(tǒng)字符集設(shè)置變量

mysql> show variables like "%character%";  # 查看系統(tǒng)字符集設(shè)置變量
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

9.創(chuàng)建一張表,表名為stu,包含主鍵,學(xué)生姓名,性別,年齡,班級(jí)id

create table stu(id int primary key auto_increment,name char(10) not null default '',sex enum('男','女') not null default '男',cid tinyint unsigned not null default 1); 這里的name char(10)表示name這個(gè)字段最多有10個(gè)字符,不區(qū)分漢字字母數(shù)字。

注釋:建表的注意事項(xiàng)。

  1. 最好每一張表都設(shè)主鍵,根據(jù)表可能要存儲(chǔ)的數(shù)據(jù)量來選擇盡可能小的數(shù)據(jù)類型。主鍵自增。其他字段能用數(shù)值類型的不要用字符串類型。int后如果不限定長(zhǎng)度,不限定非負(fù),默認(rèn)為int(11);tinyint默認(rèn)為tinyint(3)
  2. 性別字段用enum類型,enum后面的括號(hào)內(nèi)是字符串,要加引號(hào),但是MySQL處理enum是按數(shù)值類型儲(chǔ)存的,所以比直接用字符串要快。
  3. 盡量不要有NULL值,text類型(文字內(nèi)容字段的類型比如 BLOB, TEXT, GEOMETRY or JSON)不能有默認(rèn)值

查看表生成的DDL:(數(shù)據(jù)定義語言(Data Definition Language))
show create table stu;

10. 往stu表中添加一條數(shù)據(jù)

insert into stu set name = '小明';
顯示如下:

mysql> insert into stu set name = '小明';
Query OK, 1 row affected (0.11 sec)
mysql> select * from stu;
+----+------+-----+-----+
| id | name | sex | cid |
+----+------+-----+-----+
|  1 | 小明 | 男  |   1 |
+----+------+-----+-----+
1 row in set (0.00 sec)

因?yàn)橹麈Iid自增,sex和cid都有默認(rèn)值,所以只填寫name值,其它項(xiàng)也不會(huì)有null值
一次填寫多個(gè)字段,中間用,分割,如

mysql> insert into stu set name = '小紅',sex = '女',cid = 2;
Query OK, 1 row affected (0.12 sec)
mysql> select * from stu;
+----+------+-----+-----+
| id | name | sex | cid |
+----+------+-----+-----+
|  1 | 小明 | 男  |   1 |
|  2 | 小紅 | 女  |   2 |
+----+------+-----+-----+
2 rows in set (0.00 sec)

一次添加多條多個(gè)字段可以使用這樣的語句
insert into stu (name,sex,cid) value ('小剛','男',1),('Lucy','女',3),('李雷','男',1),('Lily','女',2); //句中的value也可以用values

11. 復(fù)制表操作
  1. 復(fù)制表結(jié)構(gòu)
    create table stu1 like stu;//按stu表的結(jié)構(gòu)創(chuàng)建一個(gè)stu1表,
    看一下效果:
mysql> desc stu1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11)          | NO   | PRI | NULL    | auto_increment |
| name  | char(10)         | NO   |     |         |                |
| sex   | enum('男','女')  | NO   |     | 男      |                |
| cid   | int(10) unsigned | NO   |     | 1       |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
mysql> desc stu;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11)          | NO   | PRI | NULL    | auto_increment |
| name  | char(10)         | NO   |     |         |                |
| sex   | enum('男','女')  | NO   |     | 男      |                |
| cid   | int(10) unsigned | NO   |     | 1       |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

兩張表具有相同的表結(jié)構(gòu)

  1. 復(fù)制表數(shù)據(jù)
    insert into stu1 select * from stu;//把表stu中的數(shù)據(jù)全部添加到stu1
  2. 同時(shí)復(fù)制表結(jié)構(gòu)和表數(shù)據(jù)
    create table stu2 select * from stu; # 這種方法會(huì)丟失字段的主鍵屬性和主鍵自增屬性。還需要手動(dòng)修改添加這些屬性(如果原表有的話)
    alter table stu2 add primary key(id)
    alter table stu2 modify id int unsigned auto_increment;

復(fù)制表結(jié)構(gòu)還有2種方法,比如
create table c1 select*from stu limit 0 # 只復(fù)制結(jié)構(gòu),不用數(shù)據(jù)
create table c2 select*from stu where 1=2 # where 之后跟一個(gè)false,或者bool為false的運(yùn)算式,也不會(huì)復(fù)制表數(shù)據(jù)。
同樣這兩種方法會(huì)丟失表的主鍵和自增屬性(不推薦)
推薦方式是分2步,先復(fù)制表結(jié)構(gòu),再復(fù)制表數(shù)據(jù)?;蛘邆浞荼恚賹?dǎo)入。

12. 刪除表中的數(shù)據(jù)
  1. 刪除1條數(shù)據(jù)
mysql> select * from stu2;
+----+------+-----+-----+
| id | name | sex | cid |
+----+------+-----+-----+
|  1 | 小明 | 男  |   1 |
|  2 | 小紅 | 女  |   2 |
| 15 | 小剛 | 男  |   1 |
| 16 | Lucy | 女  |   3 |
| 17 | 李雷 | 男  |   1 |
| 18 | Lily | 女  |   2 |
+----+------+-----+-----+
6 rows in set (0.00 sec)
mysql> delete from stu2 where id = 15;//刪除id為15的這條數(shù)據(jù)
Query OK, 1 row affected (0.13 sec)
###### 8. 有如下表,寫出顯示各班及格和不及格人數(shù)的sql
mysql> select * from stu2;
+----+------+-----+-----+
| id | name | sex | cid |
+----+------+-----+-----+
|  1 | 小明 | 男  |   1 |
|  2 | 小紅 | 女  |   2 |
| 16 | Lucy | 女  |   3 |
| 17 | 李雷 | 男  |   1 |
| 18 | Lily | 女  |   2 |
+----+------+-----+-----+
  1. 一次刪除多條數(shù)據(jù)
mysql> delete from stu2 where id in (1,2,18);
Query OK, 3 rows affected (0.14 sec)
mysql> select * from stu2;
+----+------+-----+-----+
| id | name | sex | cid |
+----+------+-----+-----+
| 16 | Lucy | 女  |   3 |
| 17 | 李雷 | 男  |   1 |
+----+------+-----+-----+
2 rows in set (0.00 sec)
  1. 如果不加where限定,會(huì)刪除表中所有數(shù)據(jù)
mysql> delete from stu2;
Query OK, 2 rows affected (0.12 sec)
mysql> select * from stu2;
Empty set (0.00 sec)
  1. 清空表數(shù)據(jù)用truncate+表名,如:
mysql> select * from stu1;
+----+------+-----+-----+
| id | name | sex | cid |
+----+------+-----+-----+
|  1 | 小明 | 男  |   1 |
|  2 | 小紅 | 女  |   2 |
| 15 | 小剛 | 男  |   1 |
| 16 | Lucy | 女  |   3 |
| 17 | 李雷 | 男  |   1 |
| 18 | Lily | 女  |   2 |
+----+------+-----+-----+
6 rows in set (0.00 sec)
mysql> truncate stu1;//清空表stu1的所有數(shù)據(jù)
Query OK, 0 rows affected (0.08 sec)
mysql> select * from stu1;
Empty set (0.00 sec)
  1. truncatedelete from+表名清空表的區(qū)別
    delete from+表名清空表數(shù)據(jù)只是把表中的數(shù)據(jù)全部刪除,還會(huì)保留主鍵自增的位置,比如當(dāng)前表中最后一條數(shù)據(jù)的id為20,清空表之后,再添加1條數(shù)據(jù),那么新增的那條數(shù)據(jù)的id為21.而如果用truncate清空表,新增數(shù)據(jù)的id會(huì)從1開始。
13.修改表操作
  1. 修改表名 alter table 舊表名 rename 新表名;
mysql> show tables;
+---------------+
| Tables_in_stu |
+---------------+
| stu           |
| stu1          |
| stu2          |
+---------------+
3 rows in set (0.00 sec)
mysql> alter table stu rename student;
Query OK, 0 rows affected (0.09 sec)
mysql> show tables;
+---------------+
| Tables_in_stu |
+---------------+
| stu1          |
| stu2          |
| student       |
+---------------+
3 rows in set (0.00 sec)
  1. 修改字段名,用change
    alter table student change name mingzi varchar(20) not null default '';
    看例子:
mysql> select * from student;
+----+------+-----+-----+
| id | name | sex | cid |
+----+------+-----+-----+
|  1 | 小明 | 男  |   1 |
|  2 | 小紅 | 女  |   2 |
| 15 | 小剛 | 男  |   1 |
| 16 | Lucy | 女  |   3 |
| 17 | 李雷 | 男  |   1 |
| 18 | Lily | 女  |   2 |
+----+------+-----+-----+
6 rows in set (0.00 sec)
mysql> alter table student change name mingzi varchar(20) not null default '';//把name字段改為mingzi,類型從char(10)改為varchar(20),其他不變。注意,改了字段后面一定別忘了加上對(duì)應(yīng)的字段值類型
Query OK, 6 rows affected (0.30 sec)
Records: 6  Duplicates: 0  Warnings: 0
mysql> select * from student;
+----+--------+-----+-----+
| id | mingzi | sex | cid |
+----+--------+-----+-----+
|  1 | 小明   | 男  |   1 |
|  2 | 小紅   | 女  |   2 |
| 15 | 小剛   | 男  |   1 |
| 16 | Lucy   | 女  |   3 |
| 17 | 李雷   | 男  |   1 |
| 18 | Lily   | 女  |   2 |
+----+--------+-----+-----+
6 rows in set (0.00 sec)
mysql> desc student;
+--------+------------------+------+-----+---------+----------------+
| Field  | Type             | Null | Key | Default | Extra          |
+--------+------------------+------+-----+---------+----------------+
| id     | int(11)          | NO   | PRI | NULL    | auto_increment |
| mingzi | varchar(20)      | NO   |     |         |                |
| sex    | enum('男','女')  | NO   |     | 男      |                |
| cid    | int(10) unsigned | NO   |     | 1       |                |
+--------+------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
  1. 修改字段,不改字段名,用modify
    alter table student modify mingzi char(12);//把mingzi字段的類型改為char(12),后面沒有寫not null default '',看看表結(jié)構(gòu)的變化:
mysql> desc student;
+--------+------------------+------+-----+---------+----------------+
| Field  | Type             | Null | Key | Default | Extra          |
+--------+------------------+------+-----+---------+----------------+
| id     | int(11)          | NO   | PRI | NULL    | auto_increment |
| mingzi | char(12)         | YES  |     | NULL    |                |
| sex    | enum('男','女')  | NO   |     | 男      |                |
| cid    | int(10) unsigned | NO   |     | 1       |                |
+--------+------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

默認(rèn)值變成null了。

  1. 添加一個(gè)字段addtime
    alter table student add addtime int(10) not null default 0;
    看一下變化:
mysql> desc student;
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| id      | int(11)          | NO   | PRI | NULL    | auto_increment |
| mingzi  | char(12)         | YES  |     | NULL    |                |
| sex     | enum('男','女')  | NO   |     | 男      |                |
| cid     | int(10) unsigned | NO   |     | 1       |                |
| addtime | int(10)          | NO   |     | 0       |                |
+---------+------------------+------+-----+---------+----------------+
5 rows in set (0.02 sec)

改一下cid字段

mysql> alter table student modify cid tinyint unsigned not null default 1;
Query OK, 6 rows affected (0.28 sec)
Records: 6  Duplicates: 0  Warnings: 0
mysql> desc student;
+---------+---------------------+------+-----+---------+----------------+
| Field   | Type                | Null | Key | Default | Extra          |
+---------+---------------------+------+-----+---------+----------------+
| id      | int(11)             | NO   | PRI | NULL    | auto_increment |
| mingzi  | char(12)            | YES  |     | NULL    |                |
| sex     | enum('男','女')     | NO   |     | 男      |                |
| cid     | tinyint(3) unsigned | NO   |     | 1       |                |
| addtime | int(10)             | NO   |     | 0       |                |
+---------+---------------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

由此可見tinyint默認(rèn)會(huì)修改為tinyint(3)

  1. 刪除一個(gè)字段:alter table 表名 drop 要?jiǎng)h除的字段名;比如:
    alter table student drop addtime;
  2. 刪除自增
    alter table student modify id int not null;//即修改主鍵字段的修飾語,去掉auto_increment,覆蓋掉原來的
    看效果:
mysql> alter table student modify id int not null;
Query OK, 6 rows affected (0.29 sec)
Records: 6  Duplicates: 0  Warnings: 0
mysql> desc student;
+--------+---------------------+------+-----+---------+-------+
| Field  | Type                | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| id     | int(11)             | NO   | PRI | NULL    |       |
| mingzi | char(12)            | YES  |     | NULL    |       |
| sex    | enum('男','女')     | NO   |     | 男      |       |
| cid    | tinyint(3) unsigned | NO   |     | 1       |       |
+--------+---------------------+------+-----+---------+-------+
4 rows in set (0.06 sec)
  1. 刪除主鍵,alter table 表名 drop primary key;
    注意:刪除主鍵之前必須先要?jiǎng)h除主鍵的自增,否則會(huì)報(bào)錯(cuò)。
mysql> alter table student drop primary key;
Query OK, 6 rows affected (0.42 sec)
Records: 6  Duplicates: 0  Warnings: 0
mysql> desc student;
+--------+---------------------+------+-----+---------+-------+
| Field  | Type                | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| id     | int(11)             | NO   |     | NULL    |       |
| mingzi | char(12)            | YES  |     | NULL    |       |
| sex    | enum('男','女')     | NO   |     | 男      |       |
| cid    | tinyint(3) unsigned | NO   |     | 1       |       |
+--------+---------------------+------+-----+---------+-------+
4 rows in set (0.04 sec)
14. 有如下表,寫出顯示各班及格和不及格人數(shù)的sql
+----+------+-----+-----+-------+
| id | name | sex | cid | score |
+----+------+-----+-----+-------+
|  1 | 小明 | 男  |   1 |   100 |
|  2 | 小紅 | 女  |   2 |    30 |
|  3 | 小剛 | 男  |   1 |    66 |
|  4 | Lucy | 女  |   3 |    90 |
|  5 | 李雷 | 男  |   1 |    30 |
|  6 | Lily | 女  |   2 |    77 |
+----+------+-----+-----+-------+
mysql> select cid,sum(if(score>=60,1,0)) 及格,sum(if(score<60,1,0)) 不及格 from student group by cid;
+-----+------+--------+
| cid | 及格 | 不及格 |
+-----+------+--------+
|   1 |    2 |      1 |
|   2 |    1 |      1 |
|   3 |    1 |      0 |
+-----+------+--------+
3 rows in set (0.00 sec)

如果這篇文章對(duì)你有幫助,不妨點(diǎn)個(gè)贊哦 (˙?˙)?--?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,628評(píng)論 18 399
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 3,995評(píng)論 0 11
  • 1.1、常用數(shù)據(jù)庫包括:Oracle、MySQL、SQLServer、DB2、SyBase等 1.2、Navica...
    NOX_5d2b閱讀 3,555評(píng)論 0 0
  • Nana 第一感覺女祭司很冷靜,無窮的志慧,直覺力觀察力強(qiáng)大,抽到這個(gè)想起了小時(shí)候的自己特別像女祭司,喜歡觀察周遭...
    leenaii閱讀 593評(píng)論 0 1
  • 每日沉醉于不想上班的念頭里日漸消瘦,我有一百個(gè)不想上班的理由... 一大早的洗了一個(gè)頭發(fā),很是開心,畢竟好多天沒有...
    我叫Amour1921閱讀 943評(píng)論 2 11

友情鏈接更多精彩內(nèi)容