--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 表;