SQL语言(二)

INSERT


  • 语法一

语法: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] ... ]

INSERT System_List VALUES(1,'RedHat','CentOS',7.5);
INSERT System_List(system_id,system_name,system_release) VALUES(2,'Debian','Ubuntu');
MariaDB [testdb1]> SELECT * FROM System_List;
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         1 | RedHat      | CentOS         |            7.5 |
|         2 | Debian      | Ubuntu         |           NULL |
+-----------+-------------+----------------+----------------+
2 rows in set (0.00 sec)
  • 语法二

语法: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] ... ]

INSERT System_List SET system_id=3, system_name='RedHat', system_release='REHL', system_version=7.4;
MariaDB [testdb1]> SELECT * FROM System_List;
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         1 | RedHat      | CentOS         |            7.5 |
|         2 | Debian      | Ubuntu         |           NULL |
|         3 | RedHat      | REHL           |            7.4 |
+-----------+-------------+----------------+----------------+
3 rows in set (0.00 sec)
  • 语法三

语法:INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] SELECT ... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ]

INSERT testable2(user,host) SELECT system_name,system_release FROM System_List;
MariaDB [testdb1]> SELECT * FROM testable2;
+--------+------------------+-------------------------------------------+
| user   | host             | password                                  |
+--------+------------------+-------------------------------------------+
| RedHat | CentOS           |                                           |
| Debian | Ubuntu           |                                           |
| RedHat | REHL             |                                           |
+--------+------------------+-------------------------------------------+
3 rows in set (0.00 sec)

UPDATE


语法:UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ... [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]

UPDATE System_List SET system_release='Fedora',system_version=28 WHERE system_id=1;
MariaDB [testdb1]> SELECT * FROM System_List;
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         1 | RedHat      | Fedora         |             28 |
|         2 | Debian      | Ubuntu         |           NULL |
|         3 | RedHat      | REHL           |            7.4 |
+-----------+-------------+----------------+----------------+
3 rows in set (0.00 sec)

DELETE


语法:DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]

DELETE FROM System_List WHERE system_id=2;

删除操作极为危险,所以在生产环境中尽量避免使用。可以在表中添加一个字段,作为标记位。当标记位为指定的值时,我们便将该字段判定为已删除。同时,当登录 MySQL 客户端时,指定 --safe-updates 或者 -U 选项,以保证当对表进行修改时,必须使用 WHERE 关键字

MariaDB [testdb1]> DALETE FROM System_List;
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 'DALETE FROM System_List' at line 1

当然,在生产中也难以避免对表的删除需求。此时使用 DELETE 对表进行删除的效率就会非常笨重,但优点是当误删除时,可以通过日志进行回滚。也可以使用 TRUNCATE TABLE tbl_name 对整张表进行删除,此操作高效,但是不可回滚,非常危险!!!

SELECT


  • 单表查询

简单查询

--'*'表示所有字段
SELECT * FROM System_List;
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         1 | RedHat      | Fedora         |             28 |
|         2 | Debian      | Ubuntu         |           18.4 |
|         3 | RedHat      | REHL           |            7.4 |
|         4 | Debian      | Mint           |             19 |
|         5 | RedHat      | CentOS         |            7.5 |
+-----------+-------------+----------------+----------------+
5 rows in set (0.00 sec)

SELECT system_release FROM System_List;
+----------------+
| system_release |
+----------------+
| Fedora         |
| Ubuntu         |
| REHL           |
| Mint           |
| CentOS         |
+----------------+
5 rows in set (0.00 sec)

WHERE子句

操作符 功能
= 等于
<> 不等于
> 大于
< 小于
>= 大于等于
<= 小于等于
BETWEEN X AND Y 在X与Y之间
LIKE 模糊匹配
IN 包含
IS [NOT] NULL 是否为空

注:RLIKE 用于通过正则表达式匹配,但是不推荐使用。因为 RLIKE 子句将会使索引失效。

