Mysql基础练习--实例

修改字段名:alter table 表名 change 旧字段名 新字段名 新数据类型;
--- 主键 ----------------------------------------------------------
-- 修改表结构 添加主键
alter table user4 add primary key(id);
-- 如何删除
alter table user4 drop primary key;
-- 使用modify修改字段,添加约束
alter table 表名 modify id int primary key;

--- 唯一约束 ----------------------------------------------------------
-- 第一种方式
create table user5(
id int,
name varchar(20),
unique(name)
);
-- 两个键在一起不重复就行
create table user5(
id int,
name varchar(20),
unique(id,name)
);
-- 后面补唯一键
alter table user5 add unique(name);
-- 删除唯一键约束
alter table user5 drop index name;
-- 通过modify添加
alter table 表名 modify name varchar(20) unique;

--- 外键约束 ----------------------------------------------------------
-- 设计两个表:父表,子表
-- 班级表
create classes(
id int primary key,
name varchar(20)
);
-- 学生表
create table students(
id int primary key,
name varchar(20),
class_id int,
foreign key(class_id) references classes(id)
);
--注意:主表(父表) classes中没有的数据值,在副表(子表)中是不可以使用的
--2.主表中的记录被副表引用,是不可以被删除的

-- 数据库的三大设计范式.sql-----------------------------------------------
-- 1.第一范式
-- 1NF
-- 数据表中的所有字段都是不可分割的原子值
create table student2(
id int primary key,
name varchar(20),
address varchar(20)
);
insert into student2 values(1,'张三','中国河南省新乡市获嘉县中和镇');
insert into student2 values(2,'李四','中国河南省郑州市获嘉县中和镇');
insert into student2 values(3,'王五','中国河南省三门峡市获嘉县中和镇');

-- 字段值还可以继续拆分的,就不满足第一范式
-- 2.第二范式
-- 必须是满足第一范式的前提下,第二范式要求,除主键外的每一列都必须完全依赖与主键
-- 如果要出现不完全依赖,只可能发生在联合主键的情况下

-- 3.第三范式
-- 3NF
-- 必须先满足第二范式,除开主键列的其他列之间不能有传递依赖关系

-- mysql查询练习 --------------------------------------------------------
--学生表
Student
学号
姓名
性别
出生年月日
所在班级
create table student(
sno varchar(20) primary key comment '学生学号',
sname varchar(20) not null comment '学生姓名',
ssex varchar(10) not null comment '学生性别',
sbirthday datetime comment '学生出生年月日',
class varchar(20) comment '学生所在班级'
) comment '学生表';

--课程表
Course
课程号
课程名称
教师编号
create table course(
cno varchar(20) primary key comment '课程号',
cname varchar(20) not null comment '课程名称',
tno varchar(20) not null comment '教师编号',
foreign key(tno) references teacher(tno)
) comment '课程表';

--成绩表
Score
学号
课程号
成绩
create table score(
sno varchar(20) primary key comment '学号',
cno varchar(20) not null comment '课程号',
degree decimal comment '成绩',
foreign key(sno) references student(sno),
foreign key(cno) references course(cno)
)comment '成绩表';

--教师表
Teacher
教师编号
教师名字
教师性别
出生年月日
职称
所在部门
create table teacher(
tno varchar(20) primary key comment '教师编号',
tname varchar(20) not null comment '教室名字',
tsex varchar(10) not null comment '教师性别',
tbirthday datetime comment '教师出生年月日',
prof varchar(20) not null comment '教师职称',
depart varchar(20) not null comment '教师所在部门'
) comment '教师表';

-- 往数据表中添加数据
#添加学生信息
insert into student values('101','曾华','男','1977-09-01','95033');
insert into student values('102','匡明','男','1975-10-02','95031');
insert into student values('103','王丽','女','1976-01-23','95033');
insert into student values('104','李军','男','1976-02-20','95033');
insert into student values('105','王芳','女','1975-02-10','95031');
insert into student values('106','陆君','男','1974-06-03','95031');
insert into student values('107','王尼玛','男','1976-02-20','95033');
insert into student values('108','张全蛋','男','1975-02-10','95031');
insert into student values('109','赵铁柱','男','1974-06-03','95031');
#添加教师表
insert into teacher values('804','李诚','男','1958-12-02','副教授','计算机系');
insert into teacher values('856','张旭','男','1969-03-12','讲师','电子工程系');
insert into teacher values('825','王萍','女','1972-05-05','助教','计算机系');
insert into teacher values('831','刘冰','女','1958-08-14','助教','电子工程系');
#添加课程表
insert into course values('3-105','计算机导论','825');
insert into course values('3-245','操作系统','804');
insert into course values('6-166','数字电路','856');
insert into course values('9-888','高等数学','831');
#添加成绩表
insert into score values('103','3-105','92');
insert into score values('103','3-245','86');
insert into score values('103','6-166','85');
insert into score values('105','3-105','88');
insert into score values('105','3-245','75');
insert into score values('105','6-166','79');
insert into score values('109','3-105','76');
insert into score values('109','3-245','68');
insert into score values('109','6-166','81');

-- 查询练习:
-- 1. 查询student表的所有记录
#select * from student;
-- 2. 查询student表中的所有记录的sname,ssex和class列
#select sname,ssex,class from student;
-- 3. 查询教师所有的单位即不重复的depart列
#select distinct depart from teacher;
-- 4. 查询score表中成绩在60到80之间的所有记录
-- 查询区间 between ... and ...
#select * from score where degree between 60 and 80;
-- 直接运算符比较
#select * from score where degree > 60 and degree < 80;
-- 5. 查询score表中成绩为85,86或88的记录
-- 表示 或者关系的查询 in
#select * from score where degree in(85,86,88);
-- 6. 查询student表中"95031"班或性别为"女"的同学记录
#select * from student where class='95031' or ssex='女';
-- 7. 以class降序查询student表的所有记录
-- 升序,降序 默认顺序是升序 所以不写asc也可以
#select * from student order by class desc;
#select * from student order by class [asc];
-- 8. 以cno升序,degree降序查询score表的所有记录
#select * from score order by cno asc,degree desc;
-- 9. 查询"95031"班的学生人数
-- 统计 count
#select count(*) from student where class='95031';
-- 10. 查询score表中的最高分的学生学号和课程号.(子查询或者排序)
-- 子查询的做法
#select sno,cno from score where degree=(select max(degree) from score);
分解理解:
-- 1. 找到最高分
select max(degree) from score;
-- 2. 找最高分的sno和cno
select sno,cno from score where degree=(select max(degree) from score);
-- 排序的做法
select sno,cno,degree from score order by degree desc limit 0,1;
-- 11. 查询每门课的平均成绩
-- avg()
#select cno,avg(degree) from score group by cno;
-- 12. 查询score表中至少有2名学生选修的并以3开头的课程的平均分数
#select cno,avg(degree),count(*) from score group by cno having count(cno)>=2 and cno like '3%';
-- 13. 查询分时大于70,小于90的sno列
#select sno,degree from score where degree>70 and degree<90;
#select sno,degree from score where degree between 70 and 90;
-- 14. 查询所有学生的sname,cno和degree列 -- (多表查询)
#select sname,cno,degree from student,score where student.sno = score.sno;
-- 15. 查询所有学生的sno,cname和degree列
#select sno,cname,degree from course,score where course.cno = score.cno;
-- 16. 查询所有学生的sname,cname和degree列 -- (三表关联查询) --共同字段的相等
#select sname,cname,degree from student,course,score where student.sno=score.sno and course.cno=score.cno;

原文地址:https://www.cnblogs.com/lszbk/p/12312658.html