SQL學習:SQLCookBook源代碼-mysql版本(2)

這裏更新2個~


<span style="font-size:14px;">--EmpAndDept2

--多表查詢
--需要思考的問題
--①通過什麼條件把兩張表聯繫起來
--顯示銷售部門地址和員工姓名
--如果兩張表都有相同名字的字段,則需要帶表名(也可以是別名)
select ename,sal,loc,emp.deptno from emp ,dept where dept.dname='sales' and emp.deptno=dept.deptno;
select ename,sal,loc,e.deptno from emp as e ,dept as d where d.dname='sales' and e.deptno=d.deptno;
--顯示部門號爲10 的部門名,員工名和工資
select d.dname,e.ename,e.sal from emp e,dept d 
where e.deptno=10 and e.deptno=d.deptno;
--顯示僱員名,僱員工資及所在部門的名字,並按部門排序
select d.dname,e.ename,e.sal from emp e,dept d 
where e.deptno=d.deptno order by d.dname;


--自連接:在同一張表上的連接查詢
--顯示某個員工的上級領導的姓名 比如 ford
--1.知道ford上級編號
select ename from emp 
where empno=(select mgr from emp where ename='ford');
--顯示公司每個員工名字和他的上級名字
--分析,把emp表看成兩張表,分別是worker boss
select worker.ename 僱員,boss.ename 老闆 from emp worker,emp boss
where worker.mgr=boss.empno;
--外連接(左外連接和右外連接)在EmpAndDept3裏面有介紹
--子查詢:嵌入到其他sql語句中的select語句,也叫嵌套查詢
--單行子查詢,如 顯示與smith同一部門的所有員工
select * from emp where deptno=
(select deptno from emp where ename='smith');
--多行子查詢
-- 如 查詢和部門10的工作相同的僱員的名字,崗位,工資,部門號
select distinct job from emp where deptno=10;
select * from emp where job in
(select distinct job from emp where deptno=10);
--如何排除10部門自己
select * from emp where (job in
(select distinct job from emp where deptno=10)) and deptno!=10;
--在from子句中使用子查詢</span>



<span style="font-size:14px;">--EmpAndDept3
--主要是簡單查詢和表格的創建(列的默認屬性check/unique/default設置))

--from 子句中使用子查詢
--顯示高於部門平均工資的員工名字,薪水和他所在部門的平均工資
--①首先知道各個部門的平均工資
select avg(sal),deptno from emp group by deptno;
--②把上邊的結果當做一個臨時表對待
select emp.ename,sal,temp.myavg,emp.deptno from
 emp,(select avg(sal) myavg,deptno from emp group by deptno) temp
where emp.deptno=temp.deptno and emp.sal>temp.myavg;


--分頁查詢
select * from emp;
--top後邊的數表示要挑出的記錄數,是sql server語句,mysql不可用
--limit n,i 第一個數值表示從第n+1開始取,i表示總共取幾個數,mysql語句

--①顯示第一個到第四個入職的僱員
select top 4 * from emp order by hiredate;

select * from emp order by hiredate limit 4;

--請顯示第六個到第十個入職的僱員(按照時間的先後順序)
select top 5 * from emp where empno not in
   (select top 5 empno from emp order by hiredate)
   order by hiredate;

select * from emp order by hiredate limit 5,5;

select * from emp 
    where empno not in (select empno from emp order by hiredate) 
    order by hiredate;

--顯示第十一個到十三個入職的信息(時間順序)
select top 3 * from emp where empno not in
(select top 10 empno from emp order by hiredate)
order by hiredate;

select * from emp order by hiredate limit 10,3;

--測試效率(壓力測試)時數據的產生,瘋狂複製 如: 
--identity是sqlserver的自增,mysql不可用
create table test(
testId int primary key identity(1,1),
testName varchar(30),
testPass varchar(30))engine=InnoDB;

--mysql使用auto_increment
create table test(
testId int auto_increment primary key,
testName varchar(30),
testPass varchar(30))engine=InnoDB;


insert into test (testName,testPass) values('xupei','xupei');
insert into test (testName,testPass) select testName,testPass from test --複製列
select count(*) from test
select * from test
select testId from test 
drop table test
--測試後,分頁的效率還是很高的

--如何刪除一張表中重複記錄
create table temp(
catId int,
catName varchar(40)
)

insert into cat values(1,'aa');
insert into cat values(2,'bb');

insert into cat select * from cat;    --複製數據

insert into temp select * from cat;   --把cat 的記錄distinct後,放入臨時表中
delete from cat;                       --cat表的記錄清空
insert into cat select * from #temp;   --把#temp中的數據(無重複記錄)插入cat中
drop table temp;                       --刪除臨時表#temp


