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;
#
#




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