MySQL数据库语法进阶

1.数据库的约束

show create table orders;
CREATE TABLE `orders` (
  `id` int(11) NOT NULL,
  `product` varchar(255) DEFAULT NULL,
  `price` decimal(10,2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

show create table student2;
--CREATE TABLE `student2` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `sex` varchar(5) DEFAULT NULL,
  `address` varchar(100) DEFAULT NULL,
  `math` int(11) DEFAULT NULL,
  `english` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Select * from student2 limit 3,3;
Select product  from orders group by product ;
Select product ,sum(price) as 总计  from orders group by product ;
Select product, sum(price) as 总价 from orders group by product,price;
select product ,sum(price) from orders group by product having sum(price)>30;
#数据库的约束条件
#1,主键约束primarykey 
#2,唯一约束unique
#3,非空约束,not null 
#4,默认值:default
#5,外键约束:foreign key 
#数据库约束的作用
#保证了数据的完整性,正确性,有效性,提高查询效率


#说出主键约束的作用
# 主键有唯一性,且不能为空
# 主键与非空唯一的区别:
# 主键只能有一个,主键用来标识唯一的一条记录,非空和唯一可以有多个,
#主键的使用方法:


create table student3(id int primary key ,name varchar(20),sex varchar(20), address varchar(20),math int);
INSERT INTO student3 ( id, NAME, sex, address, math )
VALUES
	( 1, "张三", "男", "北京", 80 ),
	( 2, "李四", "男", "上海", 86 ),
	( 3, "张洁", "男", "浙江", 90 ),
	( 4, "迪丽热巴", "女", "北京", 50 ),
	( 5, "张靓颖", "女", "北京", 87 );
#查询性别分组
select sex from student3 group by sex;
select name, address from student3 where id=3;

select name from student3 where sex="男";
#查询每个性别的总共人数
select sex, count(*) from student3 group by sex;
#查询每个性别的分数数学总和
select sex, sum(math) from student3 group by sex;
select sex ,address,sum(math) from student3 group by sex,address having sum(math);
select sex ,address,sum(math) from student3 group by sex,address having sum(math)>100;
SELECT
	sex,
	max( math ) 
FROM
	student3 
GROUP BY
	sex 
HAVING
	max( math );



select name,math from student3 where sex="男";






2.表关系以及多表查询

#1. 能够使用SQL语句进行排序(掌握)order by 
#2. 能够使用聚合函数(掌握)sum count vag max min 
#3. 能够使用SQL语句进行分组查询(掌握)group by 
#4. 能够完成数据的备份和恢复 
#5. 能够使用SQL语句添加主键、外键、唯一、非空约束(掌握)alter table 表名 primary key 
#6. 能够说出多表之间的关系及其建表原则


#1.数据库的备份与还原
# 1.1命令行方式备份与还原
#数据库备份
# mysqldump -u root -p 数据库名 >对应的路径(绝对路径)
#案例:mysqldump -uroot -p db>1 e:\db1.sql
#数据库还原
#需要先创建一个数据库
#mysq -u root -p db2 <e:\db1.sql
#然后运行sql语句即可

# 1.唯一约束
# primary key 主键约束,对对应的条件进行约束
 
#1. 能够说出唯一约束的作用
#保证了数据的非空与唯一性,可以使int类型的主键自增
#添加主键约束的两种方式
#1.创建表时添加
create table user2(id int primary key auto_increment);
#自增的话在后面直接添加
AUTO_INCREMENT
#2.在创建表格之后添加,注意不能有空,或者相同的值,否则添加失败
alter table user add id primary key;


#2. 能够添加唯一约束
#唯一性约束使用unique

create table user4(id int ,name varchar(20) unique);
#唯一约束的作用
#保证了数据的唯一性

#唯一约束的基本格式
#1. 说出唯一约束的作用
#字段唯一,不能重复
#2. 添加唯一约束格式?
#字段名 类型,unique




# 2.非空约束
#格式:字段名 类型 not null

#1. 能够说出非空约束的作用
#数据不能为空,null可以有多个
#2. 能够添加非空约束
#格式 字段名  类型 not null


#3.默认值(了解)
#default默认的
#1. 能够说出默认值的作用
#可以根据属性,给对应的列赋值
#2. 能够给字段添加默认值
#字段名 属性 default =对应的值

#**面试题:**

#如果一个字段设置了非空与唯一约束,该字段与主键的区别?3条
#1.非空和唯一可以存在多个
#2.主键可以设置int类型的自增
#3.一个表中主键只能有一个

#4.表关系的概念

#1.多对多
#如果两张表是多对多的关系,需要创建第三张表,并在第三张表中增加两列,引入其他两张表的主键作为自己的外键。


#2.外键约束
#foreign key 外键 references 主表名 (列名);
#使用中间表的目的是维护两表多对多的关系:
#1 中间表插入的数据 必须在多对多的主表中存在。
#2.如果主表的记录在中间表维护了关系,就不能随意删除。如果可以删除,中间表就找不到对应的数据了,这样就没有意义了。
#当前第三张表中的列由于都是引用其他表中的列,我们把第三张表中的这些列称为引用其他表的外键约束。
#给某个表中的某一列添加外键约束:

#**关键字解释**:
#constraint: 添加约束,可以不写
#foreign key(当前表中的列名): 将某个字段作为外键
#references 被引用表名(被引用表的列名) : 外键引用主表的主键
# 标准格式:constraint 约束名 foreign  key 列名 references 主表名(列名);
#给第三张表添加外键约束有两种方式:
#外键的级联(掌握)

#一对多
#一对多的关系表:其中也有2个实体,但是其中A实体中的数据可以对应另外B实体中的多个数据,反过来B实体中的多个数据只能对应A实体中的一个数据。
#一对一
#一对一关系表在实际开发中使用的并不多,其中也是2个实体,其中A实体中的数据只能对应B实体中的一个数据,同时B实体中的数据也只能对应A实体中的一个数据。例如:人和身份证对应关系,老公和老婆的对应关系。

#而一对一在建表的时候,可以在任意一方的表中添加另外一方的主键作为外键即可

#设计学生成绩管理系统数据表(按照给定需求设计即可)

#1、每个教师可以教多门课程

#2、每个课程由一个老师负责

#3、每门课程可以由多个学生选修

#4、每个学生可以选修多门课程

#5、学生选修课程要有成绩

#数据库的三大范式:
#1.第一范式原子性
#第二范式:一张表描述一件事情
#第三范式:在第一第二范式的基础之上,满足主键的约束,表的属性都是引用其他表的主属性


#笛卡尔积现象:数据冗余,解决方法使用内连接
#内连接的两种方法:
#隐式内连接:select * from 表名1,表名2 from 表名1。属性=表名2.属性;
#显示内连接:select * from 表名1 inner join 表名2 on 表名1.属性=表名2.属性;

#左外连接:查询左边表的全部信息,没有与右边表关联的也要显示
#格式:select 列 from 表明1 lift join 表名2 on 条件;

#左外连接:查询右边表的全部信息,没有与右边表关联的也要显示
#格式:select 列 from 表明1 right join 表名2 on 条件;


#子查询

#单行单列

#创建表
use db1;
-- 创建部门表
CREATE TABLE dept (
  id INT PRIMARY KEY AUTO_INCREMENT,
  NAME VARCHAR(20)
);

INSERT INTO dept (NAME) VALUES ('开发部'),('市场部'),('财务部');

-- 创建员工表
CREATE TABLE emp (
  id INT PRIMARY KEY AUTO_INCREMENT,
  NAME VARCHAR(10),
  gender CHAR(1),   -- 性别
  salary DOUBLE,   -- 工资
  join_date DATE,  -- 入职日期
  dept_id INT -- 外键 来自于部门id
);

INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('孙悟空','男',7200,'2013-02-24',1);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('猪八戒','男',3600,'2010-12-02',2);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('唐僧','男',9000,'2008-08-08',2);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('白骨精','女',5000,'2015-10-07',3);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('蜘蛛精','女',4500,'2011-03-14',1);




#需求: 1.查询工资最高的员工是谁
#查询最高工资
select max(salary) from emp;
#查询最高工资的员工姓名
select name from emp where salary in (select max(salary) from emp);
#子查询如果是单行单列,那么可以作为where后面的条件。子查询结果是一个值。

#2.查询工资小于平均工资的员工有哪些

select avg(salary) from emp;

select * from emp where salary >(select avg(salary) from emp);
#多行单列

#3.查询工资大于5000的员工,来自于哪些部门的名字

#分析:
#查询工资大于5000的员工部门id
select dept_id from emp where salary >5000;
#根据部门表查询部门
select name from dept where id in(select dept_id from emp where salary >5000);

#4.查询开发部与财务部所有的员工信息

select id from dept where name in("开发部","财务部");
select * from emp where dept_id in (select id from dept where name in("开发部","财务部"));






#多行多列
#5.查询出2011年以后入职的员工信息,包括部门名称

select * from emp where join_date >("2011-01-01");
select  e.*, dept.name  from dept inner join (select * from emp where join_date >("2011-01-01")) as e on e.dept_id=dept.id;






#子查询创建和添加数据语句
use day0201;
create table teacher (
  id int(11) not null primary key auto_increment,
  name varchar(20) not null unique
 );
create table student (
  id int(11) not null primary key auto_increment,
  name varchar(20) NOT NULL unique,
  city varchar(40) NOT NULL,
  age int 
) ;
create table course(
  id int(11) not null primary key auto_increment,
  name varchar(20) not null unique,
  teacher_id int(11) not null,
  foreign key(teacher_id) references teacher (id)
);

create table studentcourse (
   student_id int NOT NULL,
   course_id int NOT NULL,
   score double NOT NULL,
   foreign key (student_id) references student (id),
   foreign key (course_id) references course (id)
);

insert into teacher values(null,'关羽');
insert into teacher values(null,'张飞');
insert into teacher values(null,'赵云');

insert into student values(null,'小王','北京',20);
insert into student values(null,'小李','上海',18);
insert into student values(null,'小周','北京',22);
insert into student values(null,'小刘','北京',21);
insert into student values(null,'小张','上海',22);
insert into student values(null,'小赵','北京',17);
insert into student values(null,'小蒋','上海',23);
insert into student values(null,'小韩','北京',25);
insert into student values(null,'小魏','上海',18);
insert into student values(null,'小明','广州',20);

insert into course values(null,'语文',1);
insert into course values(null,'数学',1);
insert into course values(null,'生物',2);
insert into course values(null,'化学',2);
insert into course values(null,'物理',2);
insert into course values(null,'英语',3);

insert into studentcourse values(1,1,80);
insert into studentcourse values(1,2,90);
insert into studentcourse values(1,3,85);
insert into studentcourse values(1,4,78);
insert into studentcourse values(2,2,53);
insert into studentcourse values(2,3,77);
insert into studentcourse values(2,5,80);
insert into studentcourse values(3,1,71);
insert into studentcourse values(3,2,70);
insert into studentcourse values(3,4,80);
insert into studentcourse values(3,5,65);
insert into studentcourse values(3,6,75);
insert into studentcourse values(4,2,90);
insert into studentcourse values(4,3,80);
insert into studentcourse values(4,4,70);
insert into studentcourse values(4,6,95);
insert into studentcourse values(5,1,60);
insert into studentcourse values(5,2,70);
insert into studentcourse values(5,5,80);
insert into studentcourse values(5,6,69);
insert into studentcourse values(6,1,76);
insert into studentcourse values(6,2,88);
insert into studentcourse values(6,3,87);
insert into studentcourse values(7,4,80);
insert into studentcourse values(8,2,71);
insert into studentcourse values(8,3,58);
insert into studentcourse values(8,5,68);
insert into studentcourse values(9,2,88);
insert into studentcourse values(10,1,77);
insert into studentcourse values(10,2,76);
insert into studentcourse values(10,3,80);
insert into studentcourse values(10,4,85);
insert into studentcourse values(10,5,83);



#需求 1.查询获得最高分的学生信息。

select max(score) from studentcourse ;
select student_id from studentcourse where score in(select max(score) from studentcourse );

select * from student where id in (select student_id from studentcourse where score in(select max(score) from studentcourse ));

#2.查询编号是2的课程比编号是1的课程最高成绩高的学生信息
select max(score) from studentcourse where course_id in(1);
select student_id,score from studentcourse where course_id in(2) and score>(select max(score) from studentcourse where course_id in(1));

select e.score,student.name from student inner join (select student_id,score from studentcourse where course_id in(2) and score>(select max(score) from studentcourse where course_id in(1))) as e on e.student_id =student.id;
#3.查询编号是2的课程比编号是1的课程最高成绩高的学生姓名和成绩


#4.查询每个同学的学号、姓名、选课数、总成绩
select * from student inner join studentcourse on studentcourse.student_id =student.id ;
select student_id, count(course_id) ,sum(score) from studentcourse group by student_id;
select   student.name ,student.id, ee.* from student inner join (select student_id, count(course_id) ,sum(score) from studentcourse group by student_id) as ee  on ee.student_id =student.id;
#
#




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章