標籤:
一、建立表格代碼格式
create table <表名>
(
<列名> <資料類型及長度> [not null],
<列名> <資料類型及長度>,
...
<列名> <資料類型及長度>
)
例:
#人員基本資料表
create table Info //建立表名為Info的表格
(
xuhao int primary key auto_increment, //建列名為xuhao的列,資料類型為整數類型,設為主鍵並且自增長排列
name varchar(50), //建列名為name的列,資料類型為字串類型,長度為50個字元
sex bit, //建列名為sex的列,資料類型為布爾型
minzu varchar(50), //建列名為minzu的列,資料類型為字串類型,長度為50個字元
foreign key(minzu) references Nation(code), //對於minzu列建立外鍵,用表名為Nation的code列為約束
#minzu varchar(50) references Nation(code), //建外鍵的另一種格式,但是運行後在外鍵內為空白
shengri datetime, //建列名為shengri的列,資料類型為時間類型
height double(6,2) //建列名為height的列,資料類型為小數類型,總長度為6,小數點後留2位
);
命令語句:
primary key 主鍵
not null 非空
foreign key(Nation) references Nation(Code) 外鍵
auto_increment 自增長
注意:所有符號必須是英文狀態下的
每個表建立完之後加分號
表裡面的最後一列寫完之後不要加逗號
二、刪除表格代碼格式
drop table <表名>
三、表格內資料的操作
大致可分為增、刪、改、查 CRUD
1.添加資料
insert into <表名>[(列1,列2...)] values (<‘值1’>[,‘值2’...])
注意:
(1)列與值要匹配(數量,類型,排序)
(2)列可以省掉,但值必須與表中的總列數和列的次序完全對應
(3)自增長列列名可以省,但其值的位置不能省,不給明確值的要給自增列賦值‘’
例:
insert into Info(name,minzu,shengri)values(‘’,‘張三’,‘n003’,‘1992-2-2’) //在Info表中添加姓名為張三,民族為n003,生日為1992-2-2 其中‘’為自增長列表示順序增加,其餘列為null
2.刪除資料
delete from Brand //刪除所有資料
delete from car where code=‘c001‘ //刪除表car中code列為c001的行的資料
delete from car where brand=‘b001‘ or brand=‘b004‘ // 刪除表car中brand列為b001和b004的行的資料
delete from car where brand=‘b001‘ || brand=‘b004‘ // 刪除表car中brand列為b001和b004的行的資料
delete from car where brand=‘b007‘ && price>50 //刪除表car中brand列為b007並且price列大於50的行的資料
delete from car where brand=‘b007‘ and price>50 //刪除表car中brand列為b007並且price列大於50的行的資料
3.更改資料
update <表名> set <列=值>[,列=值...] where .....
例:
update info set sex=‘1‘ where code=‘p003‘ // 將info表中code列為p003的行的sex列的資料改為1
update info set sex=‘0‘,nation=‘n004‘,birthday=‘1999-9-9‘ where code=‘p001‘ //將info表中code列為p001的行的sex列改為0,nation列改為n004,birthday列改 為1999-9-9
update car set price=price * 0.9 where price > 30 //將car表中price列大於30的price列的資料乘以0.9
update car set price =price * 0.95 where (brand=‘b006‘ || brand=‘b005‘)&&price>30 // 將car表中brand列為b006和b005並且price列大於30的price列的資料乘以 0.95
4.尋找資料
select * from 表名
select 列名1,列名2... from 表名--投影
select * from 表名 where 條件--篩選
(1)等值與不等值
select * from car where code=‘c001‘; // 從car表中尋找code列為c001的行的資料
select * from car where code != ‘c001‘; // 從car表中尋找code列不為c001的行的資料
select * from car where price > 30; //從car表中尋找price列大於30的行的資料
--下面的都是範圍
select * from car where price >=30 && price <=50; //從car表中尋找price列大於等於30並且小於等於50的行的資料
select * from car where price between 30 and 50 //從car表中尋找price列大於等於30並且小於等於50的行的資料
select * from car where brand=‘b002‘ || brand=‘b004‘ || brand=‘b006‘ //從car表中尋找brand列為b002、b004、b006的行的資料
select * from car where brand in (‘b002‘,‘b004‘,‘b006‘) //從car表中尋找brand列為b002、b004、b006的行的資料
select * from car where brand not in (‘b002‘,‘b004‘,‘b006‘) //從car表中尋找brand列不為b002、b004、b006的行的資料
(2)模糊查
select * from car where name like ‘寶馬%‘%--任意多個任一字元 //從car表中尋找以“寶馬”開頭的所有資料
select * from car where name like ‘%5%‘ // 從car表中尋找中間包含“5”的所有資料
select * from car where name like ‘%型‘ // 從car表中尋找以“型” 結尾的所有資料
select * from car where name like ‘__5%‘ // 從car表中尋找第三位是“5”的所有資料 _ (空格)表示一個任一字元
(3)排序
select * from 表名 where .... order by 列名 [ASC/DESC],列名[asc/desc]....
select * from car order by price desc // 將car表的資料按照price列資料降序排列
select * from car order by brand desc,price asc // 將car表的資料按照brand列資料降序排列,price列資料升序排列
MySQL用程式碼建表(1)