MySQL的DDL、DML、DQL語句和單表增、刪、改、查 [toc] 實驗準備: 1. 初始化設置過的centos虛擬機 2. 安裝MySQL資料庫 實驗開始: DDL語句 1. 創建和刪除資料庫 2. 查詢資料庫字元集 MariaDB [(none)] show create database ...
目錄
MySQL的DDL、DML、DQL語句和單表增、刪、改、查
實驗準備:
- 初始化設置過的centos虛擬機
- 安裝MySQL資料庫
實驗開始:
DDL語句
表:二維關係 設計表:遵循規範
定義:欄位,索引
欄位:欄位名,欄位數據類型,修飾符
約束,索引:應該創建在經常用作查詢條件的欄位上
- 創建和刪除資料庫
MariaDB [(none)]> create database testdb1; (分號為結束符)
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases; (列出所有資料庫)
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| testdb1 |
+--------------------+
4 rows in set (0.00 sec)
[root@centos7 ~]#tree /data/mysql/testdb1/ (其實就是在MySQL下麵建立了一個目錄)
/data/mysql/testdb1/
└── db.opt
[root@centos7 ~]#cat /data/mysql/testdb1/db.opt (也可以查看裡面的內容,字元集也可以看到)
default-character-set=latin1
default-collation=latin1_swedish_ci
0 directories, 1 file
MariaDB [testdb1]> drop database testdb1; (刪除資料庫,同時MySQL下麵的testdb1目錄也會刪除)
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
- 查詢資料庫字元集
MariaDB [(none)]> show create database testdb1; (查看testdb1的資料庫字元集)
+----------+--------------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------------+
| testdb1 | CREATE DATABASE `testdb1` /*!40100 DEFAULT CHARACTER SET latin1 */ | (這裡最後的為字元集既latin1這個字元集不支持漢字的。)
+----------+--------------------------------------------------------------------+
1 row in set (0.00 sec)
(這樣也可以查看比較好辨認)
MariaDB [(none)]> show create database testdb1\G;
*************************** 1. row ***************************
Database: testdb1
Create Database: CREATE DATABASE `testdb1` /*!40100 DEFAULT CHARACTER SET latin1 */
1 row in set (0.00 sec)
MariaDB [(none)]> show create database mysql; (查看MySQL的資料庫字元集)
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| mysql | CREATE DATABASE `mysql` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)
- 修改字元集
MariaDB [(none)]> alter database testdb1 CHARACTER SET utf8mb4; (這個字元集支持全球文字還支持表情包)
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show create database testdb1;
+----------+---------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------+
| testdb1 | CREATE DATABASE `testdb1` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
+----------+---------------------------------------------------------------------+
1 row in set (0.00 sec)
[root@centos7 ~]#cat /data/mysql/testdb1/db.opt (文件也會修改)
default-character-set=utf8mb4
default-collation=utf8mb4_general_ci
- 創建一個表做實驗
MariaDB [(none)]> use testdb1;
Database changed
MariaDB [testdb1]> create table test ( id int unsigned auto_increment primary key,name varchar(10) not null,mobile char(11) not null );
Query OK, 0 rows affected (0.01 sec)
MariaDB [testdb1]> desc test; (查看表結構)
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| mobile | char(11) | NO | | NULL | |
+--------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
MariaDB [hellodb]> show tables; (列出當前庫里的所有表)
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.01 sec)
(查看表內容,先要進入存在這個表的資料庫) 這個表是我從外部導入進來的沒有手工創建,需要的話下次我貼出來。
MariaDB [(none)]> use hellodb
Database changed
MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
(創建表獲取幫助命令)
MariaDB [hellodb]> help create table;
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
Or:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
[partition_options]
select_statement
Or:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }
- 克隆表
MariaDB [hellodb]> create table newstudents select * from students; (通過查詢現存表創建;新表會被直接插入查詢而來的數據)
Query OK, 25 rows affected (0.01 sec)
Records: 25 Duplicates: 0 Warnings: 0
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| newstudents |
| scores |
| students |
| teachers |
| toc |
+-------------------+
8 rows in set (0.00 sec)
MariaDB [hellodb]> select * from newstudents;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
(註意:這樣不會把主鍵之類的複製過來,只是複製數據,表結構不一樣)如下:
MariaDB [hellodb]> desc newstudents;
+-----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| StuID | int(10) unsigned | NO | | 0 | |
| Name | varchar(50) | NO | | NULL | |
| Age | tinyint(3) unsigned | NO | | NULL | |
| Gender | enum('F','M') | NO | | NULL | |
| ClassID | tinyint(3) unsigned | YES | | NULL | |
| TeacherID | int(10) unsigned | YES | | NULL | |
+-----------+---------------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
MariaDB [hellodb]> desc students;
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| StuID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Name | varchar(50) | NO | | NULL | |
| Age | tinyint(3) unsigned | NO | | NULL | |
| Gender | enum('F','M') | NO | | NULL | |
| ClassID | tinyint(3) unsigned | YES | | NULL | |
| TeacherID | int(10) unsigned | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
通過複製現存的表的表結構創建,但不複製數據
MariaDB [hellodb]> create table newstudents2 like students;
Query OK, 0 rows affected (0.01 sec)
MariaDB [hellodb]> select * from newstudents2; (這裡查看數據是沒有的)
Empty set (0.00 sec)
MariaDB [hellodb]> desc newstudents2; (但是有表結構)
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| StuID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Name | varchar(50) | NO | | NULL | |
| Age | tinyint(3) unsigned | NO | | NULL | |
| Gender | enum('F','M') | NO | | NULL | |
| ClassID | tinyint(3) unsigned | YES | | NULL | |
| TeacherID | int(10) unsigned | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
- 表查看
查看支持的engine類型:
MariaDB [hellodb]> show engines;
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| CSV | YES | Stores tables as CSV files | NO | NO | NO |
| MRG_MyISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | Non-transactional engine with good performance and small data footprint | NO | NO | NO |
| SEQUENCE | YES | Generated tables filled with sequential values | YES | NO | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| Aria | YES | Crash-safe tables with MyISAM heritage | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, foreign keys and encryption for tables | YES | YES | YES |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
8 rows in set (0.00 sec)
查看表:
MariaDB [hellodb]> show tables ; (列出當前資料庫中的所有表)
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| newstudents |
| newstudents2 |
| scores |
| students |
| teachers |
| toc |
+-------------------+
9 rows in set (0.00 sec)
MariaDB [hellodb]> show tables from mysql; (指明資料庫查看這個資料庫的表)
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| column_stats |
| columns_priv |
| db |
| event |
| func |
| general_log |
| gtid_slave_pos |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| index_stats |
| innodb_index_stats |
| innodb_table_stats |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| roles_mapping |
| servers |
| slow_log |
| table_stats |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
30 rows in set (0.00 sec)
查看表結構:
MariaDB [hellodb]> desc students;
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| StuID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Name | varchar(50) | NO | | NULL | |
| Age | tinyint(3) unsigned | NO | | NULL | |
| Gender | enum('F','M') | NO | | NULL | |
| ClassID | tinyint(3) unsigned | YES | | NULL | |
| TeacherID | int(10) unsigned | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
表結構欄位的意思。
1. Field:欄位表示的是別名
2. Type:欄位表示的是列的數據類型
3. Null :欄位表示這個列是否能取空值
4. Key :在mysql中key 和index 是一樣的意思,這個Key列可能會看到有如下的值:PRI(主鍵)、MUL(普通的b-tree索引)、UNI(唯一索引)
5. Default: 列的預設值
6. Extra :其它信息
查看指定表的創建命令: (既創建過程)
MariaDB [hellodb]> show create table students;
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
`StuID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(50) NOT NULL,
`Age` tinyint(3) unsigned NOT NULL,
`Gender` enum('F','M') NOT NULL,
`ClassID` tinyint(3) unsigned DEFAULT NULL,
`TeacherID` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`StuID`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
查看看表的狀態:(查看指定某個表狀態要加單引號)
MariaDB [hellodb]> SHOW TABLE STATUS LIKE 'students'\G;
*************************** 1. row ***************************
Name: students
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 25
Avg_row_length: 655
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 26
Create_time: 2019-11-20 21:19:51
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
查看庫中所有表的狀態:
MariaDB [hellodb]> show table status from hellodb\G;
*************************** 1. row ***************************
Name: classes
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 8
Avg_row_length: 2048
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 9
Create_time: 2019-11-20 21:19:51
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
*************************** 2. row ***************************
Name: coc
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 14
Avg_row_length: 1170
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 15
Create_time: 2019-11-20 21:19:51
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
.
.
.(省略證明我不是來混行數的)
.
.
*************************** 8. row ***************************
Name: teachers
Engine: InnoDB (這裡是使用什麼存儲引擎的意思)
Version: 10
Row_format: Dynamic
Rows: 4
Avg_row_length: 4096
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 5
Create_time: 2019-11-20 21:19:51
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
*************************** 9. row ***************************
Name: toc
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 1
Create_time: 2019-11-20 21:19:51
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
9 rows in set (0.00 sec)
- 修改刪除表
刪除表:
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| newstudents |
| newstudents2 |
| scores |
| students |
| students2 |
| teachers |
| toc |
+-------------------+
10 rows in set (0.00 sec)
MariaDB [hellodb]> drop table newstudents2;
Query OK, 0 rows affected (0.00 sec)
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| newstudents |
| scores |
| students |
| students2 |
| teachers |
| toc |
+-------------------+
9 rows in set (0.00 sec)
查看修改表幫助:
MariaDB [hellodb]> help ALTER TABLE
Name: 'ALTER TABLE'
Description:
Syntax:
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]
修改表:
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
MariaDB [hellodb]> alter table students rename s1; (修改表名為s1)
Query OK, 0 rows affected (0.00 sec)
MariaDB [hellodb]> select * from students;
ERROR 1146 (42S02): Table 'hellodb.students' doesn't exist
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| s1 |
| scores |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
MariaDB [hellodb]> alter table s1 add phone varchar(11) after name; (在name欄位後面添加phone欄位) ADD:添加欄位 varchar可變的 字元長度為11為
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [hellodb]> select * from s1;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
MariaDB [hellodb]> alter table s1 modify phone int; (把phone的數據類型改為int)
Query OK, 25 rows affected (0.01 sec)
Records: 25 Duplicates: 0 Warnings: 0
MariaDB [hellodb]> desc s1
-> ;
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| StuID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Name | varchar(50) | NO | | NULL | |
| phone | int(11) | YES | | NULL | |
| Age | tinyint(3) unsigned | NO | | NULL | |
| Gender | enum('F','M') | NO | | NULL | |
| ClassID | tinyint(3) unsigned | YES | | NULL | |
| TeacherID | int(10) unsigned | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
(這些就不一一解釋了有興趣的可以自己試一試)
ALTER TABLE s1 CHANGE COLUMN phone mobile char(11);
ALTER TABLE s1 DROP COLUMN mobile;
ALTER TABLE s1 character set utf8;
ALTER TABLE s1 change name name varchar(20) character set utf8;
ALTER TABLE students ADD gender ENUM('m','f');
ALETR TABLE students CHANGE id sid int UNSIGNED NOT NULL PRIMARY KEY;
ALTER TABLE students drop primary key ;
ALTER TABLE students drop primary key ;
DESC students;
ALTER TABLE students DROP age;
DML語句
DML: INSERT, DELETE, UPDATE
INSERT 語句
功能:一次插入一行或多行數據 語法
UPDATE 語句
語句用於修改表中的數據。 註意:一定要有限制條件,否則將修改所有行的指定欄位
DELETE語句
語句用於刪除表中的行。 註意:一定要有限制條件,否則將清空表中的所有數據
可以在不刪除表的情況下刪除所有的行。這意味著表的結構、屬性和索引都是完整的:
DELETE FROM table_name 或者 DELETE * FROM table_name
- INSERT語句實驗: 三種語句
MariaDB [hellodb]> help insert;
Name: 'INSERT'
Description:
Syntax:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
Or:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
Or:
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
- 第一種語法
MariaDB [hellodb]> insert into s1 values(26,'xietingfeng',NULL,23,'M',2,1); (添加一行表記錄)
Query OK, 1 row affected (0.00 sec)
MariaDB [hellodb]> select * from s1;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
+-------+---------------+-------+-----+--------+---------+-----------+
26 rows in set (0.00 sec)
(如果出現這個提示可能是你少寫了一個欄位或者寫錯了)
MariaDB [hellodb]> insert into s1 values(26,'xietingfeng',23,'M',2,1);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
註意:如果想省略欄位名的話,只能所有欄位都賦值的話可以省略,單個賦值不可以省略。
如下:挑出三個欄位來賦值,必須對應的寫上。
MariaDB [hellodb]> insert into s1(name,age,classid) values ('liudehua',18,1);
Query OK, 1 row affected (0.00 sec)
MariaDB [hellodb]> select * from s1;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
27 rows in set (0.00 sec) (↑ 這裡是因為表結構設置預設不允許為空隨機分配的值)
一次行兩條記錄賦值:
MariaDB [hellodb]> insert into s1(name,age,gender,classid) values('mahuateng',20,'M',3),('wuyanzu',19,'M',4);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [hellodb]> select * from s1;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
29 rows in set (0.00 sec)
- 第二種語法
MariaDB [hellodb]> insert s1 set name='wuzetian',age=21,gender='F';
Query OK, 1 row affected (0.00 sec)
MariaDB [hellodb]> select * from s1;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
30 rows in set (0.00 sec)
(使用的不多一般使用第一種瞭解一下)
- 第三種語法
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)
MariaDB [hellodb]> insert s1 (name,classid,gender,age) select name,age,gender,18 from teachers;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [hellodb]> select * from s1;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
34 rows in set (0.00 sec)
(批量導入,A表參考B表的結構導入到B表中,主鍵必須是唯一的不可導入A表的主鍵)
- UPDATE語句
MariaDB [hellodb]> help update; (語句幫助)
Name: 'UPDATE'
Description:
Syntax:
Single-table syntax:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
MariaDB [hellodb]> select * from n1; (不加指定修改會把整個列都修改)
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
34 rows in set (0.00 sec)
MariaDB [hellodb]> update n1 set teacherid=1;
Query OK, 32 rows affected (0.00 sec)
Rows matched: 34 Changed: 32 Warnings: 0
MariaDB [hellodb]> select * from n1;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 1 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 1 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 1 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 1 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 6 | Shi Qing | NULL | 46 | M | 5 | 1 |
| 7 | Xi Ren | NULL | 19 | F | 3 | 1 |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | 1 |
| 9 | Ren Yingying | NULL | 20 | F | 6 | 1 |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | 1 |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | 1 |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | 1 |
| 13 | Tian Boguang | NULL | 33 | M | 2 | 1 |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | 1 |
| 15 | Duan Yu | NULL | 19 | M | 4 | 1 |
| 16 | Xu Zhu | NULL | 21 | M | 1 | 1 |
| 17 | Lin Chong | NULL | 25 | M | 4 | 1 |
| 18 | Hua Rong | NULL | 23 | M | 7 | 1 |
| 19 | Xue Baochai | NULL | 18 | F | 6 | 1 |
| 20 | Diao Chan | NULL | 19 | F | 7 | 1 |
| 21 | Huang Yueying | NULL | 22 | F | 6 | 1 |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | 1 |
| 23 | Ma Chao | NULL | 23 | M | 4 | 1 |
| 24 | Xu Xian | NULL | 27 | M | NULL | 1 |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | 1 |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 27 | liudehua | NULL | 18 | F | 1 | 1 |
| 28 | mahuateng | NULL | 20 | M | 3 | 1 |
| 29 | wuyanzu | NULL | 19 | M | 4 | 1 |
| 30 | wuzetian | NULL | 21 | F | NULL | 1 |
| 31 | Song Jiang | NULL | 18 | M | 45 | 1 |
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | 1 |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | 1 |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | 1 |
+-------+---------------+-------+-----+--------+---------+-----------+
34 rows in set (0.00 sec)
避免的方法:登陸的時候加-U進入安全模式,改模式下不加where指定不能更改。
[root@centos7 ~]#mysql -U -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.2.29-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> update n1 set teacherid=2;
ERROR 1046 (3D000): No database selected
MariaDB [(none)]>
或者修改配文件: 在mysql下麵加上這一行
[root@centos7 ~]#vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
safe-updates
MariaDB [hellodb]> update n1 set teacherid=2 where stuid=25; (用where指定主鍵的某一範圍修改)
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [hellodb]> select * from n1
-> ;
+-------+---------------+-------+-----+--------+---------+-----------+
| stuid | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 1 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 1 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 1 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 1 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 6 | Shi Qing | NULL | 46 | M | 5 | 1 |
| 7 | Xi Ren | NULL | 19 | F | 3 | 1 |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | 1 |
| 9 | Ren Yingying | NULL | 20 | F | 6 | 1 |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | 1 |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | 1 |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | 1 |
| 13 | Tian Boguang | NULL | 33 | M | 2 | 1 |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | 1 |
| 15 | Duan Yu | NULL | 19 | M | 4 | 1 |
| 16 | Xu Zhu | NULL | 21 | M | 1 | 1 |
| 17 | Lin Chong | NULL | 25 | M | 4 | 1 |
| 18 | Hua Rong | NULL | 23 | M | 7 | 1 |
| 19 | Xue Baochai | NULL | 18 | F | 6 | 1 |
| 20 | Diao Chan | NULL | 19 | F | 7 | 1 |
| 21 | Huang Yueying | NULL | 22 | F | 6 | 1 |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | 1 |
| 23 | Ma Chao | NULL | 23 | M | 4 | 1 |
| 24 | Xu Xian | NULL | 27 | M | NULL | 1 |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | 2 |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 27 | liudehua | NULL | 18 | F | 1 | 1 |
| 28 | mahuateng | NULL | 20 | M | 3 | 1 |
| 29 | wuyanzu | NULL | 19 | M | 4 | 1 |
| 30 | wuzetian | NULL | 21 | F | NULL | 1 |
| 31 | Song Jiang | NULL | 18 | M | 45 | 1 |
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | 1 |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | 1 |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | 1 |
+-------+---------------+-------+-----+--------+---------+-----------+
34 rows in set (0.00 sec)
註意:安全模式下,這樣修改表必須有主鍵才可以修改。
添加主鍵方法:
MariaDB [hellodb]> alter table n1 change stuid stuid int primary key;
- DELETE語句:(也要加where) (-U 和配置文件修改都支持)
MariaDB [hellodb]> delete from n1 where stuid=28; (刪除這一行)
Query OK, 1 row affected (0.00 sec)
MariaDB [hellodb]> select * from n1;
+-------+---------------+-------+-----+--------+---------+-----------+
| stuid | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 24 | Xu Xian | NULL | 27 | M | NULL | 1 |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | 2 |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 27 | liudehua | NULL | 18 | F | 1 | 1 |
| 29 | wuyanzu | NULL | 19 | M | 4 | 1 |
| 30 | wuzetian | NULL | 21 | F | NULL | 1 |
| 31 | Song Jiang | NULL | 18 | M | 45 | 1 |
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | 1 |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | 1 |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | 1 |
+-------+---------------+-------+-----+--------+---------+-----------+
33 rows in set (0.00 sec)
(第二種清空數據保留表結構的方式)
MariaDB [hellodb]> delete from n1;
Query OK, 33 rows affected (0.00 sec)
MariaDB [hellodb]> select * from n1;
Empty set (0.00 sec)
MariaDB [hellodb]> desc n1; (表結構還在)
+-----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| stuid | int(11) | NO | PRI | NULL | |
| Name | varchar(50) | NO | | NULL | |
| phone | int(11) | YES | | NULL | |
| Age | tinyint(3) unsigned | NO | | NULL | |
| Gender | enum('F','M') | NO | | NULL | |
| ClassID | tinyint(3) unsigned | YES | | NULL | |
| TeacherID | int(10) unsigned | YES | | NULL | |
+-----------+---------------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
DQL語句
查詢語句:SELECT (單表和多表)
單表操作
語法:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[SQL_CACHE | SQL_NO_CACHE]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[FOR UPDATE | LOCK IN SHARE MODE]
說明:
- 欄位顯示可以使用別名: col1 AS alias1, col2 AS alias2, ...
- WHERE子句:指明過濾條件以實現“選擇”的功能: 過濾條件:布爾型表達式 算術操作符:+, -, *,
/, % 比較操作符:=,<=>(相等或都為空), <>, !=(非標準SQL), >, >=, <, <= BETWEEN min_num
AND max_num IN (element1, element2, ...) IS NULL IS NOT NULL SELECT DISTINCT 去除重覆
列,範例:SELECT DISTINCT gender FROM students; LIKE: % 任意長度的任意字元 _ 任意單個字
符 RLIKE:正則表達式,索引失效,不建議使用 REGEXP:匹配字元串可用正則表達式書寫模式,
同上 邏輯操作符:NOT,AND,OR,XOR - GROUP:根據指定的條件把查詢結果進行“分組”以用於做“聚合”運算 常見聚合函數:avg(), max(),
min(), count(), sum() HAVING: 對分組聚合運算後的結果指定過濾條件 - ORDER BY: 根據指定的欄位對查詢結果進行排序 升序:ASC 降序:DESC
- LIMIT [[offset,]row_count]:對查詢的結果進行輸出行數數量限制
- 對查詢結果中的數據請求施加“鎖” FOR UPDATE: 寫鎖,獨占或排它鎖,只有一個讀和寫操作
LOCK IN SHARE MODE: 讀鎖,共用鎖,同時多個讀操作
範例:
TRUNCATE TABLE tbl_name;
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[SQL_CACHE | SQL_NO_CACHE]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[FOR UPDATE | LOCK IN SHARE MODE]
DESC students;
INSERT INTO students VALUES(1,'tom','m'),(2,'alice','f');
INSERT INTO students(id,name) VALUES(3,'jack'),(4,'allen');
SELECT * FROM students WHERE id < 3;
SELECT * FROM students WHERE gender='m';
SELECT * FROM students WHERE gender IS NULL;
SELECT * FROM students WHERE gender IS NOT NULL;
SELECT * FROM students ORDER BY name DESC LIMIT 2;
SELECT * FROM students ORDER BY name DESC LIMIT 1,2;
SELECT * FROM students WHERE id >=2 and id <=4
SELECT * FROM students WHERE BETWEEN 2 AND 4
SELECT * FROM students WHERE name LIKE ‘t%’
SELECT * FROM students WHERE name RLIKE '.*[lo].*';
SELECT id stuid,name as stuname FROM students
- 單表實驗:
- 展示表裡的所有內容*代表所有欄位,也可以挑指定欄位來顯示。
MariaDB [hellodb]> select * from s1;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
.... 省略掉
+-------+---------------+-------+-----+--------+---------+-----------+
34 rows in set (0.00 sec)
- 甚至可以做數字運算
MariaDB [hellodb]> select 2*3;
+-----+
| 2*3 |
+-----+
| 6 |
+-----+
1 row in set (0.01 sec)
MariaDB [hellodb]> select 2*3 from teachers; (指定一個表,表裡有幾行做幾次數字運算和AWK很相似)
+-----+
| 2*3 |
+-----+
| 6 |
| 6 |
| 6 |
| 6 |
+-----+
4 rows in set (0.00 sec)
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)
- 指定欄位名更改 加*的話不行
MariaDB [hellodb]> select '韋小寶',name from teachers;
+-----------+---------------+
| 韋小寶 | name |
+-----------+---------------+
| 韋小寶 | Song Jiang |
| 韋小寶 | Zhang Sanfeng |
| 韋小寶 | Miejue Shitai |
| 韋小寶 | Lin Chaoying |
+-----------+---------------+
4 rows in set (0.00 sec)
MariaDB [hellodb]> select '韋小寶',name,age,gender from teachers;
+-----------+---------------+-----+--------+
| 韋小寶 | name | age | gender |
+-----------+---------------+-----+--------+
| 韋小寶 | Song Jiang | 45 | M |
| 韋小寶 | Zhang Sanfeng | 94 | M |
| 韋小寶 | Miejue Shitai | 77 | F |
| 韋小寶 | Lin Chaoying | 93 | F |
+-----------+---------------+-----+--------+
4 rows in set (0.00 sec)
(只是顯示效果更改)
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)
- 對錶頭起別名顯示
MariaDB [hellodb]> select '韋小寶',name as "姓名",age "年齡",gender from teachers;
+-----------+---------------+--------+--------+
| 韋小寶 | 姓名 | 年齡 | gender |
+-----------+---------------+--------+--------+
| 韋小寶 | Song Jiang | 45 | M |
| 韋小寶 | Zhang Sanfeng | 94 | M |
| 韋小寶 | Miejue Shitai | 77 | F |
| 韋小寶 | Lin Chaoying | 93 | F |
+-----------+---------------+--------+--------+
4 rows in set (0.01 sec)
- 行過濾:挑出性別不是M的 !=是非標準的SQL語法 標準的為<>
MariaDB [hellodb]> select * from s1 where gender != 'M'; (只能在MySQL資料庫使用)
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
+-------+-------------