SQL實(shí)例教程
現(xiàn)有person表一張薯蝎,記錄F公司的所有在職人員信息饿幅。表中的信息包括員工名字舷手,年齡衣盾,薪酬
id | name | age | salary |
---|---|---|---|
1 | Joey | 23 | 4000 |
2 | Monica | 22 | 3300 |
3 | Ross | 25 | 8000 |
4 | Ross | 70 | 12000 |
... | ... | ... | ... |
查詢語(yǔ)句
SQL中的查找使用select語(yǔ)句
你可以通過(guò)select查找person表中所有的信息
select * from person;
有些SQL語(yǔ)句需要以捅彻;結(jié)尾
如果你只想查找表中的名字
select name from person;
如果名字叫做Ross的人比較多组去,可以使用distinct去掉重復(fù),只顯示一個(gè)Ross
select distinct name from person;
如果你想知道關(guān)于Joey的信息,你可以使用where語(yǔ)句
select * from person where name='Joey';
但是步淹,你發(fā)現(xiàn)有若干個(gè)Joey从隆,而你只想查找年紀(jì)23的Joey诚撵,那你可以使用and語(yǔ)句
select * from person where name='joey' and age=23;
person表中有很多的名字叫Ross,你想把他們都找出來(lái)广料,并讓他們按照年紀(jì)進(jìn)行升序排序
select * from person where name='Ross' order by age;
如果你想讓Ross的信息以年齡降序排列輸出砾脑,可以
select * from person where name='Ross' order by age desc;
插入語(yǔ)句
有一天,公司來(lái)了一個(gè)采購(gòu)部的經(jīng)理助手艾杏,名字叫Rachel韧衣,年紀(jì)24歲,薪酬為4500」荷#現(xiàn)在需要插入person表格中畅铭,你可以使用insert into語(yǔ)句
insert into person values ('Rachel',24,4500);
或者
insert into person (name,age,salary) values ('Rachel',24,4500);
update語(yǔ)句
在工作了幾個(gè)月后,新來(lái)的Rachel同學(xué)深受Ross的賞識(shí)勃蜘,于是決定給Rachel加薪到6000硕噩,你可以使用update-set-where調(diào)整Rachel的薪酬
update person set salary=6000 where name='Rachel';
刪除語(yǔ)句
然而好景不長(zhǎng),又過(guò)了幾個(gè)月缭贡,Rachel和Ross大吵了一架炉擅。傷心的Rachel決定離開(kāi)F公司,另謀高就阳惹。我們可以使用delete from...where...語(yǔ)句刪除表里面Rachel的信息
delete from person where name = 'Rachel';
limit語(yǔ)句
F公司對(duì)公司員工的年齡進(jìn)行一次統(tǒng)計(jì)谍失,于是你得找出公司中年齡最大的三個(gè)員工,你可以使用limit限制輸出的員工信息條數(shù)
select name, age from person order by age desc limit 3;
like模糊查找
有一天莹汤,你閑得很疼快鱼,所以你決定查詢一下除了Joey外,公司里面有多少人的名字中間帶有"o"
select name from person where name like '%o%';
%匹配一個(gè)或多個(gè)字符纲岭,于是發(fā)現(xiàn)了Monica和Phoebe
如果你只想要匹配一個(gè)字符抹竹,可以使用_
[name]表示匹配任意name中一個(gè)字符,你可以通過(guò)一下方式查找名字不以"J","P","M" 開(kāi)頭的人
select name from person where name like '[!JPM]%';
in操作符
in操作符可以替代or止潮,如果你想查找公司里面所有叫Joey和Phoebe的員工信息
select * from person where name in ('Joey','Phoebe');
between語(yǔ)句
F公司的員工信息是按照入職時(shí)間錄入的窃判,假設(shè)你想要查找在Joey入職后,到Phoebe入職期間喇闸,公司有多少個(gè)新職員
select * from person where name between 'Joey' and 'Phoebe';
Alias語(yǔ)句
Alias可以指定別名兢孝,如果你想查詢F公司里面名字叫Joey,工資為8000的員工信息時(shí)仅偎,你可以
select name as n, salary as s from person;
join語(yǔ)句
現(xiàn)在假設(shè)F公司里面還有一張表vacation跨蟹,表格里面記錄了公司的各個(gè)員工的剩余休假天數(shù)
id | day | pid |
---|---|---|
1 | 6 | 3 |
2 | 7 | 5 |
3 | 0 | 2 |
... | ... | ... |
由于記錄vacation表的時(shí)候使用的是員工的id,即本表中的pid,為了將兩個(gè)表中的信息串聯(lián)起來(lái)橘沥,于是你這樣寫(xiě):
select person.name,vacation.day from person,vacation where person.id = vacation.pid;
當(dāng)然你也可以使用join語(yǔ)句
select person.name, vacation.day from person inner join vacation on person.id = vacation.pid;
union語(yǔ)句
如果你想要從person表和vacation表中查詢數(shù)據(jù)窗轩,并把結(jié)果合并,可以使用union
select name from person
union
select day from vacation;
union默認(rèn)去除相同的查詢結(jié)果,如果你想要獲取原始的查詢結(jié)果可以使用union all
create語(yǔ)句
為了保險(xiǎn)起見(jiàn)座咆,F(xiàn)公司讓你將person表進(jìn)行備份痢艺,并將備份數(shù)據(jù)導(dǎo)入到備份數(shù)據(jù)庫(kù)F.mdb中仓洼,于是你先要?jiǎng)?chuàng)建一個(gè)數(shù)據(jù)庫(kù)F
create database F;
use F;
接著,你需要?jiǎng)?chuàng)建一個(gè)為名personcopy的備份表
create table personcopy
(
id int,
name varchar(255),
age int,
salary int
);
然后將person中的數(shù)據(jù)copy到備份表personcopy中
insert into personcopy select * form person;
這樣就完成了備份工作堤舒,但色建,其實(shí)你還有一種更加簡(jiǎn)單的方法
create table usercopy(
select * from user
);
constraints約束
當(dāng)你以第一種方法備份完數(shù)據(jù)庫(kù)之后,主管馬上找到了你舌缤,因?yàn)槟阍赾reate table時(shí)犯了一些微小但是嚴(yán)重的錯(cuò)誤:personcopy的列未添加約束箕戳。
主管建議你給id,age列都加上not null約束,并且作為表的唯一標(biāo)識(shí)的id需要加上primary key約束以及自增auto_increment
create table personcopy (
id int not full auto_increament,
name not full,
salary
primary key (id)
);
alter語(yǔ)句
盡管你已經(jīng)做得很好了国撵,但是還是有一些細(xì)節(jié)忽略了陵吸,personcopy中的age不能小于18,salary默認(rèn)應(yīng)該為0介牙。已經(jīng)重建了一次表格的你不想再重建了壮虫,于是你使用alter來(lái)添加約束
alter table personcopy
alter salary set default 0;
alter table personcopy
add check (age>18);
又過(guò)了幾天,主管又來(lái)提需求了环础,他需要你在personcopy中添加一個(gè)新的列囚似,工齡workingage
alter table personcopy
add workingage int check(workingage>0);
drop語(yǔ)句
公司的老員工Gunther因?yàn)殚L(zhǎng)期得不到公司的重視,于是线得,他做了一件極其不光彩的事
drop column salary;
他將person中的salary給刪除掉了谆构!
drop table person;
接著Gunther干脆把整個(gè)person給刪掉了
drop database F;
然后Gunther跑路了