Web數據庫SQL命令,數據庫創建_增刪改查_表與表之間的關聯

1)創建數據庫
create database mytest1;

2)刪除數據庫
drop database mytest1;

3)創建表
 create table stu(StudyNo int primary key auto_increment,
         IdCarNo int,
                Name char(10),
                Sex char(10),
                Elective char(10));

 4)創建一對一表(兩個表關聯,副表的主鍵必須是主表的外鍵纔可以。
 create table country(id int primary key auto_increment,
                    Name char(10),
                    Language char(10));
create table President(id int primary key auto_increment,
                      Name char(10),
                       sex char,
                       f_country_id);
 alter table President add constraint foreign key(f_country_id) references country(id) on delete cascade;//該語句紅色前面部分是將附表中的外鍵與主表中的外鍵相關聯,紅色部分表示刪除主表中的記錄會影響到刪除副表中的相應數據,刪除副表中數據不會影響主表中的相應數據。

創建表:

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時刪除相應的presidentnull

on delete no action:如果country中某條記錄被president指向,那麼刪除country報錯,即不能刪除。

注:on delete cascade à表示class表中的記錄刪除時,stu2表中的外鍵相關聯的記錄也會被刪除

 



5)創建一對多表
create table class(id int primary key  auto_increment,
                  classname char(10),
                  HederTeacher char(10));
create table stu(id int primary key auto_increment,
                  name char(10),
                   age int,
                   f_class_id);
   alter table stu add constraint foreign key(f_class_id) references class(id) on delete set null;//紅色部分表示刪除主表的主鍵後,副表相關聯的部分用null表示。刪除副表不會影響主表。

一對多的關係:

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



  6)創建多對多表
create table teacher_stu_middle(id int primary key auto_increment,
                              f_Teacher_id int,
                              f_Stu_id );
create table Teacher(id int primary key auto_increment,
                    name char(10));
create table Stu(id int primary key auto_increment,
                 name char(10));
   alter table teacher_stu_middle add constraint foreign key(f_Teacher_id)references Teacher(id) on delete no action;
   alter table teacher_stu_middle add constraint foreign key(f_Stu_id)references Stu(id) on delete no action;//紅色部分表示主表不能刪除與附表有任何關聯的記錄,可以刪掉附表中的任意記錄。

多對多的的關係:

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表中的記錄不能刪除

 



7)字段約束
主鍵字約束:primary key
外鍵字約束:foreign key
唯一性約束:unique
非空約束:not null
檢查約束:check(mysql數據庫對該約束不起作用)
缺省約束:default

8)數值類型
int、float、double
字符串類型:char(只能存儲一個字符)
char(M)(能存儲M個字符)(優點:效率高,缺點:佔用內存多
varchar(M)(根據內容決定,但不超過M)(優點:佔用內存少,缺點:效率低



9)數據庫增加條目
 insert into stu(id,name,sex,age)values(13,’小李’,’男’,20);//根據添加的順序依次添加
 insert into stu values(14,’小李’,’男’,34);//必須把所有字段都添加

根據表中的全部字段添加數據

Inset into 表名(字段1,字段2,字段3)values(值1,值2,值3);

這種方式要求添加表中全部的值纔可以添加數據

Insert into 表名 values(值1,值2,值3);

根據表中的某些字段添加數據

Inset into 表名(字段2)values(值2);



10)數據庫刪除條目
     delete from stu where id=2;
     delete from stu where id=2 and sex=’女’;//and表示都滿足的意思
delete from stu where id=2 or sex = ‘男’;//or表示滿足其中之一都可以
delete from stu where id in(25,26);//in表示刪除id=25或者26
delete from stu where id not in(25,26)//not in 表示刪除除了id=25或者26的其它記錄。

刪除字段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不等於123的記錄;

Delete from 表名 where id not in(1,2,3);

刪除id>35的所有記錄,不包括35

Delete from 表名 where id>35;

刪除id>30並且id<34的記錄,不包括3034

Delete from 表名 where id>30 and id<34;

刪除id<2或者id>35的所有記錄,不包括235

Delete from 表名 where id<2 or id>35;



11)數據庫修改條目
update stu set sex=’男’where age>10 and age<20;
update stu set sex=’男’,name=’小李’where id=’2’;//修改多個用逗號分隔

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<10namesexelective的字段信息

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; 



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