2019-03-21
-- 创建表 create TABLE
-- 删除表 drop TABLE
create TABLE test1(
num INT
)
drop table test1;
-- 修改表 ALTER TABLE
-- 1 修改列的长度
create table test1(
id int(5) auto_increment,
name varchar(200) DEFAULT '张三',
PRIMARY KEY(id)
)
alter table test1 modify id int(10)
-- 2 修改列的数据类型(前提:修改列必须为null)
alter table test1 modify id varchar(10)
-- 3 添加删除约束
alter table test1 modify name varchar not null;
CREATE table test3(
id int(5),
name varchar(100)
)
alter table test3 add PRIMARY key(id)
alter table test3 add unique(NAME)
alter table test3 add FOREIGN key(id) REFERENCES test1 (id)
-- 4 列和表的重命名
alter table test3 rename as test33;
alter table test33 change id newid int(9);
-- 5 添加或删除列
alter table test33 add age int(3) default 0 not null;
alter table test33 drop age;
alter table test33 drop column age1,drop column age2;
-- DQL 数据查询语言 -- 查
-- DML 数据操作语言 -- 增删改
-- DDL 数据定义语言 -- create alter drop ...
-- 插入 insert into 表名(列名列表) values(值列表)
-- 修改 update 表名 set 哪些列 [where 修改哪些行]
-- 将student1表中所有人age修改为30
update student1
set age = 30;
-- 将student1表中的身高1.6以上的人age修改为20
update student1
set age = 20
where height >1.6
-- 将student1表中名字是华安的人age修改为25
update student1
set age = 25
where stuname = '华安'
-- 将student1表中2000-1-1以后出生的人年龄涨一岁
update student1
set age = age+1
where birthday >'2000-1-1'
-- 将student1表中id9529以上的学员 性别改成男 年龄涨两岁
update student1
set gender = '男' ,age = age + 2
where id > 9529
-- 删除
-- delete from 表名 [where 哪些行]
delete from student2
where age>10
-- 删除student2表gender列的数据
update student2
set gender = null
-- and or 和 或
-- 将student2表
-- 1.向部门表新增一个部门,部门编号为50,部门名称为HR,工作地点为SY。
-- 2.向部门表新增一个部门,部门编号为60,部门名称为MARKET。
-- 3.向员工表中新增一个员工,员工编号为8888,姓名为BOB,岗位为CLERK,
-- 经理为号7788,入职日期为1985-03-03,薪资3000,奖金和部门为空。
-- 4.把员工编号为7782的部门编号修改为20
-- 5.把部门编号为10的员工 部门编号调整为20 工资增加100
-- 6.修改奖金为null的员工 奖金设置为0
-- 7.修改奖金为0的员工 奖金设置为null
-- 8.将奖金部位null的涨100
-- 9。删除经理编号为7566的员工记录
insert into dept1 VALUES(50,'HR','SY');
insert into dept1(deptno,dname) VALUES(60,'market');
INSERT INTO EMP1 VALUES(8888,'BOB','CLERK',7788,'1985-03-03',3000.00,NULL,NULL);
UPDATE EMP1
SET DEPTNO = 20
WHERE EMPNO = 7782;
UPDATE EMP1
SET SAL = SAL+100 ,DEPTNO = 20
WHERE DEPTNO = 10;
UPDATE EMP1
SET comm = 0.00
WHERE comm is NULL
update emp1
set comm = NULL
where comm = 0
update emp1
set comm = comm +100
where comm is not null
delete from emp1
wheremgr = 7566
-- 事务 一组增删改(经过提交或回滚的一组增删改)
-- 事物的四大特性(ACID):
-- A 原子性 组成事务的增删改要么全部成功 要么全部失败
-- C 一致性 一旦事务结束(提交或回滚过),数据保持一致状态
-- I 隔离性 事务之间互不影响
-- D 持久性 事务一旦提交 不能回滚
-- navicat默认是将所有增删改一次一提交
-- 取消自动提交
set autocommit = true
create table test4(
id int(9) auto_increment,
name varchar(200),
PRIMARY KEY(id)
)
insert into test4(name) VALUES ('张三');
insert into test4(name) VALUES ('李四');
insert into test4(name) VALUES ('王五');
commit -- 提交 等同于保存
SELECT * from test4
ROLLBACK -- 撤销
-- DDL 自动以commit的方式结束事务 turncate
-- truncate 截断表 但因为是ddl 不能回滚数据
truncate table test4
delete from test4
-- 锁
-- 存档点和回档
create table test5(
num int(5)
)
insert into test5 value(1);
insert into test5 value(2);
insert into test5 value(3);
SAVEPOINT s1;
insert into test5 value(4);
insert into test5 value(5);
insert into test5 value(6);
ROLLBACK to s1;
select * from test5
/*基于上述学生表和班级表,完成如下问题
(1)添加三个班级信息为:1,JAVA1班,null
2,JAVA2班,null
3,JAVA3班,null
(2)添加学生信息如下:‘A001’,‘张三’,‘男’,‘01-5月-05’,100,1
(3)添加学生信息如下:'A002','MIKE','男','1905-05-06',10
(4)插入部分学生信息: 'A003','JOHN','女’
(5)将A001学生性别修改为'女‘
(6)将A001学生信息修改如下:性别为男,生日设置为1980-04-01
(7)将生日为空的学生班级修改为Java3班
(8)请使用一条SQL语句,使用子查询,更新班级表中每个班级的人数字段
*/
create table student ( -- 学生表
xh char(4),-- 学号
xm varchar(10),-- 姓名
sex char(2),-- 性别
birthday date,-- 出生日期
sal double(7,2), -- 奖学金
studentcid int(2) -- 学生班级号
)
Create table class( -- 班级表
classid int(2), -- 班级编号
cname varchar(20),-- 班级名称
ccount int(3) -- 班级人数
)
INSERT into class(classid,cname) VALUES (1,'JAVA1班');
INSERT into class(classid,cname) VALUES (2,'JAVA2班');
INSERT into class(classid,cname) VALUES (3,'JAVA3班');
INSERT into student VALUES ('A001','张三','男','2001-05-05',100,1);
INSERT into student VALUES ('A002','MIKE','男','1905-05-06',10,null);
INSERT into student(xh,xm,sex) VALUES ('A003','JOHN','女');
update student
set sex = '女'
where xh = 'A001'
update student
set sex = '男',birthday = '1980-04-01'
where xh = 'A001'
UPDATE student
SET studentcid = (select classid from class where cname='JAVA3班')
WHERE birthday is NULL
-- 简单查询
-- 1.查什么 2.从哪查
-- 可以查列 表达式
-- 查询emp和dept表中所有的数据
select deptno,dname,loc
FROM dept2
SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno
FROM emp2
select * -- 所有列
from emp2
-- IFNULL 若第一个参数不为null 返回第一个参数 否则返回第二个参数(三元运算符)
select comm,ifnull(comm,0)
from emp2
-- 表达式 和函数 一定 要起别名
-- 列别名
-- 1.列名 列别名
-- 2.列名 as 列别名
-- 注意 别名中有空格
-- 特殊字符 需要加双引号
SELECT sal as 工资,comm,sal*6+sal*6*1.2+12*ifnull(comm,0) 年薪
from emp2
-- DISTINCT 去重(复)
-- 注意 不显示重复数据
select DISTINCT deptno
from emp2
select DISTINCT job,deptno
from emp2