SELECT * FROM System_List WHERE system_id >= 2 ;
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         2 | Debian      | Ubuntu         |           18.4 |
|         3 | RedHat      | REHL           |            7.4 |
|         4 | Debian      | Mint           |             19 |
|         5 | RedHat      | CentOS         |            7.5 |
+-----------+-------------+----------------+----------------+
4 rows in set (0.00 sec)
SELECT * FROM System_List WHERE system_id BETWEEN 2 AND 5;
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         2 | Debian      | Ubuntu         |           18.4 |
|         3 | RedHat      | REHL           |            7.4 |
|         4 | Debian      | Mint           |             19 |
|         5 | RedHat      | CentOS         |            7.5 |
+-----------+-------------+----------------+----------------+
4 rows in set (0.00 sec)
--'%'用于表示任意个数任意字符
SELECT * FROM System_List WHERE system_name LIKE 'D%';
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         2 | Debian      | Ubuntu         |           18.4 |
|         4 | Debian      | Mint           |             19 |
+-----------+-------------+----------------+----------------+
2 rows in set (0.00 sec)

--'_'用于表示单个任意字符
SELECT * FROM System_List WHERE system_release LIKE '____';
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         3 | RedHat      | REHL           |            7.4 |
|         4 | Debian      | Mint           |             19 |
+-----------+-------------+----------------+----------------+
2 rows in set (0.00 sec)
SELECT * FROM System_List WHERE system_name IN ('RedHat');
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         1 | RedHat      | Fedora         |             28 |
|         3 | RedHat      | REHL           |            7.4 |
|         5 | RedHat      | CentOS         |            7.5 |
+-----------+-------------+----------------+----------------+
3 rows in set (0.00 sec)
SELECT * FROM System_List WHERE system_name IS NULL ;
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         6 | NULL        | SESU           |           42.3 |
+-----------+-------------+----------------+----------------+
1 row in set (0.00 sec)

DISTINCT子句

--DISTINCT子句用于过滤重复行
SELECT DISTINCT system_name FROM System_List;
+-------------+
| system_name |
+-------------+
| RedHat      |
| Debian      |
| NULL        |
+-------------+
3 rows in set (0.00 sec)

逻辑操作

--AND表示逻辑与 NOT表示逻辑非
SELECT * FROM System_List WHERE NOT system_id = 3 AND system_name = 'RedHat';
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         1 | RedHat      | Fedora         |             28 |
|         5 | RedHat      | CentOS         |            7.5 |
+-----------+-------------+----------------+----------------+
2 rows in set (0.00 sec)

--OR表示逻辑或
SELECT * FROM System_List WHERE system_id = 4 OR system_name = 'RedHat';
+-----------+-------------+----------------+----------------+
| system_id | system_name | system_release | system_version |
+-----------+-------------+----------------+----------------+
|         1 | RedHat      | Fedora         |             28 |
|         3 | RedHat      | REHL           |            7.4 |
|         4 | Debian      | Mint           |             19 |
|         5 | RedHat      | CentOS         |            7.5 |
+-----------+-------------+----------------+----------------+
4 rows in set (0.00 sec)

AS子句

SELECT system_id AS ID, system_name AS NAME FROM System_List AS LIST;
+----+--------+
| ID | NAME   |
+----+--------+
|  1 | RedHat |
|  2 | Debian |
|  3 | RedHat |
|  4 | Debian |
|  5 | RedHat |
|  6 | NULL   |
+----+--------+
6 rows in set (0.00 sec)

GROUP

--当分组查询指令逻辑不通时,将会打印每组的第一个字段
SELECT system_used FROM System_List GROUP BY system_name; 
+-------------+
| system_used |
+-------------+
|          40 |
|         100 |
|          30 |
+-------------+
3 rows in set (0.00 sec)

--max()函数用于统计字段中的最大值,min()相反
SELECT system_name, MAX(system_used) AS Max_System FROM System_List GROUP BY system_name; 
+-------------+------------+
| system_name | Max_System |
+-------------+------------+
| NULL        |         40 |
| Debian      |        100 |
| RedHat      |        120 |
+-------------+------------+
3 rows in set (0.00 sec)

--avg()函数用于计算平均数
SELECT system_name, AVG(system_used) AS Avg_System FROM System_List GROUP BY system_name; 
+-------------+------------+
| system_name | Avg_System |
+-------------+------------+
| NULL        |    40.0000 |
| Debian      |    55.0000 |
| RedHat      |    66.6667 |
+-------------+------------+
3 rows in set (0.00 sec)

--当分组以后,需要进行条件判断时必须使用HAVING子句
SELECT system_name, system_support, avg(system_used)
    -> FROM System_List GROUP BY system_name, system_support
    -> HAVING avg(system_used) > 50;
