-- drop table t_stu;
-- unique 唯一約束的列允許為null
-- 如果在之后的操作中。需要某列必須為key,才能做一些操作的情況下。也可以使用唯一約束代替主鍵約束
-- create table t_stu(
-- ??studId int,
-- ??tel varchar(11) unique,
-- ??sex varchar(1),
-- ??addr varchar(100),
-- ??idCard varchar(18),
-- ??createTime datetime default now(),
-- ??unique(studId,createTime)
-- );
-- drop table t_stu;
-- create table t_stu(
-- ??studId int auto_increment unique,
-- ??tel varchar(11) unique,
-- ??sex varchar(1),
-- ??addr varchar(100),
-- ??idCard varchar(18),
-- ??createTime datetime default now()
-- );
-- insert into t_stu(studId,tel,sex,addr,idCard) values(1,"13333333333",'男','北京',"133233190001012233");
-- insert into t_stu(studId,tel,sex,addr,idCard) values(3,null,'男','石家莊',"133233190001012234");
-- alter table t_stu add constraint `uk_idcard` unique(idCard);
-- 查看建表語句
-- show create table 表名
-- drop table t_stu;
-- create table t_stu(
-- ??studId int auto_increment unique,
-- ??tel varchar(11) check(length(tel) = 11),
-- ??sex varchar(1),
-- ??addr varchar(100),
-- ??idCard varchar(18),
-- ??createTime datetime default now(),
-- ??age int check(age > 15),
-- ??constraint `ck_idCard` check(length(idCard) = 18 or length(idCard)= 15)
-- );
-- insert into t_stu(studId,tel,sex,addr,idCard) values(1,"13333333333",'男','北京',"133233190001012233");
-- alter table t_stu add constraint `ck_sex` check(sex = '男' or sex = '女');
-- insert into t_stu(studId,tel,sex,addr,idCard) values(2,"13333333333",'女','北京',"133233190001012233");
-- insert into t_stu(studId,tel,sex,addr,idCard) values(2,"13333333333",'女','北京',"133233190001012233");
-- insert into t_stu(studId,tel,sex,addr,idCard) values(3,"13333333333",'女','北京',"133233190001012");
--
-- insert into t_stu(studId,tel,sex,addr,idCard,age) values(3,"13333333333",'女','北京',"133233190001012",16);
-- 學生表
create table t_student(
??id int auto_increment primary key COMMENT "id",
??studId varchar(30) unique comment "學號",
??studName varchar(30) comment "學生姓名",
??studSex varchar(2),
??tel varchar(11),
??createTime datetime default now()
);
create table t_course(
??courseId int auto_increment primary key,
??courseName varchar(30),
??createTime datetime default now()
);
-- 學生選課表
drop table t_curricula;
/* 被參考的列必須有主鍵或者唯一約束
??create table tableName(
????studId int,
????[constraint `約束名稱`] foreign key(studId) references 主表名(列名)
??)
*/
-- drop table t_curricula;
-- create table t_curricula(
-- ??id int auto_increment primary key,
-- ??studId int,
-- ??courseId int,
-- ??constraint `fk_studId_t_student_id` foreign key(studId) references t_student(id),
-- ??FOREIGN key(courseId) references t_course(courseId)
-- );
-- alter table t_curricula add constraint `fk_courseId_t_course_courseId` foreign key(courseId) references t_course(courseId)
-- 刪除主表數據。 ?先刪除從表,再刪除主表
delete from t_curricula where courseId = 8;
delete from t_course where courseId = 8
-- 根據同一個條件,同時刪除多張表數據。
delete t_curricula,t_course from t_curricula,t_course where t_course.courseId = 1;
-- update t_course set courseId = 8 where courseId = 3;
-- update t_curricula set courseId = 8 where courseId = 3;
-- update t_course,t_curricula set t_course.courseId = 8,t_curricula.courseId = 8 where t_course.courseId = 3
drop table t_curricula;
create table t_curricula(
??id int auto_increment primary key,
??studId int,
??courseId int,
??constraint `fk_studId_t_student_id` foreign key(studId) references t_student(id),
??FOREIGN key(courseId) references t_course(courseId) on update cascade on delete set null
);
update t_course set courseId = 8 where courseId = 3;
delete from t_course where courseId = 8
create table t_del_constraint(
??id int auto_increment primary key,
??`name` ?varchar(30) unique,
??idcard varchar(18) not null,
??addr varchar(30),
??createtime datetime default now(),
??constraint `uk_idcard` unique(idcard),
??check(length(idcard) = 18),
??FOREIGN key(addr) references t_stu(addr)
)
CREATE TABLE `t_del_constraint` (
??`id` int NOT NULL AUTO_INCREMENT,
??`name` varchar(30) DEFAULT NULL,
??`idcard` varchar(18) NOT NULL,
??`addr` varchar(30) DEFAULT NULL,
??`createtime` datetime DEFAULT CURRENT_TIMESTAMP,
??PRIMARY KEY (`id`),
??UNIQUE KEY `uk_idcard` (`idcard`),
??UNIQUE KEY `name` (`name`),
??KEY `addr` (`addr`),
??CONSTRAINT `t_del_constraint_ibfk_1` FOREIGN KEY (`addr`) REFERENCES `t_stu` (`addr`),
??CONSTRAINT `t_del_constraint_chk_1` CHECK ((length(`idcard`) = 18))
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- 刪除外鍵
alter table t_del_constraint drop foreign key `t_del_constraint_ibfk_1`
-- 刪除check
alter table t_del_constraint drop check `t_del_constraint_chk_1`
-- 刪除唯一約束
alter table t_del_constraint drop index `uk_idcard`;
alter table t_del_constraint drop index `name`
-- 刪除主鍵
alter table t_del_constraint drop primary key
-- 非空約束和默認值約束都可以使用修改列的數據類型的方式進行刪除
alter table t_del_constraint modify id int;
alter table t_del_constraint modify idcard varchar(18);
alter table t_del_constraint modify createTime datetime;
-- 數據庫備份
-- 備份結構 ?mysqldump -u -p -d dbName > 路徑/文件名.sql
-- 備份數據 ?mysqldump -u -p -t dbName > 路徑/文件名.sql
-- 備份數據和結構 ?mysqldump -u -p dbName > 路徑/文件名.sql
-- 執行外部的sql命令(txt) 進入mysql之后 通過source命令 ?source 文件名
-- 查詢結果和表中數據沒有聯系,只是以表的形式進行了展示
-- 查詢部門信息
select * from dept;
-- 查詢語句 ?
-- select *(列名) from 表名 [where 查詢條件][分頁][排序][分組][聚合]
-- * 是所有列
select * from emp;
-- 查詢員工姓名,員工工資和獎金,每行數據的唯一標識
select empno,ename,sal,comm from emp;
-- 從dept中檢索部門編號 部門名稱
select deptno,dname from dept;
-- 排序默認是根據主鍵升序排序
select * from emp;
-- 查詢員工姓名
select ename from emp;
-- order by 子句 ?desc降序 asc升序
-- 查詢員工姓名,員工工資根據員工工資升序排序
select ename,sal from emp order by sal desc;
-- 查詢員工信息根據入職日期進行降序排序
select * from emp order by hiredate desc;
-- 查詢員工姓名,根據員工姓名升序排序
select ename from emp order by ename;
-- 查詢員工姓名,員工工資+獎金 根據員工資和獎金的和進行降序排序
-- ifnull(列名,值)
select ename,sal+ifnull(comm,0) from emp order by sal+ifnull(comm,0) desc;
-- 排序列可以為多列,如果根據單列排序,那么如果排序列兩個或多行數據相同,那么相同的行則以默認方式排序
-- 查詢員工姓名和員工工資并以員工工資降序,員工姓名升序排序
select ename,sal from emp order by sal desc,ename;
-- 查詢員工姓名,員工崗位,員工工資,獎金(如果獎金為null,則設置'沒獎金')
select ename,job,sal,ifnull(comm,0) from emp;
-- 查詢員工姓名,崗位,工資,獎金(如果獎金不為null,為有獎金,否則為無獎金)
select ename,job,sal,
case
when comm is not null then comm
else "沒獎金"
end COMM
from emp;
-- 查詢員工姓名,員工崗位,入職時間,如果入職日期在6月份之前,則為上半年入職,否則為下半年
select ename,job,
case
when month(hiredate) <= 6 then "上半年"
else "下半年"
end HIREDATE
from emp;
-- 待支付 待發貨 ?待收貨 ?待評價 已完成 ?0 1 2 3 4
-- 微信支付 支付寶支付 ?銀聯支付 惠支付 ?1 2 3 4
-- limit 子句 查詢指定條數 ?limit的位置從0開始
-- 查詢員工編號,員工姓名,員工崗位,查詢前5條
select empno,ename,job from emp limit 5
-- limit num1,num2 ??開始位置(頁碼-1) ?* 每頁顯示條數,顯示條數
select empno,ename,job from emp limit 0,5;
select empno,ename,job from emp limit 5,5;
select empno,ename,job from emp limit 10,5;
-- limit子句 在整個查詢語句的最后
-- 查詢員工姓名,工資,部門編號 根據工資降序排序查詢前5條
select ename,sal,deptno
from emp
order by sal desc
limit 5,5
-- where 子句 待條件的查詢
-- 查詢10號部門的員工信息....
select * from emp where 1 != 1;
select * from emp where deptno = 10;
-- 在emp表中查詢工資高于3000的員工的員工編號、員工姓名和工資信息:
select empno,ename,sal from emp where sal > 3000;
-- 總工資高于3000的員工編號,員工姓名和總工資信息
select empno,ename,sal + ifnull(comm,0) from emp
where sal + ifnull(comm,0) > 2000
-- 在emp表中查詢工資高于1600,并且部門是20部門的員工的部門編號、員工編號和員工姓名信息:
select deptno,empno,ename from emp where sal > 1600 and deptno = 20;
-- 在emp表中查詢工資高于1600低于5000,并且部門是20部門的員工的部門編號、員工編號和員工姓名信息:
select deptno,empno,ename,sal from emp where sal > 1600 and sal < 3000 and deptno ?= 20
-- 在emp表中查詢10或20部門的員工的部門編號、員工編號和員工姓名信息,并按部門編號升序排列:
select deptno,empno,ename from emp
where deptno = 20 or deptno = 10
order by deptno
limit 2
-- 范圍測試(between and) ?查詢員工編號,員工姓名,員工工資 要求工資大于等于1600 小于等于3000
select empno,ename,sal from emp where sal >= 1600 and sal <= 3000;
select empno,ename,sal from emp where sal between 1600 and 3000;
-- 組成員測試 ?in ?查詢10號或20號或者30號部門的員工信息
select * from emp where deptno = 10 or deptno = 20 or deptno = 30
order by deptno desc;
select * from emp where deptno in(10,20,30)
select * from emp where deptno not in(10,20,30);
-- like _ ?%
-- 查詢員工信息,名字當中包含A的
select * from emp where ename like 'A%';
-- 查詢員工信息,名字中包含A但是A不是第一個
select * from emp where ename like '%A%' and ename not like 'A%';
-- 查詢沒有獎金的員工信息
select * from emp where comm is null;
-- 查詢有獎金的
select * from emp where comm is not null and comm != 0;
-- 查詢員工姓名,員工崗位,員工的總工資
select ename,job,sal + ifnull(comm,0) as sal
from emp
select ename,job,sal,
case
when comm is null then '沒獎金'
else comm
end comm
from emp
select ename "員工姓名",sal "員工工資" from emp;
-- 聚合函數
-- 求和 求平均數 ?最小 ?最大 ?條數
-- 員工的工資總和
select sum(sal) total from emp;
-- 員工的總工資(獎金和工資)
select sum(sal + ifnull(comm,0)) from emp
-- 查詢10號部門的員工的總工資
select sum(sal + ifnull(comm,0)) from emp where deptno = 10
select avg(sal) from emp where deptno = 10;
-- 查詢10號部門的最大工資
select max(sal) from emp where deptno = 10;
select min(sal) from emp where deptno = 10;
-- 查詢的員工數量
select count(*) from emp;
-- 查詢有獎金的人數 ?count數量 不包含指定列為空的行
select count(comm) from emp where comm != 0;
-- 分組查詢 group by 子句 ?
-- 分組查詢時 select 查詢的列只能包含三種
-- 1、聚合函數 ?2、分組依據 ?3、常量
-- 各個 ?查詢各個部門的最大工資
?select max(sal),deptno from emp group by deptno
?select deptno,min(sal),max(sal),sum(sal) from emp group by deptno
-- 查詢各個部門的最大工資及這個人的員工的姓名 -- 不提倡
select max(sal),ename from emp group by deptno;
-- select ename,sal from emp where sal in (select max(sal) from emp group by deptno)
-- group by 可以存在多個分組一句
-- 查詢各個部門中,各個崗位的最高工資
select max(sal),deptno,job from emp group by deptno,job
-- 各個年份入職的人數
select count(*),year(hiredate),"該死的查詢" from emp group by year(hiredate)
-- having 條件檢索 和where很像,但是執行時機不同
-- 查詢員工姓名,員工的總工資,總工資高于2650
-- select ename,sal + ifnull(comm,0) total from emp having total >= 2650;
-- 查詢10號部門的員工信息
select * from emp where deptno = 10 having sal > 1300;
-- having 常用于分組查詢后的篩選
-- 各個部門的最大工資,部門編號。要求最大工資大于3000
select max(sal),deptno from emp where sal > 3000 group by deptno
select max(sal) max,deptno from emp group by deptno having max > 3000;
-- distinct 去重
-- 查詢有多少個工種
select count(distinct job) from emp
-- 查詢各個部門的工資總和(獎金不為空),部門編號,總和大于9000 并且根據總工資進行倒序排序,查看前兩條例
select sum(sal + comm) total,deptno
from emp
where comm is not null
group by deptno
order by total desc
limit 2;