--左外連接和右外連接
--顯示公司每個員工和他的上級的名字
--內連接(匹配上的才能顯示)
select w.ename,b.ename from emp w,emp b where w.mgr=b.empno;

--顯示公司每個員工和他的上級的名字,要求沒有上級的人也要顯示
--左外連接:如果左邊的表記錄全部顯示,如果沒有匹配的記錄,就NULL來填
--右外連接:如果右邊的表記錄全部顯示,如果沒有匹配的記錄,就NULL來填
--join其實就是把兩個表聯合在一起了
select w.ename,b.ename from emp w left join emp b on w.mgr=b.empno;
--外連接的作用特別強大,有時比where好,因爲很多時候,我們不只是要選出需要的值,也需要對那些不含有某些值的行進行操作,EmpAndDept4有例子

--創建的表的列約束
--約束用於確保數據庫滿足特定的商業規則,在sql server 中,約束包括
--not null,unique,primary key,foreign key和check五種
--not null:如果在列上定義了not null ,那麼插入數據時,必須爲列提供數據
--auto_increment,即使插入記錄失敗也會自增1
create table test1(
test1Id int auto_increment primary key,
test1name varchar(30) unique,
test1pass varchar(30) not null,
test1age int
)engine=InnoDB;
--unique:當定義了唯一約束後,該列值是不能重複的,但是可以爲null,且最多隻有一個null
--primary key:用於唯一標示錶行的數據,當定義主鍵約束後,該列不能重複,且不能爲null
--需要說明的是:一張表最多有一個主鍵,但可以有多個unique約束
--表可以有複合主鍵
--複合主鍵小案例、
create table test2(
test1Id int ,
test1name varchar(30) ,
test1pass varchar(30) ,
test1age int
primary key(test1Id,test1name)
)engine=InnoDB;
--行級定義和表級定義
--foreign key:用於定義主表和從表之間的關係,外鍵約束要定義在從表上,
--主表則必須具有主鍵約束或unique約束,當定義外鍵約束後,要求外鍵列數據
--必須在主表的主鍵列存在或爲null

--check:用於強制行數據必須滿足的條件,假定在sal列上定義了check約束,並要求
--sal列值在1000~2000之間,如果不在1000~2000之間,就會提示出錯
create table test3(
test1Id int ,
test1name varchar(30) ,
test1pass varchar(30) ,
sal int check(sal>=1000 and sal<=2000)
)engine=InnoDB;

--default使用
create table mes(
mesId int auto_increment primary key,
mescon varchar(2000) not null,
mesdate datetime default getdate()
)engine=InnoDB;
insert into mes(mescon) values('你好')
insert into mes(mescon,mesdate) values('你好','2015-8-5')
select * from mes




--商店售貨系統表設計案例
--現有一個商店的數據庫,記錄客戶及其購物情況,由下邊三個表組成:
--商品goods(商品號goodsId,商品名goodsName,單價unitprice,商品類別category
--,供應商pr0vider);
--客戶custumer(客戶號customerId,姓名name,住址address,電郵email,性別sex,
--身份證cardId);
--購買purchase(客戶號customerId,商品號goodsId,購買數量nums);
--用SQL語言完成下列功能:
--1 建表,在定義中要求聲明:
--① 每個表的主外鍵;
--② 客戶的姓名不能爲空值;
--③ 單價必須大於0,購買數量必須在1到30之間;
--④ 電郵不能夠重複;
--⑤ 客戶性別必須是男或者女,默認是男
--⑥ 商品類別是 食物 日用品

--goods表
create table goods(
goodId nvarchar(50) primary key,
goodName nvarchar(80) not null,
unitprice numeric(10,2) check (unitprice>0),
category nvarchar(3) check(category in('食物','日用品')),
provider nvarchar(50)
)engine=InnoDB;

--customer表
--在mysql中同時設置default和check時需要先設置default 之後再加check約束,否則會報錯
create table customer(
customerId nvarchar(50) primary key,
customername nvarchar(50) not null,
address nvarchar(100),
email nvarchar(100) unique,
sex nchar(1) default '男' check(sex in ('男','女'))  ,
cardId nvarchar(18)
)engine=InnoDB;

--purchase表
create table purchase(
customerId nvarchar(50) ,
goodId nvarchar(50) ,
nums int check(nums>0),
foreign key(customerId) references customer(customerId),
foreign key(goodId) references goods(goodId)
)engine=InnoDB;</span><span style="font-size: 14px;">

</span>







mysql代碼github地址:

https://github.com/databatman/SQLCookBook-MysqlVersion

PS:所有的mysql的EmpAnd1-n代碼都會上傳到這個文件夾,順便求關注啊,雖然還是菜鳥一個,啊哈








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