+-------------+----------------+------------------+
| system_name | system_support | avg(system_used) |
+-------------+----------------+------------------+
| Debian      | P              |          55.0000 |
| RedHat      | V              |          85.0000 |
+-------------+----------------+------------------+
2 rows in set (0.00 sec)

ORDER

--ORDER子句默认使用ASC选项,升序排列
SELECT * FROM System_List ORDER BY system_used;
+-----------+-------------+----------------+----------------+-------------+----------------+
| system_id | system_name | system_release | system_version | system_used | system_support |
+-----------+-------------+----------------+----------------+-------------+----------------+
|         4 | Debian      | Mint           |             19 |          10 | P              |
|         1 | RedHat      | Fedora         |             28 |          30 | P              |
|         6 | NULL        | SESU           |           42.3 |          40 | V              |
|         3 | RedHat      | REHL           |            7.4 |          50 | V              |
|         2 | Debian      | Ubuntu         |           18.4 |         100 | P              |
|         5 | RedHat      | CentOS         |            7.5 |         120 | V              |
+-----------+-------------+----------------+----------------+-------------+----------------+
6 rows in set (0.00 sec)

--使用DESC选项以倒序排列打印
SELECT * FROM System_List ORDER BY system_used DESC;
+-----------+-------------+----------------+----------------+-------------+----------------+
| system_id | system_name | system_release | system_version | system_used | system_support |
+-----------+-------------+----------------+----------------+-------------+----------------+
|         5 | RedHat      | CentOS         |            7.5 |         120 | V              |
|         2 | Debian      | Ubuntu         |           18.4 |         100 | P              |
|         3 | RedHat      | REHL           |            7.4 |          50 | V              |
|         6 | NULL        | SESU           |           42.3 |          40 | V              |
|         1 | RedHat      | Fedora         |             28 |          30 | P              |
|         4 | Debian      | Mint           |             19 |          10 | P              |
+-----------+-------------+----------------+----------------+-------------+----------------+
6 rows in set (0.00 sec)

--对字符串进行排序时将会根据字符的先后顺序排列打印
SELECT * FROM System_List ORDER BY system_release;
+-----------+-------------+----------------+----------------+-------------+----------------+
| system_id | system_name | system_release | system_version | system_used | system_support |
+-----------+-------------+----------------+----------------+-------------+----------------+
|         5 | RedHat      | CentOS         |            7.5 |         120 | V              |
|         1 | RedHat      | Fedora         |             28 |          30 | P              |
|         4 | Debian      | Mint           |             19 |          10 | P              |
|         3 | RedHat      | REHL           |            7.4 |          50 | V              |
|         6 | NULL        | SESU           |           42.3 |          40 | V              |
|         2 | Debian      | Ubuntu         |           18.4 |         100 | P              |
+-----------+-------------+----------------+----------------+-------------+----------------+
6 rows in set (0.00 sec)

LIMIT

--LIMIT用于对输出的结果进行切割,可以同时定义'偏移量,字段数'
SELECT * FROM System_List ORDER BY system_release LIMIT 2,3;
+-----------+-------------+----------------+----------------+-------------+----------------+
| system_id | system_name | system_release | system_version | system_used | system_support |
+-----------+-------------+----------------+----------------+-------------+----------------+
|         4 | Debian      | Mint           |             19 |          10 | P              |
|         3 | RedHat      | REHL           |            7.4 |          50 | V              |
|         6 | NULL        | SESU           |           42.3 |          40 | V              |
+-----------+-------------+----------------+----------------+-------------+----------------+
3 rows in set (0.00 sec)

  • 多表查询

实验环境

show tables;
+-------------------+
| Tables_in_testdb2 |
+-------------------+
| course            |
| score             |
| student           |
| teacher           |
+-------------------+
4 rows in set (0.01 sec)

select * from course;
+-------+-----------------+-----+
| cno   | cname           | tno |
+-------+-----------------+-----+
| 3-105 | 计算机导论      | 825 |
| 3-245 | 操作系统        | 804 |
| 6-166 | 数字电路        | 856 |
| 9-888 | 高等数学        | 831 |
+-------+-----------------+-----+
4 rows in set (0.00 sec)

