LAMP笔记之MySQL篇(2)
數據庫定義語言
{
DDL(Data Definition Language)
用戶需要修改視圖或索引定義,只能先把它們刪除,然后再重建。
所有的SQL語句在結尾處都要使用";"符號。
?
create table <表名>(
<列名><數據類型>[限制條件]
[,<列名><數據類型>[限制條件]]...);
create table student(
sno char(6) not null unique,
sname char(8) not null,
age smallint,
sex char(1),
address char(20)
);
?
alter table進行兩種修改:增加列以及修改原有列的定義。
alter table <tablename> add (
<列名><數據類型>[限制條件]...
);
alter table students add telenum char(8);
新增加的列一律為空值。
?
alter table <tablename> modify (
<列名><數據類型>...
);
alter table students modify address char(30);
如果列中已經存在數據,則不能改變數據類型,只可以增大列寬。
?
drop table students;
?
create view <viewname> [(col1)...] as <查詢子句>;
?
create index <indexname> on <tablename>(<col1>[asc|desc],...);
create index age-index on students (age asc);
drop index age-index;
?
}
?
數據庫查詢語言
{
查詢語言(Query Language),單表查詢,連接查詢,嵌套查詢,集合查詢等。
select子句和from子句是每個查詢語句所必須的,其它子句人選。
where子句說明查詢的條件。
group by子句用來將結果分組,記住,是結果。
order by子句將結果排序,記住,是結果。
?
select sno,sdept from students;
select * from courses;
select sname,2005-sage from students;
select distinct cno from sc;默認為all
?
select sname,sdept from students where sage>20;
select sno,grade from sc where grade between 80 and 90;包括80和90
select sname,sdept from students where sdept in('english','maths');
?
下劃線"_"表示任意單字符,和linux的"?"類似吧(當然啦,如果是漢字,那么就要兩個__鳥)
百分號"%"表示包括長為零的任意長的字符串,和linux的"*"類似吧
如果是完整的字符串,則可以使用比較符號的等號"="取代like
select * from course where cno like 'cool';
<=>select * from course where cno='cool';
select * from students where sname like '李%';
?
select sno,cno from sc where grade is null;
?
select * from students where sage>20 and ssex='男';
??
group by <colname>
? ? ? ? {
? ? ? ? ?聚集函數:AVG MAX MIN SUM COUNT
? ? ? ? ?select AVG(grade) from sc;作用于整個結果
? ? ? ? ?select cno,AVG(grade) from sc group by cno;以cno相同為一組,聚集函數作用于每個分組
? ? ? ? ?}
order by <colname>/<num>
? ? ? ? {
? ? ? ? ?列可以用列名表示,也可以用在select子句出現的序號表示。特別當選擇的列是聚集函數或表達式時,
? ? ? ? ?由于沒有列名,只能用序號表示。
? ? ? ? ?select sno,grade from sc where cno='c001' order by grade asc;
? ? ? ? ?}
?
集合查詢
select sno from students where sage<22
union select sno from sc where cno='c001';
?
連接查詢
也叫多表查詢,通過連接運算符可以實現多個表查詢。
連接是關系數據庫模型的主要特點,也是區別其他類型數據庫管理系統的一個標志。
>>>>等值和非等值查詢
select student.*,sc.*?
from students,sc
where students.sno=sc.sno;
>>>>自身連接查詢(典型例子:先修課程,為表設置別名)
select cr1.cno,cr2.cpre
from course cr1,course cr2
where cr1.cpre=cr2.cno
>>>>外連接查詢
外連接類似于給添加*的表添加了一個任意匹配行,該行記錄能夠和另一邊的表中所有沒有符合連接條件的記錄匹配。
右邊加*,表示左外連接,左邊則是右外連接。
select students.sno,sname,ssex,sdept,cno,grade
from students,sc
where students.sno=sc.sno(*);
<=>select students.sno,sname,ssex,sdept,cno,grade
? ?from students left out join sc on(students.sno=sc.sno);
>>>>復合查詢
?
嵌套查詢
子查詢形成的結果又成為父查詢的條件。
子查詢不能有order by子句,order by子句只能對最終查詢結果排序。
select sname,sdept?
from students?
where sno in (select sno from sc where cno='c002');
?
select sname,sdept?
from students
where sno in (select sno from sc where cno in
? ? ? ? ? ? ? ? ?(select cno from course where cname='C++'));
?
select sname,sage
from students
where sdept in (select sdept from students where sname='王雪');?
?
select sname,sage
from students
where sage <ANY (select sage
? ? ? ? ? ? ? ? ?from student
? ? ? ? ? ? ? ? ?where sdept='CS')
and sdept <>(or !=) 'CS';
<=>
select sname,sage
from student
where sage <
? ? ? ? ? ? (select MAX(sage)
? ? ? ? ? ? ?from students
? ? ? ? ? ? ?where sdept='CS')
and sdept <> 'CS';
?
=ANY等價IN,<ANY等價<MAX,<>ALL等價NOT IN,<ALL等價<MIN
?
select sname,sdept
from student
where exists
? ? ? ? ? ? (select *
? ? ? ? ? ? ?from sc
? ? ? ? ? ? ?where sno=student.sno and cno='c001');
}
轉載于:https://blog.51cto.com/0901huazi/837635
總結
以上是生活随笔為你收集整理的LAMP笔记之MySQL篇(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ_2593最大两不想交子段和问题
- 下一篇: extmail从数据库导出通讯录