day22(Alex python)

版权声明:本文为博主原创文章,转载请注明作者和出处。https://blog.csdn.net/xq920831/article/details/82810057

开始今天的内容。

mysql常用命令

参考博客:https://www.cnblogs.com/alex3714/articles/5950372.html

MySQL 创建数据表

语法

1

CREATE TABLE table_name (column_name column_type);

创建一个student表

1

2

3

4

5

6

7

create table student(

   stu_id INT NOT NULL AUTO_INCREMENT,

   name CHAR(32) NOT NULL,

   age  INT NOT NULL,

   register_date DATE,

   PRIMARY KEY ( stu_id )

);

实例解析:

  • 如果你不想字段为 NULL 可以设置字段的属性为 NOT NULL, 在操作数据库时如果输入该字段的数据为NULL ,就会报错。
  • AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1。
  • PRIMARY KEY关键字用于定义列为主键。 您可以使用多列来定义主键,列间以逗号分隔。

MySQL 插入数据

语法

1

2

3

INSERT INTO table_name ( field1, field2,...fieldN )

                       VALUES

                       ( value1, value2,...valueN );

插入数据

1

2

3

4

5

6

7

8

9

10

11

mysql> insert into student (name,age,register_date) values ("alex li",22,"2016-03-4"

    -> ;

Query OK, 1 row affected (0.00 sec)

 

mysql> select from student;

+--------+---------+-----+---------------+

| stu_id | name    | age | register_date |

+--------+---------+-----+---------------+

|      1 | alex li |  22 | 2016-03-04    |

+--------+---------+-----+---------------+

1 row in set (0.00 sec)

MySQL 查询数据

语法

1

2

3

4

SELECT column_name,column_name

FROM table_name

[WHERE Clause]

[OFFSET M ][LIMIT N]

  • 查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用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> select from student limit 3 offset 2;

+--------+---------+-----+---------------+

| stu_id | name    | age | register_date |

+--------+---------+-----+---------------+

|      3 | alex li |  24 | 2016-03-04    |

|      4 | alex li |  24 | 2016-03-01    |

|      5 | alex li |  24 | 2016-03-02    |

+--------+---------+-----+---------------+

rows in set (0.00 sec)

比如这个SQL ,limit后面跟的是3条数据,offset后面是从第3条开始读取

 

mysql> select from student limit 3 ,1;

+--------+---------+-----+---------------+

| stu_id | name    | age | register_date |

+--------+---------+-----+---------------+

|      4 | alex li |  24 | 2016-03-01    |

+--------+---------+-----+---------------+

1 row in set (0.00 sec)

 

而这个SQL,limit后面是从第3条开始读,读取1条信息。

  

MySQL where 子句

语法

1

2

SELECT field1, field2,...fieldN FROM table_name1, table_name2...

[WHERE condition1 [AND [OR]] condition2.....

以下为操作符列表,可用于 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

select from student where register_date > '2016-03-04';

  

MySQL UPDATE 查询

语法

1

2

UPDATE table_name SET field1=new-value1, field2=new-value2

[WHERE Clause]

1

update student set age=22 ,name="Alex Li" where stu_id>3;

MySQL DELETE 语句

语法

1

DELETE FROM table_name [WHERE Clause]<br><br>delete from student where stu_id=5; 

MySQL LIKE 子句

语法

1

2

3

4

5

6

SELECT field1, field2,...fieldN table_name1, table_name2...

WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'

 

 

select *from student where name binary like "%Li";

select *from student where name binary like  binary "%Li"; #只匹配大写

MySQL 排序

1

2

3

4

SELECT field1, field2,...fieldN table_name1, table_name2...

ORDER BY field1, [field2...] [ASC [DESC]]

使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列。 默认情况下,它是按升序排列。

select *from student where name like binary "%Li" order by stu_id desc;

MySQL GROUP BY 语句  

1

2

3

4

SELECT column_name, function(column_name)

FROM table_name

WHERE column_name operator value

GROUP BY column_name;

示例

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

mysql> SELECT FROM employee_tbl;

+----+--------+---------------------+--------+

| id | name   date                | singin |

+----+--------+---------------------+--------+

|  1 | 小明 | 2016-04-22 15:25:33 |      1 |

|  2 | 小王 | 2016-04-20 15:25:47 |      3 |

|  3 | 小丽 | 2016-04-19 15:26:02 |      2 |

|  4 | 小王 | 2016-04-07 15:26:14 |      4 |

|  5 | 小明 | 2016-04-11 15:26:40 |      4 |

|  6 | 小明 | 2016-04-04 15:26:54 |      2 |

+----+--------+---------------------+--------+

 

接下来我们使用 GROUP BY 语句 将数据表按名字进行分组,并统计每个人有多少条记录:

mysql> SELECT nameCOUNT(*) FROM   employee_tbl GROUP BY name;

+--------+----------+

name   COUNT(*) |

+--------+----------+

| 小丽 |        1 |

| 小明 |        3 |

| 小王 |        2 |

+--------+----------+

rows in set (0.01 sec)

 

使用 WITH ROLLUP

mysql> SELECT nameSUM(singin) as singin_count FROM  employee_tbl GROUP BY name WITH ROLLUP;

+--------+--------------+

name   | singin_count |

+--------+--------------+

| 小丽 |            2 |

| 小明 |            7 |

| 小王 |            7 |

NULL   |           16 |

+--------+--------------+

rows in set (0.00 sec)

其中记录 NULL 表示所有人的登录次数。<br>

我们可以使用 coalesce 来设置一个可以取代 NUll 的名称,coalesce 语法:

mysql> SELECT coalesce(name'总数'), SUM(singin) as singin_count FROM  employee_tbl GROUP BY name WITH ROLLUP;

 

下面是针对上述内容,我在自己电脑上的实际操作。

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 table student drop register_date; #从student表删除register_date   字段alter table student add phone int(11) not null; #添加phone字段

修改字段类型及名称

如果需要修改字段类型及名称, 你可以在ALTER命令中使用 MODIFY 或 CHANGE 子句 。

例如,把字段 c 的类型从 CHAR(1) 改为 CHAR(10),可以执行以下命令:

1

mysql> ALTER TABLE testalter_tbl MODIFY CHAR(10);

使用 CHANGE 子句, 语法有很大的不同。 在 CHANGE 关键字之后,紧跟着的是你要修改的字段名,然后指定新字段名及类型。尝试如下实例:

1

2

3

mysql> ALTER TABLE testalter_tbl CHANGE i j BIGINT;

 

mysql> ALTER TABLE testalter_tbl CHANGE j j INT;

ALTER TABLE 对 Null 值和默认值的影响

当你修改字段时,你可以指定是否包含只或者是否设置默认值。

以下实例,指定字段 j 为 NOT NULL 且默认值为100 。

1

2

mysql> ALTER TABLE testalter_tbl 

    -> MODIFY BIGINT NOT NULL DEFAULT 100;

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 testalter_tbl RENAME TO alter_tbl;

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

mysql> create table class(

    -> id  int not null primary key,

    -> name char(16));

Query OK, 0 rows affected (0.02 sec)

 

 

CREATE TABLE `student2` (

  `id` int(11) NOT NULL,

  `namechar(16) NOT NULL,

  `class_id` int(11) NOT NULL,

  PRIMARY KEY (`id`),

  KEY `fk_class_key` (`class_id`),

  CONSTRAINT `fk_class_key` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

此时如果class 表中不存在id 1,student表也插入不了,这就叫外键约束

mysql> insert into student2(id,name,class_id) values(1,'alex', 1);

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`student2`, CONSTRAINT `fk_class_key` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`))

 

 

 

mysql> insert into class(id,namevalues(1,"linux");

Query OK, 1 row affected (0.01 sec)

 

mysql> insert into student2(id,name,class_id) values(1,'alex', 1);

Query OK, 1 row affected (0.00 sec)

 

 

#如果有student表中跟这个class表有关联的数据,你是不能删除class表中与其关联的纪录的

mysql> delete from class where id =1;

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`testdb`.`student2`, CONSTRAINT `fk_class_key` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`))

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

A    B

-    -

1    3

2    4

3    5

4    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

select from INNER JOIN on a.a = b.b;

select a.*,b.*  from a,b where a.a = b.b;

 

a | b

--+--

3 | 3

4 | 4

其实就是只显示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

select from LEFT JOIN on a.a = b.b;

 

a |  b

--+-----

1 | null

2 | null

3 |    3

4 |    4

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

select from RIGHT JOIN on a.a = b.b;

 

a    |  b

-----+----

3    |  3

4    |  4

null |  5

null |  6

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

select from FULL JOIN on a.a = b.b;

 

 a   |  b

-----+-----

   1 | null

   2 | null

   3 |    3

   4 |    4

null |    6

null |    5

mysql 并不直接支持full join,but 总是难不到我们

1

2

3

4

5

6

7

8

9

10

11

12

select from left join on a.a = b.b UNION select from right join on a.a = b.b;

+------+------+

| a    | b    |

+------+------+

|    3 |    3 |

|    4 |    4 |

|    1 | NULL |

|    2 | NULL |

NULL |    5 |

NULL |    6 |

+------+------+

rows in set (0.00 sec)

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;

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值