select * from score;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 101 | 3-105 |     64 |
| 103 | 3-105 |     92 |
| 103 | 3-245 |     86 |
| 103 | 6-166 |     85 |
| 105 | 3-105 |     88 |
| 105 | 3-245 |     75 |
| 105 | 6-166 |     79 |
| 107 | 3-105 |     91 |
| 108 | 3-105 |     78 |
| 109 | 3-105 |     76 |
| 109 | 3-245 |     68 |
| 109 | 6-166 |     81 |
+-----+-------+--------+
12 rows in set (0.01 sec)

select * from student;
+-----+--------+------+---------------------+-------+
| sno | sname  | ssex | sbirthday           | class |
+-----+--------+------+---------------------+-------+
| 101 | 李军   | 男   | 1976-02-20 00:00:00 | 95033 |
| 103 | 陆君   | 男   | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明   | 男   | 1975-10-02 00:00:00 | 95031 |
| 107 | 王丽   | 女   | 1976-01-23 00:00:00 | 95033 |
| 108 | 曾华   | 男   | 1977-09-01 00:00:00 | 95033 |
| 109 | 王芳   | 女   | 1975-02-10 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
6 rows in set (0.02 sec)

select * from teacher;
+-----+--------+------+---------------------+-----------+-----------------+
| tno | tname  | tsex | tbirthday           | prof      | depart          |
+-----+--------+------+---------------------+-----------+-----------------+
| 804 | 李诚   | 男   | 1958-12-02 00:00:00 | 副教授    | 计算机系        |
| 825 | 王萍   | 女   | 1972-05-05 00:00:00 | 助教      | 计算机系        |
| 831 | 刘冰   | 女   | 1977-08-14 00:00:00 | 助教      | 电子工程系      |
| 856 | 张旭   | 男   | 1969-03-12 00:00:00 | 讲师      | 电子工程系      |
+-----+--------+------+---------------------+-----------+-----------------+
4 rows in set (0.00 sec)

内连接 

SELECT st.sname, c.cname, sc.degree FROM student AS st INNER JOIN course AS c INNER JOIN score AS sc ON st.sno = sc.sno AND sc.cno = c.cno;
+--------+-----------------+--------+
| sname  | cname           | degree |
+--------+-----------------+--------+
| 李军   | 计算机导论      |     64 |
| 陆君   | 计算机导论      |     92 |
| 匡明   | 计算机导论      |     88 |
| 王丽   | 计算机导论      |     91 |
| 曾华   | 计算机导论      |     78 |
| 王芳   | 计算机导论      |     76 |
| 陆君   | 操作系统        |     86 |
| 匡明   | 操作系统        |     75 |
| 王芳   | 操作系统        |     68 |
| 陆君   | 数字电路        |     85 |
| 匡明   | 数字电路        |     79 |
| 王芳   | 数字电路        |     81 |
+--------+-----------------+--------+
12 rows in set (0.00 sec)

交叉连接

SELECT * FROM course CROSS JOIN teacher;
+-------+-----------------+-----+-----+--------+------+---------------------+-----------+-----------------+
| cno   | cname           | tno | tno | tname  | tsex | tbirthday           | prof      | depart          |
+-------+-----------------+-----+-----+--------+------+---------------------+-----------+-----------------+
| 3-105 | 计算机导论      | 825 | 804 | 李诚   | 男   | 1958-12-02 00:00:00 | 副教授    | 计算机系        |
| 3-245 | 操作系统        | 804 | 804 | 李诚   | 男   | 1958-12-02 00:00:00 | 副教授    | 计算机系        |
| 6-166 | 数字电路        | 856 | 804 | 李诚   | 男   | 1958-12-02 00:00:00 | 副教授    | 计算机系        |
| 9-888 | 高等数学        | 831 | 804 | 李诚   | 男   | 1958-12-02 00:00:00 | 副教授    | 计算机系        |
| 3-105 | 计算机导论      | 825 | 825 | 王萍   | 女   | 1972-05-05 00:00:00 | 助教      | 计算机系        |
| 3-245 | 操作系统        | 804 | 825 | 王萍   | 女   | 1972-05-05 00:00:00 | 助教      | 计算机系        |
| 6-166 | 数字电路        | 856 | 825 | 王萍   | 女   | 1972-05-05 00:00:00 | 助教      | 计算机系        |
| 9-888 | 高等数学        | 831 | 825 | 王萍   | 女   | 1972-05-05 00:00:00 | 助教      | 计算机系        |
| 3-105 | 计算机导论      | 825 | 831 | 刘冰   | 女   | 1977-08-14 00:00:00 | 助教      | 电子工程系      |
| 3-245 | 操作系统        | 804 | 831 | 刘冰   | 女   | 1977-08-14 00:00:00 | 助教      | 电子工程系      |
| 6-166 | 数字电路        | 856 | 831 | 刘冰   | 女   | 1977-08-14 00:00:00 | 助教      | 电子工程系      |
| 9-888 | 高等数学        | 831 | 831 | 刘冰   | 女   | 1977-08-14 00:00:00 | 助教      | 电子工程系      |
| 3-105 | 计算机导论      | 825 | 856 | 张旭   | 男   | 1969-03-12 00:00:00 | 讲师      | 电子工程系      |
| 3-245 | 操作系统        | 804 | 856 | 张旭   | 男   | 1969-03-12 00:00:00 | 讲师      | 电子工程系      |
| 6-166 | 数字电路        | 856 | 856 | 张旭   | 男   | 1969-03-12 00:00:00 | 讲师      | 电子工程系      |
| 9-888 | 高等数学        | 831 | 856 | 张旭   | 男   | 1969-03-12 00:00:00 | 讲师      | 电子工程系      |
+-------+-----------------+-----+-----+--------+------+---------------------+-----------+-----------------+
16 rows in set (0.00 sec)

