版权声明:本文为博主原创文章,转载请注明作者和出处。https://blog.csdn.net/xq920831/article/details/82810057
开始今天的内容。
mysql常用命令
参考博客:https://www.cnblogs.com/alex3714/articles/5950372.html
MySQL 创建数据表
语法
1 |
|
创建一个student表
1 2 3 4 5 6 7 |
|
实例解析:
- 如果你不想字段为 NULL 可以设置字段的属性为 NOT NULL, 在操作数据库时如果输入该字段的数据为NULL ,就会报错。
- AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1。
- PRIMARY KEY关键字用于定义列为主键。 您可以使用多列来定义主键,列间以逗号分隔。
MySQL 插入数据
语法
1 2 3 |
|
插入数据
1 2 3 4 5 6 7 8 9 10 11 |
|
MySQL 查询数据
语法
1 2 3 4 |
|
- 查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
- SELECT 命令可以读取一条或者多条记录。
- 你可以使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
- 你可以使用 WHERE 语句来包含任何条件。
- 你可以通过OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0。
- 你可以使用 LIMIT 属性来设定返回的记录数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
MySQL where 子句
语法
1 2 |
|
以下为操作符列表,可用于 WHERE 子句中。
下表中实例假定 A为10 B为20
操作符 | 描述 | 实例 |
---|---|---|
= | 等号,检测两个值是否相等,如果相等返回true | (A = B) 返回false。 |
<>, != | 不等于,检测两个值是否相等,如果不相等返回true | (A != B) 返回 true。 |
> | 大于号,检测左边的值是否大于右边的值, 如果左边的值大于右边的值返回true | (A > B) 返回false。 |
< | 小于号,检测左边的值是否小于右边的值, 如果左边的值小于右边的值返回true | (A < B) 返回 true。 |
>= | 大于等于号,检测左边的值是否大于或等于右边的值, 如果左边的值大于或等于右边的值返回true | (A >= B) 返回false。 |
<= | 小于等于号,检测左边的值是否小于于或等于右边的值, 如果左边的值小于或等于右边的值返回true | (A <= B) 返回 true。 |
使用主键来作为 WHERE 子句的条件查询是非常快速的。
1 |
|
MySQL UPDATE 查询
语法
1 2 |
|
1 |
|
MySQL DELETE 语句
语法
1 |
|
MySQL LIKE 子句
语法
1 2 3 4 5 6 |
|
MySQL 排序
1 2 3 4 |
|
MySQL GROUP BY 语句
1 2 3 4 |
|
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
下面是针对上述内容,我在自己电脑上的实际操作。
Last login: Wed Sep 19 13:56:19 on console
xuqiangdeMacBook-Pro-3:~ xuqiang$ alias mysql=/usr/local/mysql/bin/mysql
xuqiangdeMacBook-Pro-3:~ xuqiang$ alias mysqladmin=/usr/local/mysql/bin/mysqladmin
xuqiangdeMacBook-Pro-3:~ xuqiang$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.12 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database mydb1 charset "utf8";
Query OK, 1 row affected, 1 warning (0.10 sec)
mysql> use mydb1
Database changed
mysql> create table student(
-> id int AUTO_INCREMENT,
-> name char(32) not null,
-> age int not null,
-> regsiter_time date,
-> primary key (id));
Query OK, 0 rows affected (0.11 sec)
mysql> show tables;
+-----------------+
| Tables_in_mydb1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
mysql> insert into student(name,age,regsiter_time) values("agentxu",26,"2018-9-21");
Query OK, 1 row affected (0.03 sec)
mysql> insert into student(name,age,regsiter_time) values("agentsun",27,"2018-9-21");
Query OK, 1 row affected (0.07 sec)
mysql> select * from student;
+----+----------+-----+---------------+
| id | name | age | regsiter_time |
+----+----------+-----+---------------+
| 1 | agentxu | 26 | 2018-09-21 |
| 2 | agentsun | 27 | 2018-09-21 |
+----+----------+-----+---------------+
2 rows in set (0.00 sec)
mysql> insert into student(name,age,regsiter_time) values("agentzhao",27,"2018-9-21");
Query OK, 1 row affected (0.05 sec)
mysql> insert into student(name,age,regsiter_time) values("agentzhu",27,"2018-9-21");
Query OK, 1 row affected (0.07 sec)
mysql> insert into student(name,age,regsiter_time) values("agentfeng",27,"2018-9-21");
Query OK, 1 row affected (0.09 sec)
mysql> select * from student; +----+-----------+-----+---------------+
| id | name | age | regsiter_time |
+----+-----------+-----+---------------+
| 1 | agentxu | 26 | 2018-09-21 |
| 2 | agentsun | 27 | 2018-09-21 |
| 3 | agentzhao | 27 | 2018-09-21 |
| 4 | agentzhu | 27 | 2018-09-21 |
| 5 | agentfeng | 27 | 2018-09-21 |
+----+-----------+-----+---------------+
5 rows in set (0.00 sec)
mysql> select * from student limit 2 offset 1;
+----+-----------+-----+---------------+
| id | name | age | regsiter_time |
+----+-----------+-----+---------------+
| 2 | agentsun | 27 | 2018-09-21 |
| 3 | agentzhao | 27 | 2018-09-21 |
+----+-----------+-----+---------------+
2 rows in set (0.00 sec)
mysql> select * from student where id>4;
+----+-----------+-----+---------------+
| id | name | age | regsiter_time |
+----+-----------+-----+---------------+
| 5 | agentfeng | 27 | 2018-09-21 |
+----+-----------+-----+---------------+
1 row in set (0.00 sec)
mysql> insert into student(name,age,regsiter_time) values("agentfeng",7,"2018-9-21");
Query OK, 1 row affected (0.03 sec)
mysql> insert into student(name,age,regsiter_time) values("agentfeng",17,"2018-9-21");
Query OK, 1 row affected (0.06 sec)
mysql> insert into student(name,age,regsiter_time) values("agentzhao",57,"2018-9-21");
Query OK, 1 row affected (0.03 sec)
mysql> insert into student(name,age,regsiter_time) values("agentzhao",17,"2018-9-21");
Query OK, 1 row affected (0.07 sec)
mysql> insert into student(name,age,regsiter_time) values("agentzhao",17,"2018-8-21");
Query OK, 1 row affected (0.08 sec)
mysql> insert into student(name,age,regsiter_time) values("agentfeng",7,"2018-3-21");
Query OK, 1 row affected (0.09 sec)
mysql> insert into student(name,age,regsiter_time) values("agentxu",27,"2017-3-21");
Query OK, 1 row affected (0.08 sec)
mysql> select * from student;
+----+-----------+-----+---------------+
| id | name | age | regsiter_time |
+----+-----------+-----+---------------+
| 1 | agentxu | 26 | 2018-09-21 |
| 2 | agentsun | 27 | 2018-09-21 |
| 3 | agentzhao | 27 | 2018-09-21 |
| 4 | agentzhu | 27 | 2018-09-21 |
| 5 | agentfeng | 27 | 2018-09-21 |
| 6 | agentfeng | 7 | 2018-09-21 |
| 7 | agentfeng | 17 | 2018-09-21 |
| 8 | agentzhao | 57 | 2018-09-21 |
| 9 | agentzhao | 17 | 2018-09-21 |
| 10 | agentzhao | 17 | 2018-08-21 |
| 11 | agentfeng | 7 | 2018-03-21 |
| 12 | agentxu | 27 | 2017-03-21 |
+----+-----------+-----+---------------+
12 rows in set (0.00 sec)
mysql> update student set name="agentss" where id=6;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student set regsiter_time="2017-05-31" where id=6;
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+-----------+-----+---------------+
| id | name | age | regsiter_time |
+----+-----------+-----+---------------+
| 1 | agentxu | 26 | 2018-09-21 |
| 2 | agentsun | 27 | 2018-09-21 |
| 3 | agentzhao | 27 | 2018-09-21 |
| 4 | agentzhu | 27 | 2018-09-21 |
| 5 | agentfeng | 27 | 2018-09-21 |
| 6 | agentss | 7 | 2017-05-31 |
| 7 | agentfeng | 17 | 2018-09-21 |
| 8 | agentzhao | 57 | 2018-09-21 |
| 9 | agentzhao | 17 | 2018-09-21 |
| 10 | agentzhao | 17 | 2018-08-21 |
| 11 | agentfeng | 7 | 2018-03-21 |
| 12 | agentxu | 27 | 2017-03-21 |
+----+-----------+-----+---------------+
12 rows in set (0.00 sec)
mysql> delete from student where id=5;
Query OK, 1 row affected (0.04 sec)
mysql> select * from student;
+----+-----------+-----+---------------+
| id | name | age | regsiter_time |
+----+-----------+-----+---------------+
| 1 | agentxu | 26 | 2018-09-21 |
| 2 | agentsun | 27 | 2018-09-21 |
| 3 | agentzhao | 27 | 2018-09-21 |
| 4 | agentzhu | 27 | 2018-09-21 |
| 6 | agentss | 7 | 2017-05-31 |
| 7 | agentfeng | 17 | 2018-09-21 |
| 8 | agentzhao | 57 | 2018-09-21 |
| 9 | agentzhao | 17 | 2018-09-21 |
| 10 | agentzhao | 17 | 2018-08-21 |
| 11 | agentfeng | 7 | 2018-03-21 |
| 12 | agentxu | 27 | 2017-03-21 |
+----+-----------+-----+---------------+
11 rows in set (0.00 sec)
mysql> select * from student where regsiter_time like "2018-09%";
+----+-----------+-----+---------------+
| id | name | age | regsiter_time |
+----+-----------+-----+---------------+
| 1 | agentxu | 26 | 2018-09-21 |
| 2 | agentsun | 27 | 2018-09-21 |
| 3 | agentzhao | 27 | 2018-09-21 |
| 4 | agentzhu | 27 | 2018-09-21 |
| 7 | agentfeng | 17 | 2018-09-21 |
| 8 | agentzhao | 57 | 2018-09-21 |
| 9 | agentzhao | 17 | 2018-09-21 |
+----+-----------+-----+---------------+
7 rows in set, 1 warning (0.00 sec)
mysql> select * from student where regsiter_time like "2018-09%" order by age;
+----+-----------+-----+---------------+
| id | name | age | regsiter_time |
+----+-----------+-----+---------------+
| 7 | agentfeng | 17 | 2018-09-21 |
| 9 | agentzhao | 17 | 2018-09-21 |
| 1 | agentxu | 26 | 2018-09-21 |
| 2 | agentsun | 27 | 2018-09-21 |
| 3 | agentzhao | 27 | 2018-09-21 |
| 4 | agentzhu | 27 | 2018-09-21 |
| 8 | agentzhao | 57 | 2018-09-21 |
+----+-----------+-----+---------------+
7 rows in set, 1 warning (0.01 sec)
mysql> select * from student where regsiter_time like "2018-09%" order by age desc;
+----+-----------+-----+---------------+
| id | name | age | regsiter_time |
+----+-----------+-----+---------------+
| 8 | agentzhao | 57 | 2018-09-21 |
| 2 | agentsun | 27 | 2018-09-21 |
| 3 | agentzhao | 27 | 2018-09-21 |
| 4 | agentzhu | 27 | 2018-09-21 |
| 1 | agentxu | 26 | 2018-09-21 |
| 7 | agentfeng | 17 | 2018-09-21 |
| 9 | agentzhao | 17 | 2018-09-21 |
+----+-----------+-----+---------------+
7 rows in set, 1 warning (0.00 sec)
mysql> select name,count(*) from student group by name;
+-----------+----------+
| name | count(*) |
+-----------+----------+
| agentxu | 2 |
| agentsun | 1 |
| agentzhao | 4 |
| agentzhu | 1 |
| agentss | 1 |
| agentfeng | 2 |
+-----------+----------+
6 rows in set (0.00 sec)
mysql> select name,count(*)as name_cot from student group by name;
+-----------+----------+
| name | name_cot |
+-----------+----------+
| agentxu | 2 |
| agentsun | 1 |
| agentzhao | 4 |
| agentzhu | 1 |
| agentss | 1 |
| agentfeng | 2 |
+-----------+----------+
6 rows in set (0.00 sec)
mysql> select name,count(*)as name_cot from student group by name with rollup;
+-----------+----------+
| name | name_cot |
+-----------+----------+
| agentfeng | 2 |
| agentss | 1 |
| agentsun | 1 |
| agentxu | 2 |
| agentzhao | 4 |
| agentzhu | 1 |
| NULL | 11 |
+-----------+----------+
7 rows in set (0.00 sec)
mysql> select coalesce(name,"sum_cot"),count(*)as name_cot from student group by name with rollup;
+--------------------------+----------+
| coalesce(name,"sum_cot") | name_cot |
+--------------------------+----------+
| agentfeng | 2 |
| agentss | 1 |
| agentsun | 1 |
| agentxu | 2 |
| agentzhao | 4 |
| agentzhu | 1 |
| sum_cot | 11 |
+--------------------------+----------+
7 rows in set (0.00 sec)
MySQL ALTER命令
我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令。
删除,添加或修改表字段
1 |
|
修改字段类型及名称
如果需要修改字段类型及名称, 你可以在ALTER命令中使用 MODIFY 或 CHANGE 子句 。
例如,把字段 c 的类型从 CHAR(1) 改为 CHAR(10),可以执行以下命令:
1 |
|
使用 CHANGE 子句, 语法有很大的不同。 在 CHANGE 关键字之后,紧跟着的是你要修改的字段名,然后指定新字段名及类型。尝试如下实例:
1 2 3 |
|
ALTER TABLE 对 Null 值和默认值的影响
当你修改字段时,你可以指定是否包含只或者是否设置默认值。
以下实例,指定字段 j 为 NOT NULL 且默认值为100 。
1 2 |
|
mysql> alter table student sex enum("M","F");
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sex enum("M","F")' at line 1
mysql> alter table student add sex enum("M","F");
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-----------+-----+---------------+------+
| id | name | age | regsiter_time | sex |
+----+-----------+-----+---------------+------+
| 1 | agentxu | 26 | 2018-09-21 | NULL |
| 2 | agentsun | 27 | 2018-09-21 | NULL |
| 3 | agentzhao | 27 | 2018-09-21 | NULL |
| 4 | agentzhu | 27 | 2018-09-21 | NULL |
| 6 | agentss | 7 | 2017-05-31 | NULL |
| 7 | agentfeng | 17 | 2018-09-21 | NULL |
| 8 | agentzhao | 57 | 2018-09-21 | NULL |
| 9 | agentzhao | 17 | 2018-09-21 | NULL |
| 10 | agentzhao | 17 | 2018-08-21 | NULL |
| 11 | agentfeng | 7 | 2018-03-21 | NULL |
| 12 | agentxu | 27 | 2017-03-21 | NULL |
+----+-----------+-----+---------------+------+
11 rows in set (0.00 sec)
mysql> insert into student(name,age,regsiter_time,sex) values("agentxx",18,"2016-11-2","M");
Query OK, 1 row affected (0.03 sec)
mysql> select * from student; +----+-----------+-----+---------------+------+
| id | name | age | regsiter_time | sex |
+----+-----------+-----+---------------+------+
| 1 | agentxu | 26 | 2018-09-21 | NULL |
| 2 | agentsun | 27 | 2018-09-21 | NULL |
| 3 | agentzhao | 27 | 2018-09-21 | NULL |
| 4 | agentzhu | 27 | 2018-09-21 | NULL |
| 6 | agentss | 7 | 2017-05-31 | NULL |
| 7 | agentfeng | 17 | 2018-09-21 | NULL |
| 8 | agentzhao | 57 | 2018-09-21 | NULL |
| 9 | agentzhao | 17 | 2018-09-21 | NULL |
| 10 | agentzhao | 17 | 2018-08-21 | NULL |
| 11 | agentfeng | 7 | 2018-03-21 | NULL |
| 12 | agentxu | 27 | 2017-03-21 | NULL |
| 13 | agentxx | 18 | 2016-11-02 | M |
+----+-----------+-----+---------------+------+
12 rows in set (0.00 sec)
mysql> alter table student drop age;
Query OK, 0 rows affected (0.22 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-----------+---------------+------+
| id | name | regsiter_time | sex |
+----+-----------+---------------+------+
| 1 | agentxu | 2018-09-21 | NULL |
| 2 | agentsun | 2018-09-21 | NULL |
| 3 | agentzhao | 2018-09-21 | NULL |
| 4 | agentzhu | 2018-09-21 | NULL |
| 6 | agentss | 2017-05-31 | NULL |
| 7 | agentfeng | 2018-09-21 | NULL |
| 8 | agentzhao | 2018-09-21 | NULL |
| 9 | agentzhao | 2018-09-21 | NULL |
| 10 | agentzhao | 2018-08-21 | NULL |
| 11 | agentfeng | 2018-03-21 | NULL |
| 12 | agentxu | 2017-03-21 | NULL |
| 13 | agentxx | 2016-11-02 | M |
+----+-----------+---------------+------+
12 rows in set (0.00 sec)
mysql> update student set sex="M" where id<15;
Query OK, 11 rows affected (0.11 sec)
Rows matched: 12 Changed: 11 Warnings: 0
mysql> select * from student;
+----+-----------+---------------+------+
| id | name | regsiter_time | sex |
+----+-----------+---------------+------+
| 1 | agentxu | 2018-09-21 | M |
| 2 | agentsun | 2018-09-21 | M |
| 3 | agentzhao | 2018-09-21 | M |
| 4 | agentzhu | 2018-09-21 | M |
| 6 | agentss | 2017-05-31 | M |
| 7 | agentfeng | 2018-09-21 | M |
| 8 | agentzhao | 2018-09-21 | M |
| 9 | agentzhao | 2018-09-21 | M |
| 10 | agentzhao | 2018-08-21 | M |
| 11 | agentfeng | 2018-03-21 | M |
| 12 | agentxu | 2017-03-21 | M |
| 13 | agentxx | 2016-11-02 | M |
+----+-----------+---------------+------+
12 rows in set (0.00 sec)
mysql> alter table student modify sex enum("M","F") not null;
Query OK, 0 rows affected (0.21 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+---------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(32) | NO | | NULL | |
| regsiter_time | date | YES | | NULL | |
| sex | enum('M','F') | NO | | NULL | |
+---------------+---------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table student change sex gender char(32) not null default "M";
Query OK, 12 rows affected (0.05 sec)
Records: 12 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-----------+---------------+--------+
| id | name | regsiter_time | gender |
+----+-----------+---------------+--------+
| 1 | agentxu | 2018-09-21 | M |
| 2 | agentsun | 2018-09-21 | M |
| 3 | agentzhao | 2018-09-21 | M |
| 4 | agentzhu | 2018-09-21 | M |
| 6 | agentss | 2017-05-31 | M |
| 7 | agentfeng | 2018-09-21 | M |
| 8 | agentzhao | 2018-09-21 | M |
| 9 | agentzhao | 2018-09-21 | M |
| 10 | agentzhao | 2018-08-21 | M |
| 11 | agentfeng | 2018-03-21 | M |
| 12 | agentxu | 2017-03-21 | M |
| 13 | agentxx | 2016-11-02 | M |
+----+-----------+---------------+--------+
12 rows in set (0.00 sec)
mysql> desc student;
+---------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(32) | NO | | NULL | |
| regsiter_time | date | YES | | NULL | |
| gender | char(32) | NO | | M | |
+---------------+----------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
修改表名
1 |
|
mysql> alter table student rename to student_agent;
Query OK, 0 rows affected (0.01 sec)
mysql> desc student_agent;
+---------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(32) | NO | | NULL | |
| regsiter_time | date | YES | | NULL | |
| gender | char(32) | NO | | M | |
+---------------+----------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
mysql> alter table student_agent rename to students ;
Query OK, 0 rows affected (0.03 sec)
关于外键
外键,一个特殊的索引,用于关键2个表,只能是指定内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
mysql> create table `score`( `id` int not null, `subject` char(32) not null, `scores` int not null, `stu_id` int not null, primary key (`id`), key `fk_students_key` (`stu_id`), constraint `fk_students_key` foreign key (`stu_id`) references `students` (`id`));
Query OK, 0 rows affected (0.04 sec)
mysql> show tables;
+-----------------+
| Tables_in_mydb1 |
+-----------------+
| score |
| students |
+-----------------+
2 rows in set (0.00 sec)
mysql> desc score;
+---------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| subject | char(32) | NO | | NULL | |
| scores | int(11) | NO | | NULL | |
| stu_id | int(11) | NO | MUL | NULL | |
+---------+----------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> desc students;
+---------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(32) | NO | | NULL | |
| regsiter_time | date | YES | | NULL | |
| gender | char(32) | NO | | M | |
+---------------+----------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
mysql> alter table score modify id int auto_increment;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into score(subject,scores,stu_id) values("math",98,1);
Query OK, 1 row affected (0.02 sec)
mysql> insert into score(subject,scores,stu_id) values("math",90,2);
Query OK, 1 row affected (0.06 sec)
mysql> insert into score(subject,scores,stu_id) values("chinese",89,1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into score(subject,scores,stu_id) values("chinese",09,2);
Query OK, 1 row affected (0.09 sec)
mysql> insert into score(subject,scores,stu_id) values("english",78,1);
Query OK, 1 row affected (0.09 sec)
mysql> insert into score(subject,scores,stu_id) values("english",70,2);
Query OK, 1 row affected (0.06 sec)
mysql> select * from score;
+----+---------+--------+--------+
| id | subject | scores | stu_id |
+----+---------+--------+--------+
| 1 | math | 98 | 1 |
| 2 | math | 90 | 2 |
| 3 | chinese | 89 | 1 |
| 4 | chinese | 9 | 2 |
| 5 | english | 78 | 1 |
| 6 | english | 70 | 2 |
+----+---------+--------+--------+
6 rows in set (0.00 sec)
mysql> update score set scores=88 where id=4;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from score;
+----+---------+--------+--------+
| id | subject | scores | stu_id |
+----+---------+--------+--------+
| 1 | math | 98 | 1 |
| 2 | math | 90 | 2 |
| 3 | chinese | 89 | 1 |
| 4 | chinese | 88 | 2 |
| 5 | english | 78 | 1 |
| 6 | english | 70 | 2 |
+----+---------+--------+--------+
6 rows in set (0.00 sec)
mysql> select * from students;
+----+-----------+---------------+--------+
| id | name | regsiter_time | gender |
+----+-----------+---------------+--------+
| 1 | agentxu | 2018-09-21 | M |
| 2 | agentsun | 2018-09-21 | M |
| 3 | agentzhao | 2018-09-21 | M |
| 4 | agentzhu | 2018-09-21 | M |
| 6 | agentss | 2017-05-31 | M |
| 7 | agentfeng | 2018-09-21 | M |
| 8 | agentzhao | 2018-09-21 | M |
| 9 | agentzhao | 2018-09-21 | M |
| 10 | agentzhao | 2018-08-21 | M |
| 11 | agentfeng | 2018-03-21 | M |
| 12 | agentxu | 2017-03-21 | M |
| 13 | agentxx | 2016-11-02 | M |
+----+-----------+---------------+--------+
12 rows in set (0.00 sec)
这里需要注意的是创建外键连接表,关键字不是引号(会报错,不知为什么),需要用` ` 把关键字括起来。
参考:https://blog.csdn.net/haha_66666/article/details/78444457
MySQL NULL 值处理
我们已经知道MySQL使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作。
为了处理这种情况,MySQL提供了三大运算符:
IS NULL: 当列的值是NULL,此运算符返回true。
IS NOT NULL: 当列的值不为NULL, 运算符返回true。
<=>: 比较操作符(不同于=运算符),当比较的的两个值为NULL时返回true。
关于 NULL 的条件比较运算是比较特殊的。你不能使用 = NULL 或 != NULL 在列中查找 NULL 值 。
在MySQL中,NULL值与任何其它值的比较(即使是NULL)永远返回false,即 NULL = NULL 返回false 。
MySQL中处理NULL使用IS NULL和IS NOT NULL运算符。
Mysql 连接(left join, right join, inner join ,full join)
我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据。
本章节我们将向大家介绍如何使用 MySQL 的 JOIN 在两个或多个表中查询数据。
你可以在SELECT, UPDATE 和 DELETE 语句中使用 Mysql 的 JOIN 来联合多表查询。
JOIN 按照功能大致分为如下三类:
- INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
- LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
- RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
Suppose you have two tables, with a single column each, and data as follows:
1 2 3 4 5 6 |
|
Inner join
An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
1 2 3 4 5 6 7 |
|
其实就是只显示2个表的交集
Left join
A left join will give all rows in A, plus any common rows in B.
1 2 3 4 5 6 7 8 |
|
Right join
A right join will give all rows in B, plus any common rows in A.
1 2 3 4 5 6 7 8 |
|
Full join
A full outer join will give you the union of A and B, i.e. all the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa
1 2 3 4 5 6 7 8 9 10 |
|
mysql 并不直接支持full join,but 总是难不到我们
1 2 3 4 5 6 7 8 9 10 11 12 |
|
mysql> create table A(
-> a int not null);
Query OK, 0 rows affected (0.03 sec)
mysql> create table B( b int not null);
Query OK, 0 rows affected (0.04 sec)
mysql> insert into A(a) values(1);
Query OK, 1 row affected (0.10 sec)
mysql> insert into A(a) values(2);
Query OK, 1 row affected (0.09 sec)
mysql> insert into A(a) values(3);
Query OK, 1 row affected (0.07 sec)
mysql> insert into A(a) values(4);
Query OK, 1 row affected (0.05 sec)
mysql> insert into B(b) values(3);
Query OK, 1 row affected (0.01 sec)
mysql> insert into B(b) values(4);
Query OK, 1 row affected (0.04 sec)
mysql> insert into B(b) values(5);
Query OK, 1 row affected (0.02 sec)
mysql> insert into B(b) values(6);
Query OK, 1 row affected (0.09 sec)
mysql> insert into B(b) values(7);
Query OK, 1 row affected (0.07 sec)
mysql> select * from A;
+---+
| a |
+---+
| 1 |
| 2 |
| 3 |
| 4 |
+---+
4 rows in set (0.00 sec)
mysql> select * from B;
+---+
| b |
+---+
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+---+
5 rows in set (0.00 sec)
mysql> select * from A inner join B on A.a=B.b;
+---+---+
| a | b |
+---+---+
| 3 | 3 |
| 4 | 4 |
+---+---+
2 rows in set (0.00 sec)
mysql> select * from A left join B on A.a=B.b;
+---+------+
| a | b |
+---+------+
| 3 | 3 |
| 4 | 4 |
| 1 | NULL |
| 2 | NULL |
+---+------+
4 rows in set (0.00 sec)
mysql> select * from A right join B on A.a=B.b;
+------+---+
| a | b |
+------+---+
| 3 | 3 |
| 4 | 4 |
| NULL | 5 |
| NULL | 6 |
| NULL | 7 |
+------+---+
5 rows in set (0.00 sec)
mysql> select * from A left join B on A.a=B.b union select * from A right join B on A.a=B.b;
+------+------+
| a | b |
+------+------+
| 3 | 3 |
| 4 | 4 |
| 1 | NULL |
| 2 | NULL |
| NULL | 5 |
| NULL | 6 |
| NULL | 7 |
+------+------+
7 rows in set (0.00 sec)
附:
mysql退出三种方法:
mysql > exit;
mysql > quit;
mysql > \q;