--stu_info表
+----+------+------------+-------------+
| id | name | birth | tel |
+----+------+------------+-------------+
| 1 | ztq | 1994-12-29 | 159****0773 |
| 2 | tqz | 1994-11-27 | 159****0773 |
| 3 | fq | 1994-05-23 | 187****8793 |
| 4 | qf | 1994-05-23 | 187****8793 |
+----+------+------------+-------------+
--course_info表
+------+---------+------------+------------+
| id | name | startTime | endTime |
+------+---------+------------+------------+
| 1 | C语言 | 2019-09-01 | 2019-12-31 |
| 2 | C++ | 2019-09-15 | 2019-12-31 |
| 3 | JAVA | 2019-09-15 | 2019-12-15 |
| 4 | 德语 | 2019-09-01 | 2019-12-31 |
| 5 | 日语 | 2019-09-01 | 2019-12-31 |
| 6 | 法语 | 2019-09-01 | 2019-12-31 |
+------+---------+------------+------------+
--stu_course表
+--------+-----------+
| stu_id | course_id |
+--------+-----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 3 | 3 |
| 3 | 5 |
| 4 | 3 |
| 4 | 4 |
+--------+-----------+
-- 创建数据库
create database 数据库名;
-- 删除数据库
-- drop database 数据库名;
-- 使用指定的数据库
use 数据库名;
-- 创建表
mysql> create table if not exists stu_info( -> id int unsigned auto_increment NOT NULL,--自动增加
-> name varchar(50) NOT NULL , -> birth DATE NOT NULL,
-> tel varchar(20) NOT NULL DEFAULT 111,-- 默认值
-> PRIMARY KEY (id)
-> INDEX indexName 字段名,设置索引
-> )ENGINE=InnoDB,auto_increment=1;
-- 创建临时表(TEMPORARY),show tables 不会显示临时表
--临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间
create TEMPORARY table if not exists stu_info
...
-- 删除表
drop table 表名;
-- 删除主键
ALTER 表名 DROP PRIMARY KEY;
--设置主键
--主键只能作用于一个列上,添加主键索引时,你需要确保该主键默认不为空(NOT NULL)
ALTER TABLE 表名 MODIFY 字段名 类型 NOT NULL;
ALTER TABLE 表名 ADD PRIMARY KEY (字段名);
-- 设置自增序列的起始值
ALTER TABLE 表名 AUTO_INCREMENT = 2;
--设置外键
ALTER TABLE stu_course ADD CONSTRAINT fk_id FOREIGN KEY(stu_id) REFERENCES stu_info(id);
alter table stu_course add CONSTRAINT fk_id1 FOREIGN KEY(course_id) REFERENCES course_info(id);
-- 往表中插入数据
insert into 表名
(字段名1,字段名2,...)
values
(字段1值,字段2值,...)
insert into 表名
values
(字段1值,字段2值,...,最后一个字段值)
insert into stu_info
values
(1,'ztq','1994-12-29','159****0773'),
(2,'tqz','1994-11-27','159****0773'),
(3,'fq','1994-05-23','187****8793'),
(4,'qf','1994-05-23','187****8793');
-- 查询
mysql> select now(),curdate(),curtime();
+---------------------+------------+-----------+
| now() | curdate() | curtime() |
+---------------------+------------+-----------+
| 2019-06-26 14:22:39 | 2019-06-26 | 14:22:39 |
+---------------------+------------+-----------+
-- 区间查询
select * from stu_info where id between 2 and 4;
--等价 select * from stu_info where id >= 2 and id <= 4;
-- 出生日期查询
select * from stu_info where YEAR(birth) >= 1994;
-- 年龄查询
mysql> select (YEAR(CURDATE())-YEAR(birth)-(RIGHT(CURDATE(),5) < RIGHT(birth,5))) as age from stu_info;
+------+
| age |
+------+
| 24 |
| 24 |
| 25 |
| 25 |
+------+
-- 查询出现过的人的id
mysql> select distinct(stu_id) from stu_course;
+--------+
| stu_id |
+--------+
| 1 |
| 2 |
| 3 |
| 4 |
+--------+
-- order by排序(默认升序(ASC),降序为DESC)
select * from stu_info order by name asc;
select * from stu_info order by id desc;
-- 不重复人的数量
mysql> select count(distinct(name)) as name_count from info;
-- group by分组
-- 出现的stu_id的次数
mysql> select stu_id,count(*) from stu_course group by stu_id;
+--------+----------+
| stu_id | count(*) |
+--------+----------+
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
+--------+----------+
--WITH ROLLUP 可以实现在分组统计数据基础上再进行相同的统计,coalesce(stu_id,'stu_id'),把NULL替换成stu_id
mysql> select stu_id,count(*),sum(course_id) from stu_course group by stu_id with rollup;
+--------+----------+----------------+
| stu_id | count(*) | sum(course_id) |
+--------+----------+----------------+
| 1 | 2 | 3 |
| 2 | 2 | 3 |
| 3 | 2 | 8 |
| 4 | 2 | 7 |
| NULL | 8 | 21 |
+--------+----------+----------------+
--like 查询 其中'-'匹配一个字符,'%'匹配0-n个字符
mysql> select * from stu_info where name like '%q%'; --名字中带q的
mysql> select * from stu_info where name like '%tq%';--名字找那个带tq的
mysql> select * from stu_info where name like '_tq%';--第2、3个字母是tq的
--union 查询(接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据)(查询字段必须一致)
mysql> select id, name from stu_info where tel like '159%'
-> UNION
-> select id, name from stu_info where name = 'fq';
+----+------+
| id | name |
+----+------+
| 1 | ztq |
| 2 | tqz |
| 3 | fq |
+----+------+
-- 连接查询
mysql> select a.name as stu_name,b.course_id as course_id from stu_info a inner join stu_course b on a.id = b.stu_id;
+----------+-----------+
| stu_name | course_id |
+----------+-----------+
| ztq | 1 |
| ztq | 2 |
| tqz | 1 |
| tqz | 2 |
| fq | 3 |
| fq | 5 |
| qf | 3 |
| qf | 4 |
+----------+-----------+
--设置外键关联后上述查询可以用以下语句实现
select name,course_id from stu_info,stu_course where stu_info.id = stu_course.stu_id;
--多表连接查询
mysql> select stu_info.name,course_info.name from stu_info,stu_course,course_info where stu_info.id = stu_course.stu_id and course_info.id = stu_course.course_id;
+------+---------+
| name | name |
+------+---------+
| ztq | C语言 |
| ztq | C++ |
| tqz | C语言 |
| tqz | C++ |
| fq | JAVA |
| fq | 日语 |
| qf | JAVA |
| qf | 德语 |
+------+---------+
-- 正则表达式
--匹配名字中带q的
select * from stu_info where name regexp 'q';
-- '^字符串a':匹配以a开头的字符串
mysql> select * from stu_info where name regexp '^z';
-- '字符串a$':匹配以a为结尾的字符串
mysql> select * from stu_info where name regexp 'q$';
-- '.':匹配任何非'\n'的字符,要匹配'\n'则表示为'[.\n]'
-- 匹配带q,且q前还有其他字符
mysql> select * from stu_info where name regexp '.q';
-- '[...]':匹配其中的任意 一个 字符,'|' :或,使用 '|' 前或后的表达式
-- 匹配以z或q或f头的字符串
select * from stu_info where name regexp '^[zqf]';--等效下行语句
select * from stu_info where name regexp '^z|^f|^q';
-- '[^...]':匹配其中的不存在的任意字符
-- 匹配不含'ztq'的字符串
mysql> select * from stu_info where name regexp '[^ztq]';
+----+------+------------+-------------+
| id | name | birth | tel |
+----+------+------------+-------------+
| 3 | fq | 1994-05-23 | 187****8793 |
| 4 | qf | 1994-05-23 | 187****8793 |
+----+------+------------+-------------+
-- '*':匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z","zo",以及 "zoo"。* 等价于{0,}。
select * from stu_info where name regexp '^zz*';
--'+':匹配前面的子表达式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。
select * from stu_info where name regexp '^z+';
--'{n}':n 是一个非负整数。匹配确定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。
-- '{n,m}':m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次
-- 匹配至少含有一个z的
mysql> select * from stu_info where name regexp 'z{1,}';
-- update更新
update stu_info set tel = '187****8793' where id = 4;
--delete删除表中数据
delete from stu_info where id = 5;
delete from stu_info;--表中数据全部删除(不删除表)
-- 查询某个数据库中所有的表名
select table_name from information_schema.tables where table_schema = '数据库名'
show tables;
--查看某个数据库的表信息
select * from information_schema.tables where table_name ='表名';
--查看表的字段(等价语句:describe 表名)
show columns from 表名;
--SHOW CREATE TABLE 命令获取创建数据表,包含了原数据表的结构,索引等。
show create table 表名;
-- 查看所有数据库
show databases;
-- 查看当前使用的数据库
select database();
-- 查看数据库使用的端口
show variables like 'port';
SHOW STATUS: 服务器状态
SHOW VARIABLES: 服务器配置变量
--ALTER添加字段(字段会自动添加到数据表字段的末尾)
ALTER TABLE 表名 ADD new字段名 new字段类型;
--如果你需要指定新增字段的位置,可以使用MySQL提供的关键字 FIRST (设定位第一列), after字段名(设定位于某个字段之后)。
ALTER TABLE 表名 ADD new字段名 new字段类型 AFTER 原字段名;
--ALTER删除字段
ALTER TABLE 表名 DROP 字段名;
-- 修改字段类型(modify)
alter table 表名 modify 字段名 新类型;
-- 修改字段类型(change)
-- 修改字段名A 为 B,且类型 改为新类型C
alter table 表名 change 字段A new字段B 新类型C;
--修改字段的默认值
ALTER TABLE 表名 ALTER 字段 SET DEFAULT 默认值;
--表重命名(不建议,有些版本的mysql会丢失数据,新建表,然后复制数据)
alter table 表 rename to 新表名;
--复制表
SHOW CREATE TABLE 表A \G;-- 获取被复制表的结构
CREATE TABLE 表B(结构与表A一致)
INSERT INTO 表B (字段...)
-> SELECT (字段...)
-> FROM 表A ;
insert into 表B select * from 表A;
--索引可以大大提高MySQL的检索速度,同时却会降低更新表的速度如对表进行INSERT、UPDATE和DELETE。因为更新表时,MySQL不仅要保存数据,还要保存一下索引文件(建立索引会占用磁盘空间的索引文件)。
--创建索引时,你需要确保该索引是应用在 SQL 查询语句的条件(一般作为 WHERE 子句的条件)。
--索引也是一张表,该表保存了主键与索引字段,并指向实体表的记录。
--创建索引
mysql> ALTER table 表 ADD INDEX indexName(字);
mysql> create index indexName ON 表(字段);
--删除索引
DROP INDEX indexName ON 表;
--唯一索引
--它与前面的普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。
CREATE UNIQUE INDEX indexName ON 表(字段);
-- 显示索引信息
SHOW INDEX FROM 表;