左外连接

SELECT c.cname, s.degree FROM course AS c LEFT OUTER JOIN score AS s ON s.cno = c.cno;
+-----------------+--------+
| cname           | degree |
+-----------------+--------+
| 计算机导论      |     64 |
| 计算机导论      |     92 |
| 计算机导论      |     88 |
| 计算机导论      |     91 |
| 计算机导论      |     78 |
| 计算机导论      |     76 |
| 操作系统        |     86 |
| 操作系统        |     75 |
| 操作系统        |     68 |
| 数字电路        |     85 |
| 数字电路        |     79 |
| 数字电路        |     81 |
| 高等数学        |   NULL |
+-----------------+--------+
13 rows in set (0.01 sec)

右外连接与左外连接一样,只是左右顺序的调整,这里不再赘述。

全外连接

SELECT sno, sname, ssex, sbirthday FROM student
    -> union
    -> SELECT tno, tname, tsex, tbirthday FROM teacher;
+-----+--------+------+---------------------+
| sno | sname  | ssex | sbirthday           |
+-----+--------+------+---------------------+
| 101 | 李军   | 男   | 1976-02-20 00:00:00 |
| 103 | 陆君   | 男   | 1974-06-03 00:00:00 |
| 105 | 匡明   | 男   | 1975-10-02 00:00:00 |
| 107 | 王丽   | 女   | 1976-01-23 00:00:00 |
| 108 | 曾华   | 男   | 1977-09-01 00:00:00 |
| 109 | 王芳   | 女   | 1975-02-10 00:00:00 |
| 804 | 李诚   | 男   | 1958-12-02 00:00:00 |
| 825 | 王萍   | 女   | 1972-05-05 00:00:00 |
| 831 | 刘冰   | 女   | 1977-08-14 00:00:00 |
| 856 | 张旭   | 男   | 1969-03-12 00:00:00 |
+-----+--------+------+---------------------+
10 rows in set (0.01 sec)

全外连接就是使用 UNION 子句将两个外连接表拼接起来,从而得到两个表的全集

自连接

SELECT t.sname AS 学生姓名, s.sname AS 教师姓名 FROM test AS s LEFT OUTER JOIN test AS t ON t.tid = s.sid where t.tid IS NOT NULL;
+--------------+--------------+
| 学生姓名     | 教师姓名     |
+--------------+--------------+
| 陆君         | 李诚         |
| 匡明         | 王萍         |
| 曾华         | 刘冰         |
| 王芳         | 张旭         |
+--------------+--------------+
4 rows in set (0.00 sec)

子查询

SELECT st.sname, sc.degree FROM student AS st LEFT OUTER JOIN score AS sc ON st.sno = sc.sno AND cno = "3-105" WHERE degree > (SELECT AVG(degree) FROM score);
+--------+--------+
| sname  | degree |
+--------+--------+
| 陆君   |     92 |
| 匡明   |     88 |
| 王丽   |     91 |
+--------+--------+
3 rows in set (0.00 sec)

 

 

 

你可能感兴趣的:(数据库,INSERT,DELETE,UPDATE,SELECT)