编程语言:sql - 机构化查询语言
DDL - 数据定义语言 - create / drop / alter
DML - 数据操作语言 - insert / delete /update /select
DCL - 数据控制语言 - grant / revoke
crete databases school default charset utf8; #创建数据库
dorp databases school; #删除数据库
use <数据库名> #切换数据库
show tables; #查看表
–如果表存在删除学生表
drop table if exists tb_student;
–修改表学生表
alter TABLE tb_student add COLUMN stuaddr VARCHAR(255);
alter TABLE tb_student CHANGE COLUMN stuaddr stuaddr VARCHAR(511);
alter TABLE tb_student DROP COLUMN stuaddr VARCHAR(255);
–创建学生表
create table tb_student
(
stuid int not null comment '学号非空约束',
stuname varchar(20) not null comment '姓名',
stusex bit default 1 comment '性别默认1',
stubirth date comment '生日',
primary key (stuid) comment '主键'
);
–向学生表插入数据
insert into tb_student VALUES (1001,'辛会旭',1,'1994-12-15','山东聊城');
insert into tb_student (stuid,stuname) VALUES (1003,'辛会旭');
insert into tb_student (stuid,stuname) VALUES (1004,'辛会旭'),(1005,'辛长旭'),(1006,'辛桐旭');
–截断表(删除全表)
TRUNCATE TABLE tb_student;
–删除学生
DELETE FROM tb_student WHERE stuid=1002;
– 修改表
UPDATE tb_student set colid=1 WHERE stuid BETWEEN 1001 and 1005
– 修改学生表添加外键约束(参照完整性)
CREATE TABLE tb_teacher
(
teaid int not null comment '工号',
teaname varchar(20) not null comment '姓名',
teatitle varchar(10) default '助教' comment '职称',
collod int not null comment '所属学院',
primary key (teaid),
foreign key (collid) references tb_college (collid) #外键
)
-- 如果表没有外键需要自行以下语句
-- ALTER TABLE tb_student ADD CONSTRAINT fk_student_colid FOREIGN KEY (colid) REFERENCES tb_college (colid)
– 创建选课记录表
CREATE TABLE tb_score
(
scid int auto_increment comment '选课记录编号',
stuid int not null comment '选课学生',
couid int not null comment '所选课程',
scdate datetime comment '选课时间日期',
scmark decimal(4,1) comment '考试成绩',
primary key (scid),
foreign key (stuid) REFERENCES tb_student (stuid),
FOREIGN key (couid) REFERENCES tb_course (couid)
)
– 添加唯一性约束(一个学生某个课程只能选一次)
alter table tb_score add constraint uni_score_stuid_couid unique (stuid,couid);
– 查询所有学生信息
SELECT * from tb_student;
– 查询所有课程名称以及学分(投影和别名)
select couname as 课程, coucredit 学分 from tb_course;
-- 查询数据进行处理
select stuname as 姓名, case stusex when 1 then '男' else '女' end as 性别 from tb_student;
和上一条语句一样,只适用于mysql,oracle不通用
select stuname as 姓名, if(stusex,'男','女') from tb_student;
– 查询所有90后学生的姓名、性别和出生日期(筛选)
select stuname,stusex,stubirth from tb_student where stubirth >= '1990-1-1' and stubirth <= '1999-12-31';
select stuname,stusex,stubirth from tb_student where stubirth between '1990-1-1' and '1999-12-31';
– 查询姓“辛”的学生姓名和性别(模糊)%:一个或多个字符,_:一个字符
select stuname,stusex,stubirth from tb_student where stuname like '辛%';
– 查询录入家庭住址的学生姓名(不为空值)
select stuname from tb_student where stuaddr is not null;
– 查询学生选课的所有日期(去重)
select DISTINCT scdate from tb_score;
– 查询男生的姓名和生日按年龄从打到小排序
– asc - ascending - 升序
– desc - descending - 降序
– order by 后面可以添加多个列
SELECT stuname ,year(NOW()) - year(stubirth) FROM tb_student WHERE stusex =1 order by stubirth desc;
SELECT stuname as 姓名,year(NOW()) - year(stubirth) 年龄 FROM tb_student WHERE stusex =1 order by 年龄 desc;
– 常用的聚合函数 max / min / count / sum / avg
– 聚合函数不计算空值(null)
– 查询年龄最大学生的出生日期(聚合函数)
select min(stubirth) from tb_student;
– 查询年龄最小学生的出生日期(聚合函数)
select max(stubirth) from tb_student;
– 查询男女生的人数(分组和聚合函数)
select stusex,count(stusex) from tb_student group by stusex;
– 查询课程编号为1111课程的平均成绩(筛选和聚合函数)
select avg(scmark) from tb_score where couid = 1111;
– 查询平均成绩大于80分的学生学号和成绩
– 分组前的筛选where子句
– 分组后的筛选having子句
SELECT scid as 学号,AVG(scmark) as 平均分 from tb_score GROUP BY stuid having 平均分>=80;
–查询年龄最大的学生的姓名(子查询)
SELECT * from tb_student t2 WHERE t2.stubirth = (
SELECT min(t1.stubirth) FROM tb_student t1);
– 查询选了两门以上的课程的学生姓名(子查询、分组条件、集合运算)
SELECT stuname FROM tb_student WHERE stuid in (
SELECT stuid FROM tb_score GROUP BY stuid HAVING COUNT(stuid)>2)
字段设置默认值
update 表名 set 字段名称='默认的值' where 字段名称 is null
mysql根据某个字段去重
在有主键的情况下
SELECT * FROM sa_request t WHERE t.logid IN (
SELECT MAX(logid) FROM sa_request GROUP BY caseid
);
或者效率高些的
SELECT * FROM sa_request t WHERE EXISTS (
SELECT 1 FROM sa_request GROUP BY caseid
HAVING MAX(logid) = t.logid
);
---
[去重参考](https://www.cnblogs.com/qlqwjy/p/8270011.html)
多表查询
[多表查询参考](https://www.cnblogs.com/gccbuaa/p/7283907.html)
replace函数替换字符串语句的用法(mysql字符串替换)
比如你要将表 tb1里面的 f1字段的abc替换为def
UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def');
[参考url](https://www.cnblogs.com/kenshinobiy/p/5822698.html)