国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

常用SQL語句學(xué)習(xí)解釋

2024-07-21 02:12:18
字體:
供稿:網(wǎng)友
(1) 數(shù)據(jù)定義語言(ddl)
數(shù)據(jù)定義語言用來定義數(shù)據(jù)庫的各級模式。常用關(guān)鍵字有:create(建立數(shù)據(jù)表)、alter(更改數(shù)據(jù)表)、drop(刪除數(shù)據(jù)表)。
建立數(shù)據(jù)表
create table table_name(
column1 datatype [not null] [not null primary key],
column2 datatype [not null],
...
)
說明:上面的datatype 指的是字段的類型,nut null 指是否為空,primary key 指本表的主鍵。
建立索引 
create index index_name on table_name (column_name)
說明:為數(shù)據(jù)表格的某個字段建立索引以增加查詢時的速度。
更改數(shù)據(jù)表 
alter table table_name add column column_name datatype
說明:增加一個字段。
alter table table_name add primary key (column_name)
說明:將某個字段設(shè)為主鍵。
alter table table_name drop primary key (column_name)
說明:將某個字段的主鍵定義取消。
刪除數(shù)據(jù)表
drop table_name
drop index_name
(2) 數(shù)據(jù)操作語言(dml)
數(shù)據(jù)操作語言(dml)用于操作數(shù)據(jù)表,如增加、刪除、查詢、修改等。常用關(guān)鍵字有:insert(插入數(shù)據(jù))、delete(刪除數(shù)據(jù))、select(查詢數(shù)據(jù))和updata(修改數(shù)據(jù))。
插入數(shù)據(jù)
insert into table_name(column1,column2,...) values (value1,value2, ...)
說明:在插入語句中,若不指明要插入字段則,按表中的字段順序依次插入。另外,插入數(shù)據(jù)的類型應(yīng)和所插入字段的類型相匹配。
insert into table_name (column1,column2,...) select columnx,columny,...
from another_table
說明:通過一個子查詢將別的表格相應(yīng)字段的值插入該表格。
刪除數(shù)據(jù)
delete from table_name where conditions
說明:刪除符合條件的記錄。
查詢數(shù)據(jù)
select column1,columns2,... from table_name
說明:把table_name 的相應(yīng)字段查詢出來。
select * from table_name where column1 = x and column2 > y
說明:這是一個帶有條件的查詢語句,'*'表示查詢所有的字段,where 之后是條件表達式。
select column1,column2 from table_name order by column2 [desc]
說明:order by 是指定以某個字段排序,[desc]是指從大到小排列,若沒有指明,則是從小到大排列。
select * from table1,table2 where table1.colum1=table2.column1
說明:這是一個組合查詢,查詢兩個表格中 column1 字段具有相同值的記錄。作為兩個表中建立關(guān)系的字段,其類型必須匹配。
select count (*) from table_name where column_name = x
說明:查詢符合條件的記錄數(shù)。
select sum(column1) from table_name
說明:計算column1字段的數(shù)據(jù)總和。除了sum之外,還可以用avg()計算平均值 、用max()計算最大值、用min()計算最小值。
select * from table_name1 where exists (select * from table_name2 where
conditions)
說明:這條查詢語句用一個子查詢語句作為該查詢語句的條件。exists指是否存在。
select * from table_name1 where column1 in (select column1 from
table_name2 where conditions )
說明:in后面接的是一個集合,表示將column1字段的值在集合中的所有記錄從table_name1表中選出來。
select * from table_name1 where column1 like 'x%'
說明:該語句為模糊查詢。這里的“%”是一個通配符,表示將column1字段中以x開頭的所有記錄選出來。
select * from table_name1 where column1 between x and y
說明:between 表示 column1 的值介于 x 和 y之間。
更改資料
update table_name set column1='x' where conditions
說明:該語句表示在某種條件下將column1字段的值改為x。若不加條件,則默認該字段全部更改。
以上是最基本的sql語法知識,若想更深層次的了解,請參閱數(shù)據(jù)庫方面的書籍。
注冊會員,創(chuàng)建你的web開發(fā)資料庫,
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 广德县| 民和| 保山市| 延庆县| 望城县| 沙洋县| 湘乡市| 宣化县| 汪清县| 吴堡县| 贵港市| 疏附县| 安溪县| 分宜县| 沾益县| 北流市| 壤塘县| 双柏县| 四会市| 余江县| 砚山县| 永州市| 桓台县| 九寨沟县| 华容县| 吕梁市| 吉木萨尔县| 武平县| 内江市| 营山县| 乐昌市| 麟游县| 吴堡县| 沂南县| 辉县市| 灵石县| 镶黄旗| 茌平县| 兴城市| 商河县| 瑞丽市|