標籤:
建立表:
create table Ceshi
(
Uid varchar(50) primary key,
Pwd varchar(50),
Name varchar(50),
Nation varchar(50),
foreign key(Nation) reference Nation(Code)
)
寫查詢語句需要注意:
1.建立表的時候,最後一列後面不要寫逗號
2.如果有多餘語句一起執行,注意在語句之間加分號分隔
3.寫代碼所有符號都是半形的
關係型資料庫:表和表之間是有關係的
建立表的幾個關鍵字:
1.主鍵:primary key
2.非空:not null
3.自增長列:auto_increment
4.外鍵關係:foreign key(列名) reference 表名(列名)
CRUD操作:
1.添加資料:
insert into 表名 values(‘‘,‘‘,‘‘,‘‘) 要求values括弧裡面的值的個數要和表裡面列數相同
insert into 表名(列名,列名) 添加指定列的值
2.修改資料:
update info set name=‘張三‘ where code=‘p001‘
3.刪除資料:
delete from info where code=‘p001‘
查詢資料:
1.普通查詢,查所有的
select*from info 查所有資料
select code,name from info 查指定列
2.條件查詢
select*from info where code=‘ ‘ 一個條件
select*from info where name=‘ ‘ and nation=‘ ‘ 兩個條件並的關係
select*from info where name=‘ ‘ or nation=‘ ‘ 兩個條件或的關係
3.排序查詢
select*from info order by birthday 預設升序排列asc 如果要降序排列 desc
select*from info order by brand,oil desc 多列排序
4.彙總函式
select count(*) from info 取個數
select sum(price) from car 查詢price列的和
select avg(price) from car 查詢price列的平均值
select min(price)/max(price) from car 查詢price列的最小值或最大值
5.分頁查詢
select*from car limit n,m 跳過n條資料取m條資料
6.分組資料
select brand from car group by brand 簡單分組查詢
select brand from car group by brand having count(*)>2 查詢系列裡面車的數量大於2的系列
7.去重查詢
select distinct brand from car
8.修改列名
select brand as ‘系列‘ from car
9.模糊查詢
select*from car where name like ‘奧%‘ %代表任意多個字元 _代表一個字元
10.離散查詢
select*from car where code in (‘ ‘,‘ ‘,‘ ‘,‘ ‘)
select*from car where code not in (‘ ‘,‘ ‘,‘ ‘,‘ ‘)
瞭解資料庫語句