Sql语句复习

MySql

一、终端操作数据库库

1.如何登录数据库

​ (1)进入数据库bin文件夹

​ (2)mysql -uroot -p123456

2.查询数据库服务器中所有的数据库

​ (1)show databases;

3.如何选择某个数据库进行操作

​ (1)use mybatis

​ (2)select * from user;

4.推出数据库服务器

​ (1)exit

5.创建数据库

​ (1)create database test;

​ 创建成功:Query OK, 1 row affected (0.01 sec)

6.查看数据库中的所有表

​ (1)show tables;

7.创建表

create table pet(
	name varchar(20),
    owner varchar(20),
    sex char(1),
    birth date);

8.查看创建好的表结构

​ (1)describe pet;

9.增删改查

​ (1)insert

insert into pet
values('dog','dalong','f','1998-03-30');
insert into pet
values('cat','zhangsan','f','2020-01-05');
insert into pet
values('pig','lisi','m','1992-06-22');
insert into pet
values('bird','wangwu','f','2003-03-16');

​ (2)delect

delete from pet where name='cat';

​ (3)update

update pet set name='pigg' where owner='lisi'; 

​ (4)select

select * from pet where name='pigg';

二、约束

1.主键约束

​ (1)能唯一确定一张表中的一条记录,不重复,不为空

create table user(
	id int primary key,
    name varchar(20)
);

​ (2)联合主键

​ 只要联合的主键值加起来不重复就可以,但是不能为空

create table user2(
	id int,
    name varchar(20),
    password varchar(20),
    primary key(id,name)
);

2.自增约束

create table user3(
	id int primary key auto_increment,
    name varchar(20)
);

3.建表后加入 / 删除主键约束

create table user4(
	id int,
    name varchar(20)
);

​ (1)增加

alter table user4 add primary key(id);

​ (2)删除

alter table user4 drop primary key;

​ (3)修改

alter table user4 modify id int primary key;

4.唯一约束

​ (1)约束修饰的字段的值不可以被修改

alter table user5 add unique(name);

​ (2)删除唯一约束

alter table user5 drop index(name);

​ (3)modify方式添加

 alter table user5 modify name varchar(20) unique;

5. 非空约束

​ (1)修饰的字段不能为空

create table user6(
	id int,
    name varchar(20) on null
);

6.默认约束

​ (1)插入字段值的时候,如果没有传值,就会使用默认值

create table user6(
	id int,
    name varchar(20), 
    age int default 10
);

7.外键约束

主表,副表,

主表中没有的数据值,在副表中,是不可以被使用的。

主表中的记录被副表引用,是不可以被删除的。

//班级表
create table 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)
);
insert into classes values(1,'1班'); 
insert into classes values(2,'2班'); 
insert into classes values(3,'3班'); 
insert into classes values(4,'4班'); 
insert into students values(1,'1001',1); 
insert into students values(2,'1002',2); 
insert into students values(3,'1003',3); 
insert into students values(4,'1004',4); 

三、三大范式

1.第一范式

表中的所有字段都是不可分割的原子值

范式,设计的越详细,对于某些实际操作可能更好,但不一定都是好处

create table student2(
	id int,
    name varchar(20),
    address varchar(30)
);

下面address可继续分割

insert into student2 values(1,'zhangsan','山东烟台');
insert into student2 values(2,'lisi','山东威海');
insert into student2 values(3,'wangwu','山东济南');
insert into student2 values(4,'xiaoliu','山东青岛');

2.第二范式

​ 必须是满足第一范式的前提下,第二范式要求,除主键外的每一列都必须完全依赖于主键。

​ 如果出现不完全依赖,只可能发生在联合主键的情况下。

3.第三范式

​ 必须满足2NF,消除传递依赖。

四、练习

1.数据准备

​ (1)学生表

学号、姓名、性别、生日、班级

create table student(
	sno varchar(20) primary key,
    sname varchar(20) not null,
    ssex varchar(10) not null,
    sbirthday datetime,
    class varchar(20)
);

​ (2)课程表

课程号、课程名、教师编号

create table course(
	cno varchar(20) primary key,
    cname varchar(20) not null,
    tno varchar(20) not null,
    foreign key(tno) references teacher(tno)
);

​ (3)成绩表

学号、课程号、成绩

create table score(
	sno varchar(20) primary key,
    cno varchar(20) not null,
    degree decimal,
    foreign key(sno) references student(sno),
    foreign key(cno) references course(cno)
);

​ (4)教师表

教师编号、教师姓名、教师性别、生日、职称、部门

create table teacher(
    tno varchar(20) primary key,
	tname varchar(20) not null,
    tsex varchar(10) not null,
    tbirthday datetime,
    prof varchar(20),
    depart varchar(20) not null
);

2.添加数据

​ (1)学生表

insert into student values('101','sam','m','2002-09-01','12456');
insert into student values('102','jack','m','2001-06-01','12535');
insert into student values('103','amy','f','2003-04-01','14434');
insert into student values('104','kobi','m','2002-07-01','157687');
insert into student values('105','bubi','f','2001-03-01','635354');
insert into student values('106','baby','m','2000-02-01','53878');
insert into student values('107','xxx','m','2000-02-01','53878');
insert into student values('108','hhh','f','2000-02-01','53878');
insert into student values('109','ooo','m','2000-02-01','53878');

​ (2)教师表

insert into teacher values('801','ooo','m','1972-02-01','A','IT');
insert into teacher values('802','iii','f','1973-02-01','B','CH');
insert into teacher values('803','ppp','m','1972-02-01','A','ENG');
insert into teacher values('804','uuu','f','1980-02-01','C','IT');

​ (3)课程表

insert into course values('1-105','database','801');
insert into course values('1-165','C++','804');
insert into course values('2-105','english','803');
insert into course values('3-105','chinese','802');

​ (4)成绩表

insert into score values('101','1-105','79');
insert into score values('102','1-165','97');
insert into score values('102','1-165','88');
insert into score values('104','2-105','62');
insert into score values('105','3-105','76');
insert into score values('106','3-105','83');
insert into score values('106','2-105','92');
insert into score values('108','1-165','75');
insert into score values('109','1-165','66');

3.查询练习

​ (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之间的所有记录。

select * from score where degree between 60 and 80;
select * from score where degree>60 and degree<80;

​ (5)查询score表中成绩为85,86或88的记录。

select * from score where degree in(85,86,88);

​ (6)查询student表中53878班或性别为f的同学记录。

select * from student where class='53878' or ssex='m';

​ (7)以class降序查询student表的所有记录。

降序:desc;升序默认:asc;

select * from student order by class desc;

​ (8)以cno升序、degree降序查询score表的所有记录。

select * from score order by cno asc,degree desc;

​ (9)查询53878班的学生人数.

统计:count

select count(*) from student where class='53878';

​ (10)查询score表中的最高分学生学号和课程号。(子查询或者排序)

select sno,cno from score where degree=(select max(degree) from score);

​ (11)查询每门课的平均成绩

group by分组

select cno,avg(degree) from score group by cno;

​ (12)查询score表中至少有2名学生选修的并以1开头的课程的平均分数

select cno avg(degree) from score group by cno having count(cno)>=2 and cno like '1%';

​ (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;
posted @ 2020-03-26 10:18  Foreverless  阅读(127)  评论(0)    收藏  举报