創建表:
表1名:county
create table county *(id int primary key auto_increment ,name cahr(10),language char(10));
表2名: president
create table president(id int primary key auto_increment,name char(10),sex char,f_country_id int);
爲president表添加外鍵約束,這個外鍵的值來自於country表中的主鍵
Alter table president add constraint foreign key(f_country_id)references country(id)on delete cascade;
on delete cascade:刪除country時刪除相應的president
on delete set null:刪除country時刪除相應的president爲null
on delete no action:如果country中某條記錄被president指向,那麼刪除country報錯,即不能刪除。
注:on delete cascade à表示class表中的記錄刪除時,stu2表中的外鍵相關聯的記錄也會被刪除
一對多的關係:
1、 創建class表
create tableclass(classname char(10),primary key,headteacher char(10));
2、 創建stu2表
Create table stu2(numint primary key auto_increment,name char(10),age int,f_classname char(10));
alter table stu2 addconstant foreign key(f_classname)reference class(classname)on delete set null;
注:delete set null à表示class表中的記錄刪除時,stu2表中外鍵相關聯的外鍵值被設爲null。
多對多的的關係:
1、 創建teacher表
crete table teacher(id int primary key auto_increment,name char(10));
2、 創建stu表
create table stu(id intprimary key auto_increment,name char(10));
3、 創建一箇中間表
create table middle(id int primary key auto_increment,f_teacher_id int,f_stu_id int);
4、 爲中間表設置第一外鍵,這個外鍵的值來自於teacher表
alter table middle add constraint foreign key(f_teacher_id)references teacher(id)on delete no action;
5、 爲中間表設置第二外鍵,這個外鍵的值來自於stu表
alter table middle add constraint foreign key(f_stu_id)references stu(id) on delete no action;
注:on delete no action表示stu表中的記錄不能刪除
根據表中的全部字段添加數據
Inset into 表名(字段1,字段2,字段3)values(值1,值2,值3);
這種方式要求添加表中全部的值纔可以添加數據
Insert into 表名 values(值1,值2,值3);
根據表中的某些字段添加數據
Inset into 表名(字段2)values(值2);
刪除字段1的那條記錄
Delete from 表名 where 字段1=值1;
刪除字段1並且字段2=值2的那條記錄
Delete form 表名 where 字段1=值1and 字段2=值2;
刪除字段1=值2或者字段1=值3的兩條記錄
Delete form 表名where 字段1= 值2 or 字段1= 值3;
刪除字段1=值1以及字段1=值3的兩條記錄
Delete from 表名 where 字段1 in(值1,值3);
刪除id不等於1、2、3的記錄;
Delete from 表名 where id not in(1,2,3);
刪除id>35的所有記錄,不包括35
Delete from 表名 where id>35;
刪除id>30並且id<34的記錄,不包括30、34
Delete from 表名 where id>30 and id<34;
刪除id<2或者id>35的所有記錄,不包括2、35
Delete from 表名 where id<2 or id>35;
將sex=’m’的全部記錄修改爲’男’
Update 表名 set sex=’男’where sex=’m’;
將id=30那條記錄的sex改爲‘女’
Update 表名 set sex=’女’where id=30;
將id=29那條記錄的cardno改爲‘2016’,name改爲‘(●’◡’●)’
Update 表名 set cardno=‘2016’,name=‘(●’◡’●)’where id=29;
查詢數據
查詢表裏的所有數據
Select * from 表名;
查詢student表中id>20的所有記錄的數據
Select * from studentwhere id>20;
查詢表中所有字段2的信息
Select 字段2 from 表名;
查詢表中字段2,字段4,字段6的信息
Select 字段2,字段4,字段6 from 表名;
查詢id<10的name,sex,elective的字段信息
Select name,sex,elective from 表名 where id<10;
多表查詢語句
1、 查詢各班的學生信息:笛卡兒積
方式1:
selectt_class.C_NAME,t-stu.S_NAME,t_stu_S_SEX,t_stu.S_MONEY
from t_class,t_stu;
方式2:
selectc.C_NAME,s.S_NAME,s.S_SEX,s.S_MONEY
from t_class c,s_stu s
2、 查詢各班的學生信息,當t_stu表中的C_ID等於t_class表中的C_ID時,表示該班級的學生
selectc.C_NAME,s.S_NAME,s.S_SEX,s.S_MONEY
from t_class c,t_stu s
where c.C_ID=s.C_ID
3、 查詢班級名稱、學生姓名、性別、繳費、相同班級的要放在一起,姓名根據字典順序排列。
selectc.C_NAME,s.S_NAME,s,s.S_SEX,s.S_MONEY
from t_class c,t_stu s
where c.C_ID=s.C_ID
order by c.C_ID,s.S_ID
4、 查詢各班的班級名稱和人數(通過班級名稱進行分組查詢)
select c.C_NAME,count(*)as 人數
from t_class c,s_stu s
where s.C_ID=c.C_ID
group by c.C_ID
查詢各班名稱和人數,但人數必須小於2,人數多的放在前面
注:group by 用於分組,
Having用於分組後進行條件過濾,
Order by用於選取相應的字段進行排序,
desc表示倒序
查詢沒有人員的班級
注:distinct表示返回表中不同記錄的條數,就是返回不同記錄的字段值
select *from t_class c
wherec.C_ID not in(select distinct s.C_ID from t_stu s where s.S_ID>3);
等價於:
select * from t_class c where c.C_ID not in(21);
分組查詢語句:
根據name字段進行分組查詢,並統計每一組的人數
selectcount(*) from student group by name;
根據name字段查詢,並得到每一個組的人數
selectname,count(*)as 人數 from student group by name;
根據name字段查詢,並得到每一組的人數以及每一組中id的最大值
selectname,count(*) as 人數, max(id) from student group byname;
根據sex字段進行查詢,並統計每一組的人數
select sex,count(*)as 人數,min(id) from student group by sex;
根據sex進行分組查詢,並只對id等於3、28、30的3條記錄進行分組查詢
selectsex,count(*)from student where id in(3,28,30) group by sex;
根據sex進行分組查詢,並只對id>20的所有記錄進行分組查詢
select sex,count(*)from student where id>20 group by sex desc;