目录
-
MySQL的DDL、DML、DQL语句和单表增、删、改、查 - 实验准备:
- 实验开始:
- DDL语句
- DML语句
- DQL语句
- 单表操作的分组统计
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 |
+-------+---------------+-------+-----+--------+---------+-----------+
14 rows in set (0.00 sec)
MariaDB [hellodb]> select * from s1 where gender <> 'M'; (所有SQL数据库都可以使用)
+-------+---------------+-------+-----+--------+---------+-----------+
| 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 |
+-------+---------------+-------+-----+--------+---------+-----------+
14 rows in set (0.00 sec)
- 挑出大于30的
MariaDB [hellodb]> select * from s1 where age >=30;
+-------+--------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+--------------+-------+-----+--------+---------+-----------+
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
+-------+--------------+-------+-----+--------+---------+-----------+
5 rows in set (0.00 sec)
- 挑出20到30之间的
MariaDB [hellodb]> select * from s1 where age >=20 and age <=30;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | 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 |
| 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 |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
15 rows in set (0.00 sec)
MariaDB [hellodb]> select * from s1 where age between 20 and 30; (结果等价于上面的)
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | 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 |
| 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 |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
15 rows in set (0.00 sec)
- 挑出大于30小于20的
MariaDB [hellodb]> select * from s1 where age >=30 or age <=20;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 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 |
| 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 |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | 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 |
+-------+---------------+-------+-----+--------+---------+-----------+
22 rows in set (0.00 sec)
- 挑出1班,3班,6班的人
MariaDB [hellodb]> select * from s1 where classid in (1,3,6);
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 7 | Xi Ren | NULL | 19 | F | 3 | 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 |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
14 rows in set (0.00 sec)
- 查看班级id为空的
MariaDB [hellodb]> select * from s1 where classid is null;
+-------+-------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+-------------+-------+-----+--------+---------+-----------+
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
+-------+-------------+-------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
- 查看班级id不为空的
MariaDB [hellodb]> select * from s1 where classid is not null;
+-------+---------------+-------+-----+--------+---------+-----------+
| 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 |
| 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 |
| 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 |
+-------+---------------+-------+-----+--------+---------+-----------+
31 rows in set (0.00 sec)
- 添加空值必须指定为null
MariaDB [hellodb]> insert students (name,age,gender,classid)values('abc',20,'M',null);
Query OK, 1 row affected (0.00 sec)
MariaDB [hellodb]> select * from s1 where classid is null;
+-------+-------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+-------------+-------+-----+--------+---------+-----------+
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
| 38 | abc | NULL | 20 | M | NULL | NULL |
+-------+-------------+-------+-----+--------+---------+-----------+
4 rows in set (0.00 sec)
- 去除calssid重复的列
MariaDB [hellodb]> select distinct classid from s1;
+---------+
| classid |
+---------+
| 2 |
| 1 |
| 4 |
| 3 |
| 5 |
| 7 |
| 6 |
| NULL |
| 45 |
| 94 |
| 77 |
| 93 |
+---------+
12 rows in set (0.00 sec)
- where语句模糊搜索匹配w开头的行 (like)
MariaDB [hellodb]> select * from s1 where name like 'w%';
+-------+--------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+--------------+-------+-----+--------+---------+-----------+
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
+-------+--------------+-------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
MariaDB [hellodb]> select * from s1 where name like '%w%'; (这个显示为包含w的行)
+-------+--------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+--------------+-------+-----+--------+---------+-----------+
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
+-------+--------------+-------+-----+--------+---------+-----------+
4 rows in set (0.00 sec)
- 查看字符串数量为三的:用三个下划线来表示
MariaDB [hellodb]> select * from s1 where name like '___';
+-------+------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+------+-------+-----+--------+---------+-----------+
| 38 | abc | NULL | 20 | M | NULL | NULL |
+-------+------+-------+-----+--------+---------+-----------+
1 row in set (0.00 sec)
- rlike '^#'等价于like '#%'
MariaDB [hellodb]> select * from s1 where name rlike '^x';
+-------+-------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+-------------+-------+-----+--------+---------+-----------+
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
+-------+-------------+-------+-----+--------+---------+-----------+
7 rows in set (0.00 sec)
MariaDB [hellodb]> select * from s1 where name like 'x%';
+-------+-------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+-------------+-------+-----+--------+---------+-----------+
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
+-------+-------------+-------+-----+--------+---------+-----------+
7 rows in set (0.00 sec)
- 建立一个表存储用户和密码
MariaDB [hellodb]> create table user (id int primary key,name varchar(10) not null,password varchar(30) not null);
Query OK, 0 rows affected (0.00 sec)
MariaDB [hellodb]> insert user value(1,'alice','centos');
Query OK, 1 row affected (0.00 sec)
MariaDB [hellodb]> insert user value(2,'bob','linux');
Query OK, 1 row affected (0.00 sec)
MariaDB [hellodb]> select * from user;
+----+-------+----------+
| id | name | password |
+----+-------+----------+
| 1 | alice | centos |
| 2 | bob | linux |
+----+-------+----------+
2 rows in set (0.00 sec)
- 使用SQL注入(SQL注入漏洞)
MariaDB [hellodb]> select * from user where name='alice' and password='tuituibang'; (密码不对应返回空值)
Empty set (0.00 sec)
MariaDB [hellodb]> select * from user where name='alice'; -- ' and password='tuituibang'; (使用特殊符号密码不对应也可以查看)
+----+-------+----------+
| id | name | password |
+----+-------+----------+
| 1 | alice | centos |
+----+-------+----------+
1 row in set (0.00 sec)
构建一个复杂密码用户结果也一样
MariaDB [hellodb]> insert user values(3,'admin','p@ssw)rd!');
Query OK, 1 row affected (0.00 sec)
MariaDB [hellodb]> select * from user;
+----+-------+-----------+
| id | name | password |
+----+-------+-----------+
| 1 | alice | centos |
| 2 | bob | linux |
| 3 | admin | p@ssw)rd! |
+----+-------+-----------+
3 rows in set (0.00 sec)
MariaDB [hellodb]> select * from user where name='admin'; -- ' and password='tuituibang'; (这里的引号数量是单数)
+----+-------+-----------+
| id | name | password |
+----+-------+-----------+
| 3 | admin | p@ssw)rd! |
+----+-------+-----------+
1 row in set (0.00 sec)
在登陆的时候构建一个奇怪的密码也可以登录 (注意这里的引号数量必须是成对的)
MariaDB [hellodb]> select * from user where name='admin' and password='' or '1=1';
+----+-------+-----------+
| id | name | password |
+----+-------+-----------+
| 1 | alice | centos |
| 2 | bob | linux |
| 3 | admin | p@ssw)rd! |
+----+-------+-----------+
3 rows in set, 5 warnings (0.00 sec)
MariaDB [hellodb]> select * from user where name='admin' and password='' or '1'='1';
+----+-------+-----------+
| id | name | password |
+----+-------+-----------+
| 1 | alice | centos |
| 2 | bob | linux |
| 3 | admin | p@ssw)rd! |
+----+-------+-----------+
3 rows in set (0.00 sec)
单表操作的分组统计
- SQL的聚合函数
AVG(column) 返回某列的平均值
BINARY_CHECKSUM
CHECKSUM
CHECKSUM_AGG
COUNT(column) 返回某列的行数(不包括NULL值)
COUNT(*) 返回被选行数(函数返回在给定的选择中被选的行数)
COUNT(DISTINCT column) 返回相异结果的数目
FIRST(column) 返回在指定的域中第一个记录的值(SQLServer2000 不支持)
LAST(column) 返回在指定的域中最后一个记录的值(SQLServer2000 不支持)
MAX(column) 返回某列的最高值
MIN(column) 返回某列的最低值
STDEV(column)
STDEVP(column)
SUM(column) 返回某列的总和
VAR(column)
VARP(column)
- count 统计列的行数
MariaDB [hellodb]> select count(stuid) from s1;
+--------------+
| count(stuid) |
+--------------+
| 35 |
+--------------+
1 row in set (0.00 sec)
MariaDB [hellodb]> select count(classid) from s1;
+----------------+
| count(classid) |
+----------------+
| 31 | (有空值不统计)
+----------------+
1 row in set (0.00 sec)
MariaDB [hellodb]> select count(1) from s1; (跟count(主键)一样,只扫描主键)
+----------+
| count(1) |
+----------+
| 35 |
+----------+
1 row in set (0.00 sec)
MariaDB [hellodb]> select count(*) from s1; (和count(主键)使用方式一样,但是在性能上有略微的区别,mysql对前者做了优化。)
+----------+
| count(*) |
+----------+
| 35 |
+----------+
1 row in set (0.00 sec)
- 使用GROUP BY (根据指定的条件把查询结果进行“分组”以用于做“聚合”运算 常见聚合函数)统计男生女生分别多少个
MariaDB [hellodb]> select gender,count(*) from s1 group by gender ;
+--------+----------+
| gender | count(*) |
+--------+----------+
| F | 14 |
| M | 21 |
+--------+----------+
2 rows in set (0.00 sec)
(起个别名)
MariaDB [hellodb]> select gender 性别,count(*) 人数 from s1 group by gender ;
+--------+--------+
| 性别 | 人数 |
+--------+--------+
| F | 14 |
| M | 21 |
+--------+--------+
2 rows in set (0.00 sec)
- AVG 统计平均年龄
MariaDB [hellodb]> select gender,avg(age) from s1 group by gender;
+--------+----------+
| gender | avg(age) |
+--------+----------+
| F | 18.9286 |
| M | 29.1905 |
+--------+----------+
2 rows in set (0.00 sec)
- max 统计男女生最大的年龄
MariaDB [hellodb]> select gender,avg(age),max(age) from s1 group by gender;
+--------+----------+----------+
| gender | avg(age) | max(age) |
+--------+----------+----------+
| F | 18.9286 | 22 |
| M | 29.1905 | 100 |
+--------+----------+----------+
2 rows in set (0.00 sec)
- 分组之后过滤行的话不能加where 要加having (在group by后面使用)
MariaDB [hellodb]> select classid,gender,avg(age) from s1 group by classid,gender;
+---------+--------+----------+ (过滤掉空的行)
| classid | gender | avg(age) |
+---------+--------+----------+
| NULL | F | 21.0000 |
| NULL | M | 49.0000 |
| 1 | F | 19.0000 |
| 1 | M | 21.5000 |
| 2 | M | 32.7500 |
| 3 | F | 18.3333 |
| 3 | M | 23.0000 |
| 4 | M | 23.6000 |
| 5 | M | 46.0000 |
| 6 | F | 20.0000 |
| 6 | M | 23.0000 |
| 7 | F | 18.0000 |
| 7 | M | 23.0000 |
| 45 | M | 18.0000 |
| 77 | F | 18.0000 |
| 93 | F | 18.0000 |
| 94 | M | 18.0000 |
+---------+--------+----------+
17 rows in set (0.00 sec)
MariaDB [hellodb]> select classid,gender,avg(age) from s1 group by classid,gender where classid is not null;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where classid is not null' at line 1
MariaDB [hellodb]> select classid,gender,avg(age) from s1 group by classid,gender having classid is not null; (HAVING: 对分组聚合运算后的结果指定过滤条件)
+---------+--------+----------+
| classid | gender | avg(age) |
+---------+--------+----------+
| 1 | F | 19.0000 |
| 1 | M | 21.5000 |
| 2 | M | 32.7500 |
| 3 | F | 18.3333 |
| 3 | M | 23.0000 |
| 4 | M | 23.6000 |
| 5 | M | 46.0000 |
| 6 | F | 20.0000 |
| 6 | M | 23.0000 |
| 7 | F | 18.0000 |
| 7 | M | 23.0000 |
| 45 | M | 18.0000 |
| 77 | F | 18.0000 |
| 93 | F | 18.0000 |
| 94 | M | 18.0000 |
+---------+--------+----------+
15 rows in set (0.00 sec)
- 分组前可以使用where过滤 (在group by之前添加)
MariaDB [hellodb]> select classid,gender,avg(age) from s1 where classid is not null group by classid,gender;
+---------+--------+----------+
| classid | gender | avg(age) |
+---------+--------+----------+
| 1 | F | 19.0000 |
| 1 | M | 21.5000 |
| 2 | M | 32.7500 |
| 3 | F | 18.3333 |
| 3 | M | 23.0000 |
| 4 | M | 23.6000 |
| 5 | M | 46.0000 |
| 6 | F | 20.0000 |
| 6 | M | 23.0000 |
| 7 | F | 18.0000 |
| 7 | M | 23.0000 |
| 45 | M | 18.0000 |
| 77 | F | 18.0000 |
| 93 | F | 18.0000 |
| 94 | M | 18.0000 |
+---------+--------+----------+
15 rows in set (0.00 sec)
- ORDER BY 排序 单字段排序:默认按照字母正序排序
MariaDB [hellodb]> select * from s1 order by name;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 38 | abc | NULL | 20 | M | NULL | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
35 rows in set (0.00 sec)
- 倒序排序
MariaDB [hellodb]> select * from s1 order by name desc;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 38 | abc | NULL | 20 | M | NULL | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
35 rows in set (0.00 sec)
- 指定asc排序 (asc:以数字大小从小到大排序,空值默认为0排在最前面)
MariaDB [hellodb]> select * from s1 order by classid asc;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 38 | abc | NULL | 20 | M | NULL | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
35 rows in set (0.00 sec)
- 还是按照数字从小到大排序但是空值排在最后面
MariaDB [hellodb]> select * from s1 order by -classid desc;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | NULL |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 38 | abc | NULL | 20 | M | NULL | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
35 rows in set (0.00 sec)
- 多字段排序 (先对性别排序在对年龄排序)
MariaDB [hellodb]> select * from s1 order by gender , age;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 38 | abc | NULL | 20 | M | NULL | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
35 rows in set (0.00 sec)
- 先对性别降序排序在对年龄正序排序。
MariaDB [hellodb]> select * from s1 order by gender desc,age asc;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | NULL |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 38 | abc | NULL | 20 | M | NULL | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 24 | Xu Xian | NULL | 27 | M | NULL | NULL |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 25 | Sun Dasheng | NULL | 100 | M | NULL | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 30 | wuzetian | NULL | 21 | F | NULL | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
35 rows in set (0.00 sec)
- 多字段排序之后无法在做过滤 (但是排序前可以)
MariaDB [hellodb]> select * from s1 order by gender desc,age asc where classid is not null;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where classid is not null' at line 1
MariaDB [hellodb]> select * from s1 order by gender desc,age asc having classid is not null;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'having classid is not null' at line 1
MariaDB [hellodb]> select * from s1 order by gender desc,age asc on classid is not null;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'on classid is not null' at line 1
(先过滤在排序)
MariaDB [hellodb]> select * from s1 where classid is not null order by gender desc,age asc; (和group by 一样还是在前面加)
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | NULL |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 28 | mahuateng | NULL | 20 | M | 3 | NULL |
| 16 | Xu Zhu | NULL | 21 | M | 1 | NULL |
| 1 | Shi Zhongyu | NULL | 22 | M | 2 | 3 |
| 2 | Shi Potian | NULL | 22 | M | 1 | 7 |
| 23 | Ma Chao | NULL | 23 | M | 4 | NULL |
| 26 | xietingfeng | NULL | 23 | M | 2 | 1 |
| 11 | Yuan Chengzhi | NULL | 23 | M | 6 | NULL |
| 18 | Hua Rong | NULL | 23 | M | 7 | NULL |
| 17 | Lin Chong | NULL | 25 | M | 4 | NULL |
| 5 | Yu Yutong | NULL | 26 | M | 3 | 1 |
| 4 | Ding Dian | NULL | 32 | M | 4 | 4 |
| 13 | Tian Boguang | NULL | 33 | M | 2 | NULL |
| 6 | Shi Qing | NULL | 46 | M | 5 | NULL |
| 3 | Xie Yanke | NULL | 53 | M | 2 | 16 |
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 10 | Yue Lingshan | NULL | 19 | F | 3 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 22 | Xiao Qiao | NULL | 20 | F | 1 | NULL |
| 9 | Ren Yingying | NULL | 20 | F | 6 | NULL |
| 21 | Huang Yueying | NULL | 22 | F | 6 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
31 rows in set (0.00 sec)
- 后面的分组字段对调位置的话显示效果不一样但是数据还是一样的。(排序逻辑不一样)
MariaDB [hellodb]> select gender,classid,avg(age) from s1 where classid is not null group by gender,classid;
+--------+---------+----------+
| gender | classid | avg(age) |
+--------+---------+----------+
| F | 1 | 19.0000 |
| F | 3 | 18.3333 |
| F | 6 | 20.0000 |
| F | 7 | 18.0000 |
| F | 77 | 18.0000 |
| F | 93 | 18.0000 |
| M | 1 | 21.5000 |
| M | 2 | 32.7500 |
| M | 3 | 23.0000 |
| M | 4 | 23.6000 |
| M | 5 | 46.0000 |
| M | 6 | 23.0000 |
| M | 7 | 23.0000 |
| M | 45 | 18.0000 |
| M | 94 | 18.0000 |
+--------+---------+----------+
15 rows in set (0.00 sec)
MariaDB [hellodb]> select gender,classid,avg(age) from s1 where classid is not null group by classid,gender;
+--------+---------+----------+
| gender | classid | avg(age) |
+--------+---------+----------+
| F | 1 | 19.0000 |
| M | 1 | 21.5000 |
| M | 2 | 32.7500 |
| F | 3 | 18.3333 |
| M | 3 | 23.0000 |
| M | 4 | 23.6000 |
| M | 5 | 46.0000 |
| F | 6 | 20.0000 |
| M | 6 | 23.0000 |
| F | 7 | 18.0000 |
| M | 7 | 23.0000 |
| M | 45 | 18.0000 |
| F | 77 | 18.0000 |
| F | 93 | 18.0000 |
| M | 94 | 18.0000 |
+--------+---------+----------+
15 rows in set (0.00 sec)
- LIMIT [[offset,]row_count]:对查询的结果进行输出行数数量限制
正序排序取岁数最小的前10名 limit 作为限制条件
MariaDB [hellodb]> select * from s1 order by age limit 10;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 14 | Lu Wushuang | NULL | 17 | F | 3 | NULL |
| 8 | Lin Daiyu | NULL | 17 | F | 7 | NULL |
| 32 | Zhang Sanfeng | NULL | 18 | M | 94 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
10 rows in set (0.00 sec)
- 跳过前三行取岁数最小的前10名
MariaDB [hellodb]> select * from s1 order by age limit 3,10;
+-------+---------------+-------+-----+--------+---------+-----------+
| StuID | Name | phone | Age | Gender | ClassID | TeacherID |
+-------+---------------+-------+-----+--------+---------+-----------+
| 33 | Miejue Shitai | NULL | 18 | F | 77 | NULL |
| 19 | Xue Baochai | NULL | 18 | F | 6 | NULL |
| 31 | Song Jiang | NULL | 18 | M | 45 | NULL |
| 27 | liudehua | NULL | 18 | F | 1 | NULL |
| 34 | Lin Chaoying | NULL | 18 | F | 93 | NULL |
| 20 | Diao Chan | NULL | 19 | F | 7 | NULL |
| 12 | Wen Qingqing | NULL | 19 | F | 1 | NULL |
| 15 | Duan Yu | NULL | 19 | M | 4 | NULL |
| 7 | Xi Ren | NULL | 19 | F | 3 | NULL |
| 29 | wuyanzu | NULL | 19 | M | 4 | NULL |
+-------+---------------+-------+-----+--------+---------+-----------+
10 rows in set (0.00 sec)
- 去重之后在做过滤 (这样取出来的数值比较准确)
MariaDB [hellodb]> select distinct age from s1 order by age limit 3; (去重取前三个)
+-----+
| age |
+-----+
| 17 |
| 18 |
| 19 |
+-----+
3 rows in set (0.00 sec)
MariaDB [hellodb]> select distinct age from s1 order by age limit 3,6; (去重跳过前3行取6行)
+-----+
| age |
+-----+
| 20 |
| 21 |
| 22 |
| 23 |
| 25 |
| 26 |
+-----+
6 rows in set (0.00 sec)