root@deutschball-virtual-machine:/home/deutschball/桌 面# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 31 Server version: 8.0.27-0ubuntu0.20.04.1 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> CREATE TABLE fleets( -> id INT NOT NULL AUTO_INCREMENT, -> commander VARCHAR(100) NOT NULL, -> PRIMARY KEY(id) -> )ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected, 1 warning (0.02 sec) mysql> insert into fleets(id)values(2); ERROR 1364 (HY000): Field 'commander' doesn't have a default value
+----+-----------+ | id | commander | +----+-----------+ | 2 | soldier1 | | 4 | soldier10 | | 6 | vader | | 8 | vader | | 10 | vader | | 12 | vader | +----+-----------+ 6 rows in set (0.00 sec)
where中使用and指定多个条件
1 2 3 4 5 6 7 8 9
mysql> select *from fleets where id%2=0 and id%3=0; +----+-----------+ | id | commander | +----+-----------+ | 6 | vader | | 12 | vader | +----+-----------+ 2 rows in set (0.00 sec)
mysql> select *from fleets; +----+-----------+ | id | commander | +----+-----------+ | 1 | vader | | 2 | soldier1 | | 3 | soldier2 | | 4 | soldier10 | | 5 | vader | | 6 | vader | | 7 | vader | | 8 | vader | | 9 | vader | | 10 | vader | | 11 | vader | | 12 | vader | | 13 | Vader | +----+-----------+ 13 rows in set (0.00 sec)
使用binary关键字:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
mysql> select *from fleets where binary commander='vader'; +----+-----------+ | id | commander | +----+-----------+ | 1 | vader | | 5 | vader | | 6 | vader | | 7 | vader | | 8 | vader | | 9 | vader | | 10 | vader | | 11 | vader | | 12 | vader | +----+-----------+ 9 rows in set, 1 warning (0.00 sec)
如果不用binary:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
mysql> select *from fleets where commander='vader'; +----+-----------+ | id | commander | +----+-----------+ | 1 | vader | | 5 | vader | | 6 | vader | | 7 | vader | | 8 | vader | | 9 | vader | | 10 | vader | | 11 | vader | | 12 | vader | | 13 | Vader | +----+-----------+ 10 rows in set (0.00 sec)
数据表更新记录
1 2
UPDATE table_name SET field1=new-value1, field2=new-value2,... [WHERE Clause]
mysql> select * from officers; +----+----------------+------------+ | id | name | title | +----+----------------+------------+ | 1 | Darth Vader | executor | | 2 | Darth Sidious | emperor | | 3 | Wilhuff Tarkin | Grand Moff | +----+----------------+------------+ 3 rows in set (0.00 sec)
mysql> select *from commanders; +----+---------------+---------+ | id | name | title | +----+---------------+---------+ | 1 | Rex | captain | | 2 | Darth Vader | general | | 3 | Darth Sidious | marshal | +----+---------------+---------+ 3 rows in set (0.00 sec)
1.现在统计所有政府官员和军队指挥官一共有多少人(考虑有些人可以集军政大权于一身,需要去重)
1 2 3 4 5 6 7 8 9 10 11 12 13
mysql> select name from officers -> union -> select name from commanders; +----------------+ | name | +----------------+ | Darth Vader | | Darth Sidious | | Wilhuff Tarkin | | Rex | +----------------+ 4 rows in set (0.01 sec)
2.统计名叫Darth Vader的是否身兼数职
1 2 3 4 5 6 7 8 9 10 11
mysql> select *from officers where name='Darth Vader' -> union all -> select *from commanders where name='Darth Vader'; +----+-------------+----------+ | id | name | title | +----+-------------+----------+ | 1 | Darth Vader | executor | | 2 | Darth Vader | general | +----+-------------+----------+ 2 rows in set (0.00 sec)
3.统计==肉眼可见的西斯==担任的职务:
肉眼可见的西斯即Darth开头
1 2 3 4 5 6 7 8 9 10 11 12 13
mysql> select * from officers where name like 'Darth%' -> union all -> select *from commanders where name like 'Darth%'; +----+---------------+----------+ | id | name | title | +----+---------------+----------+ | 1 | Darth Vader | executor | | 2 | Darth Sidious | emperor | | 2 | Darth Vader | general | | 3 | Darth Sidious | marshal | +----+---------------+----------+ 4 rows in set (0.00 sec)
排序
1 2
SELECT field1, field2,...fieldN FROM table_name1, table_name2... ORDER BY field1 [ASC [DESC][默认 ASC]], [field2...] [ASC [DESC][默认 ASC]]
order
by语句中从前到后为关键字优先级,首先按照field1关键字的规则进行排序,然后field1关键字相同项再按照field2关键字进行排序,以此类推
ASC(ascend)升序,默认模式
DESC(descent)降序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
mysql> select *from commanders order by id ASC; +----+---------------+---------+ | id | name | title | +----+---------------+---------+ | 1 | Rex | captain | | 2 | Darth Vader | general | | 3 | Darth Sidious | marshal | +----+---------------+---------+ 3 rows in set (0.00 sec)
mysql> select *from commanders order by id DESC; +----+---------------+---------+ | id | name | title | +----+---------------+---------+ | 3 | Darth Sidious | marshal | | 2 | Darth Vader | general | | 1 | Rex | captain | +----+---------------+---------+ 3 rows in set (0.00 sec)
分组
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
mysql> select * from -> popularity; +----+------+--------+ | id | name | gender | +----+------+--------+ | 1 | Tom | male | | 2 | Tom | male | | 3 | Tom | male | | 4 | Jack | male | | 5 | Jon | famal | | 6 | Mike | male | +----+------+--------+ 6 rows in set (0.00 sec)
要调查人口统计表中人重名的情况
1 2 3 4 5 6 7 8 9 10 11
mysql> select name,count(*) from popularity group by name; +------+----------+ | name | count(*) | +------+----------+ | Tom | 3 | | Jack | 1 | | Jon | 1 | | Mike | 1 | +------+----------+ 4 rows in set (0.01 sec)
WITH ROLLUP
现有彩票获奖名单如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
mysql> select * from Winners; +----+------+-------+ | id | name | prize | +----+------+-------+ | 1 | Mike | 20 | | 2 | Mike | 30 | | 3 | Mike | 10 | | 4 | Mike | 15 | | 5 | Jack | 15 | | 6 | Jack | 18 | | 7 | John | 18 | | 8 | Jack | 19 | | 9 | Mike | 5 | +----+------+-------+ 9 rows in set (0.00 sec)
1.查看每个人一共获奖多少次
1 2 3 4 5 6 7 8 9
mysql> select name,count(*) from Winners group by name; +------+----------+ | name | count(*) | +------+----------+ | Mike | 5 | | Jack | 3 | | John | 1 | +------+----------+ 3 rows in set (0.00 sec)
2.查看每个人一共获奖多少钱
1 2 3 4 5 6 7 8 9 10
mysql> select name,SUM(prize) as tot_prize from Winners group by name with rollup; +------+-----------+ | name | tot_prize | +------+-----------+ | Jack | 52 | | John | 18 | | Mike | 80 | | NULL | 150 | +------+-----------+ 4 rows in set (0.00 sec)
mysql> select coalesce(name,'sum_prize'),SUM(prize) as tot_prize from Winners group by name with rollup; +----------------------------+-----------+ | coalesce(name,'sum_prize') | tot_prize | +----------------------------+-----------+ | Jack | 52 | | John | 18 | | Mike | 80 | | sum_prize | 150 | +----------------------------+-----------+ 4 rows in set (0.00 sec)
mysql> select * from officers; +----+---------+-----------+ | id | name | title | +----+---------+-----------+ | 1 | Mike | Mayor | | 2 | Jack | executor | | 3 | Jackson | candidate | | 4 | John | emperor | | 5 | Tom | minister | +----+---------+-----------+ 5 rows in set (0.00 sec)
mysql> select * from commanders; +----+-------+---------------+ | id | name | military_rank | +----+-------+---------------+ | 1 | Vader | general | | 2 | Rex | captain | | 3 | John | marshal | +----+-------+---------------+ 3 rows in set (0.00 sec)
现在John皇帝不希望军队和政治耦合,希望调查有没有在军政上同时身居要职的大官,
即统计其中身兼数职(比如John既是帝国皇帝又是军队元帅)的人
1 2 3 4 5 6 7
mysql> select a.name,a.title,b.military_rank from officers a inner join commanders b on a.name=b.name; +------+---------+---------------+ | name | title | military_rank | +------+---------+---------------+ | John | emperor | marshal | +------+---------+---------------+ 1 row in set (0.00 sec)
其中
1
mysql> select a.name,a.title,b.military_rank from officers a inner join commanders b on a.name=b.name;
可以翻译为:
保留a表的name和title列,保留b表的military_rank列,
a表即officers表,b表即commanders表,
两表根据name列等值连接
调查完后John皇帝很开心
LEFT JOIN
左合并会保留左侧表的全部数据,不管右侧表有无匹配数据
1 2 3 4 5 6 7 8 9 10 11 12
mysql> select a.name,a.title,b.military_rank from officers a left join commanders b on a.name=b.name; +---------+-----------+---------------+ | name | title | military_rank | +---------+-----------+---------------+ | Mike | Mayor | NULL | | Jack | executor | NULL | | Jackson | candidate | NULL | | John | emperor | marshel | | Tom | minister | NULL | +---------+-----------+---------------+ 5 rows in set (0.00 sec)
可以翻译为:
政府官员先都列在表里,然后军队指挥官如果有人也是政府官员则把其军衔也写在表里,否则写NULL
RIGHT JOIN
类比左合并,保留右侧表的全部数据,不管左侧表有无匹配数据
NULL值处理
null值与任何值的任何比较都是null
1 2 3 4 5 6 7 8 9 10 11 12 13 14
mysql> select * from officers; +----+---------+-----------+ | id | name | title | +----+---------+-----------+ | 1 | Mike | Mayor | | 2 | Jack | executor | | 3 | Jackson | candidate | | 4 | John | emperor | | 5 | Tom | minister | +----+---------+-----------+ 5 rows in set (0.00 sec)
mysql> select * from officers where null=null or null >null or null<null; Empty set (0.00 sec)
判断NULL
IS NULL,IS NOT NULL,<=>三种方法
现有数据表
1 2 3 4 5 6 7 8 9 10
mysql> select * from officers; +----+------+---------+ | id | name | post | +----+------+---------+ | 1 | John | Mayor | | 2 | Mike | NULL | | 3 | Jack | NULL | | 4 | Tom | emperor | +----+------+---------+ 4 rows in set (0.00 sec)
mysql> select * from officers where post is not null; +----+------+---------+ | id | name | post | +----+------+---------+ | 1 | John | Mayor | | 4 | Tom | emperor | +----+------+---------+ 2 rows in set (0.00 sec)
mysql> select * from officers where post is null; +----+------+------+ | id | name | post | +----+------+------+ | 2 | Mike | NULL | | 3 | Jack | NULL | +----+------+------+ 2 rows in set (0.00 sec)
mysql> select * from officers where post <=> null; +----+------+------+ | id | name | post | +----+------+------+ | 2 | Mike | NULL | | 3 | Jack | NULL | +----+------+------+ 2 rows in set (0.00 sec)
替换NULL
1
ifnull(a,b)
如果a为NULL则返回b的值
1 2 3 4 5 6 7 8
mysql> select * from officers where ifnull(post,'')=''; +----+------+------+ | id | name | post | +----+------+------+ | 2 | Mike | NULL | | 3 | Jack | NULL | +----+------+------+ 2 rows in set (0.00 sec)
一般用于int值替换成0参与计算
正则
在where子句中使用正则表达式,类似于等号和LIKE语句:
1
where 键值 REGEXP <pattern>
现有数据表如下:
1 2 3 4 5 6 7 8 9 10 11
mysql> select * from officers; +----+---------------+----------+ | id | name | post | +----+---------------+----------+ | 1 | John | Mayor | | 2 | Mike | NULL | | 3 | Jack | NULL | | 5 | Darth Vader | executor | | 6 | Darth Sidious | emperor | +----+---------------+----------+ 5 rows in set (0.00 sec)
要查询政府官员中肉眼可见的西斯
1 2 3 4 5 6 7 8
mysql> select * from officers where name regexp '^Darth'; +----+---------------+----------+ | id | name | post | +----+---------------+----------+ | 5 | Darth Vader | executor | | 6 | Darth Sidious | emperor | +----+---------------+----------+ 2 rows in set (0.00 sec)
mysql> select * from officers; +----+---------------+----------+ | id | name | post | +----+---------------+----------+ | 1 | John | Mayor | | 5 | Darth Vader | executor | | 6 | Darth Sidious | emperor | +----+---------------+----------+ 3 rows in set (0.00 sec)
mysql> use empire; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed mysql> show columns from officers; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | post | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 3 rows in set (0.02 sec)
mysql> show columns from officers; +---------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | post | varchar(20) | YES | | NULL | | | military_rank | varchar(10) | NO | | soldier | | +---------------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
其中
1
mysql> alter table officers add military_rank varchar(10) not null default 'soldier';
mysql> alter table officers add salary int(10) not null default 3000 after name; Query OK, 0 rows affected, 1 warning (0.04 sec) Records: 0 Duplicates: 0 Warnings: 1
mysql> show columns from officers; +---------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | salary | int | NO | | 3000 | | | post | varchar(20) | YES | | NULL | | | military_rank | varchar(10) | NO | | soldier | | +---------------+-------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
删除字段DROP
1
ALTER TABLE <tablename> DROP <key>;
现在希望删除salary字段
1 2 3 4 5 6 7 8 9 10 11 12 13 14
mysql> alter table officers drop salary; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> show columns from officers; +---------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | post | varchar(20) | YES | | NULL | | | military_rank | varchar(10) | NO | | soldier | | +---------------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
只修改字段属性MODIFY
1 2 3
mysql> alter table officers modify post varchar(10); Query OK, 2 rows affected (0.03 sec) Records: 2 Duplicates: 0 Warnings: 0
修改字段名及属性CHANGE
1
ALTER TABLE <tablename> MODIFY <old key> <new key> <type> ...
现在希望修改post字段为position
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> alter table officers change post position varchar(10); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> show columns from officers; +---------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | position | varchar(10) | YES | | NULL | | | military_rank | varchar(10) | NO | | soldier | | +---------------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
mysql> alter table officers change post position; 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 '' at line 1
修改字段默认值
1
ALTER TABLE <tablename> ALTER <key> SET <属性> <新属性值>
比如希望修改官员的默认职位为'大臣'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> alter table officers alter position set default 'minister'; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> show columns from officers; +---------------+-------------+------+-----+----------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+----------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | position | varchar(10) | YES | | minister | | | military_rank | varchar(10) | NO | | soldier | | +---------------+-------------+------+-----+----------+----------------+ 4 rows in set (0.00 sec)
1.CREATE INDEX <indexname> ON <tablename>(columnname); 2.ALTER TABLE <tablename> ADD INDEX <indexname>(columnname); 3.建表时指定 mysql> create table testIndex( -> id int not null, -> name varchar(20)not null, -> index myindex(name) -> ); Query OK, 0 rows affected (0.02 sec)
删除索引
1
DROP INDEX <indexname> ON <tablename>
1 2 3
mysql> drop index myindex on testIndex; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
唯一索引
建立唯一索引的列不允许有重复数据
建立索引的方法
1 2 3 4 5 6 7 8 9
1.CREATE UNIQUE INDEX <indexname> ON <tablename>(columnname); 2.ALTER TABLEL <tablename> ADD UNIQUE <indexname>(columnname); 3.建表时指定 mysql> create table mytable( -> id int(10)not null, -> name varchar(20)not null, -> unique myindex(name) -> ); Query OK, 0 rows affected, 1 warning (0.01 sec)
1 2 3 4 5 6 7 8 9 10
mysql> create unique index myindex on mytable(id); ERROR 1146 (42S02): Table 'empire.mytable' doesn't exist mysql> create unique index myindex on testIndex(id); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
CREATE TABLE <target_table_name> LIKE <source_table_name>;//克隆表结构 INSERT INTO <target_table_name> SELECT * FROM <source_table_name>;//克隆表数据 CREATE TABLE <target_table_name> SELECT * FROM <source_table_name>;//一步到位 CREATE TABLE <target_table_name> SELECT * FROM <source_table_name> where 1=2;//克隆表结构
mysql> show columns from officers; +---------------+-------------+------+-----+----------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+----------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | position | varchar(10) | YES | MUL | minister | | | military_rank | varchar(10) | NO | MUL | soldier | | +---------------+-------------+------+-----+----------+----------------+ 4 rows in set (0.00 sec)
mysql> select * from officers; +----+---------------+----------+---------------+ | id | name | position | military_rank | +----+---------------+----------+---------------+ | 5 | Darth Vader | executor | soldier | | 6 | Darth Sidious | emperor | soldier | +----+---------------+----------+---------------+ 2 rows in set (0.00 sec)
mysql> show columns from newOfficers; +---------------+-------------+------+-----+----------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+----------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | position | varchar(10) | YES | MUL | minister | | | military_rank | varchar(10) | NO | MUL | soldier | | +---------------+-------------+------+-----+----------+----------------+ 4 rows in set (0.00 sec)
mysql> select * from newOfficers; Empty set (0.00 sec)