MySQL SQL 語法參考
更新時(shí)間:2006年12月03日 00:00:00 作者:
一、資料定義 ddl(data definition language)
資料定語言是指對(duì)資料的格式和形態(tài)下定義的語言,他是每個(gè)資料庫要建立時(shí)候時(shí)首先要面對(duì)的,舉凡資料分哪些表格關(guān)系、表格內(nèi)的有什麼欄位主鍵、表格和表格之間互相參考的關(guān)系等等,都是在開始的時(shí)候所必須規(guī)劃好的。
1、建表格:
create table table_name(
column1 datatype [not null] [not null primary key],
column2 datatype [not null],
...);
說明:
datatype --是資料的格式,詳見表。
nut null --可不可以允許資料有空的(尚未有資料填入)。
primary key --是本表的主鍵。
2、更改表格
alter table table_name
add column column_name datatype
說明:增加一個(gè)欄位(沒有刪除某個(gè)欄位的語法。
alter table table_name
add primary key (column_name)
說明:更改表得的定義把某個(gè)欄位設(shè)為主鍵。
alter table table_name
drop primary key (column_name)
說明:把主鍵的定義刪除。
3、建立索引
create index index_name on table_name (column_name)
說明:對(duì)某個(gè)表格的欄位建立索引以增加查詢時(shí)的速度。
4、刪除
drop table_name
drop index_name
二、的資料形態(tài) datatypes
smallint
16 位元的整數(shù)。
interger
32 位元的整數(shù)。
decimal(p,s)
p 精確值和 s 大小的十進(jìn)位整數(shù),精確值p是指全部有幾個(gè)數(shù)(digits)大小值,s是指小數(shù)
點(diǎn)後有幾位數(shù)。如果沒有特別指定,則系統(tǒng)會(huì)設(shè)為 p=5; s=0 。
float
32位元的實(shí)數(shù)。
double
64位元的實(shí)數(shù)。
char(n)
n 長(zhǎng)度的字串,n不能超過 254。
varchar(n)
長(zhǎng)度不固定且其最大長(zhǎng)度為 n 的字串,n不能超過 4000。
graphic(n)
和 char(n) 一樣,不過其單位是兩個(gè)字元 double-bytes, n不能超過127。這個(gè)形態(tài)是為
了支援兩個(gè)字元長(zhǎng)度的字體,例如中文字。
vargraphic(n)
可變長(zhǎng)度且其最大長(zhǎng)度為 n 的雙字元字串,n不能超過 2000。
date
包含了 年份、月份、日期。
time
包含了 小時(shí)、分鐘、秒。
timestamp
包含了 年、月、日、時(shí)、分、秒、千分之一秒。
三、資料操作 dml (data manipulation language)
資料定義好之後接下來的就是資料的操作。資料的操作不外乎增加資料(insert)、查詢資料(query)、更改資料(update) 、刪除資料(delete)四種模式,以下分 別介紹他們的語法:
1、增加資料:
insert into table_name (column1,column2,...)
values ( value1,value2, ...)
說明:
1.若沒有指定column 系統(tǒng)則會(huì)按表格內(nèi)的欄位順序填入資料。
2.欄位的資料形態(tài)和所填入的資料必須吻合。
3.table_name 也可以是景觀 view_name。
insert into table_name (column1,column2,...)
select columnx,columny,... from another_table
說明:也可以經(jīng)過一個(gè)子查詢(subquery)把別的表格的資料填入。
2、查詢資料:
基本查詢
select column1,columns2,...
from table_name
說明:把table_name 的特定欄位資料全部列出來
select *
from table_name
where column1 = xxx
[and column2 > yyy] [or column3 <> zzz]
說明:
1.'*'表示全部的欄位都列出來。
2.where 之後是接條件式,把符合條件的資料列出來。
select column1,column2
from table_name
order by column2 [desc]
說明:order by 是指定以某個(gè)欄位做排序,[desc]是指從大到小排列,若沒有指明,則是從小到大
排列
組合查詢
組合查詢是指所查詢得資料來源并不只有單一的表格,而是聯(lián)合一個(gè)以上的
表格才能夠得到結(jié)果的。
select *
from table1,table2
where table1.colum1=table2.column1
說明:
1.查詢兩個(gè)表格中其中 column1 值相同的資料。
2.當(dāng)然兩個(gè)表格相互比較的欄位,其資料形態(tài)必須相同。
3.一個(gè)復(fù)雜的查詢其動(dòng)用到的表格可能會(huì)很多個(gè)。
整合性的查詢:
select count (*)
from table_name
where column_name = xxx
說明:
查詢符合條件的資料共有幾筆。
select sum(column1)
from table_name
說明:
1.計(jì)算出總和,所選的欄位必須是可數(shù)的數(shù)字形態(tài)。
2.除此以外還有 avg() 是計(jì)算平均、max()、min()計(jì)算最大最小值的整合性查詢。
select column1,avg(column2)
from table_name
group by column1
having avg(column2) > xxx
說明:
1.group by: 以column1 為一組計(jì)算 column2 的平均值必須和 avg、sum等整合性查詢的關(guān)鍵字
一起使用。
2.having : 必須和 group by 一起使用作為整合性的限制。
復(fù)合性的查詢
select *
from table_name1
where exists (
select *
from table_name2
where conditions )
說明:
1.where 的 conditions 可以是另外一個(gè)的 query。
2.exists 在此是指存在與否。
select *
from table_name1
where column1 in (
select column1
from table_name2
where conditions )
說明:
1. in 後面接的是一個(gè)集合,表示column1 存在集合里面。
2. select 出來的資料形態(tài)必須符合 column1。
其他查詢
select *
from table_name1
where column1 like 'x%'
說明:like 必須和後面的'x%' 相呼應(yīng)表示以 x為開頭的字串。
select *
from table_name1
where column1 in ('xxx','yyy',..)
說明:in 後面接的是一個(gè)集合,表示column1 存在集合里面。
select *
from table_name1
where column1 between xx and yy
說明:between 表示 column1 的值介於 xx 和 yy 之間。
3、更改資料:
update table_name
set column1='xxx'
where conditoins
說明:
1.更改某個(gè)欄位設(shè)定其值為'xxx'。
2.conditions 是所要符合的條件、若沒有 where 則整個(gè) table 的那個(gè)欄位都會(huì)全部被更改。
4、刪除資料:
delete from table_name
where conditions
說明:刪除符合條件的資料。
說明:關(guān)于where條件后面如果包含有日期的比較,不同數(shù)據(jù)庫有不同的表達(dá)式。具體如下:
(1)如果是access數(shù)據(jù)庫,則為:where mydate>#2000-01-01#
(2)如果是oracle數(shù)據(jù)庫,則為:where mydate>cast('2000-01-01' as date)
或:where mydate>to_date('2000-01-01','yyyy-mm-dd')
在delphi中寫成:
thedate='2000-01-01';
query1.sql.add('select * from abc where mydate>cast('+''''+thedate+''''+' as date)');
如果比較日期時(shí)間型,則為:
where mydatetime>to_date('2000-01-01 10:00:01','yyyy-mm-dd hh24:mi:ss');
資料定語言是指對(duì)資料的格式和形態(tài)下定義的語言,他是每個(gè)資料庫要建立時(shí)候時(shí)首先要面對(duì)的,舉凡資料分哪些表格關(guān)系、表格內(nèi)的有什麼欄位主鍵、表格和表格之間互相參考的關(guān)系等等,都是在開始的時(shí)候所必須規(guī)劃好的。
1、建表格:
create table table_name(
column1 datatype [not null] [not null primary key],
column2 datatype [not null],
...);
說明:
datatype --是資料的格式,詳見表。
nut null --可不可以允許資料有空的(尚未有資料填入)。
primary key --是本表的主鍵。
2、更改表格
alter table table_name
add column column_name datatype
說明:增加一個(gè)欄位(沒有刪除某個(gè)欄位的語法。
alter table table_name
add primary key (column_name)
說明:更改表得的定義把某個(gè)欄位設(shè)為主鍵。
alter table table_name
drop primary key (column_name)
說明:把主鍵的定義刪除。
3、建立索引
create index index_name on table_name (column_name)
說明:對(duì)某個(gè)表格的欄位建立索引以增加查詢時(shí)的速度。
4、刪除
drop table_name
drop index_name
二、的資料形態(tài) datatypes
smallint
16 位元的整數(shù)。
interger
32 位元的整數(shù)。
decimal(p,s)
p 精確值和 s 大小的十進(jìn)位整數(shù),精確值p是指全部有幾個(gè)數(shù)(digits)大小值,s是指小數(shù)
點(diǎn)後有幾位數(shù)。如果沒有特別指定,則系統(tǒng)會(huì)設(shè)為 p=5; s=0 。
float
32位元的實(shí)數(shù)。
double
64位元的實(shí)數(shù)。
char(n)
n 長(zhǎng)度的字串,n不能超過 254。
varchar(n)
長(zhǎng)度不固定且其最大長(zhǎng)度為 n 的字串,n不能超過 4000。
graphic(n)
和 char(n) 一樣,不過其單位是兩個(gè)字元 double-bytes, n不能超過127。這個(gè)形態(tài)是為
了支援兩個(gè)字元長(zhǎng)度的字體,例如中文字。
vargraphic(n)
可變長(zhǎng)度且其最大長(zhǎng)度為 n 的雙字元字串,n不能超過 2000。
date
包含了 年份、月份、日期。
time
包含了 小時(shí)、分鐘、秒。
timestamp
包含了 年、月、日、時(shí)、分、秒、千分之一秒。
三、資料操作 dml (data manipulation language)
資料定義好之後接下來的就是資料的操作。資料的操作不外乎增加資料(insert)、查詢資料(query)、更改資料(update) 、刪除資料(delete)四種模式,以下分 別介紹他們的語法:
1、增加資料:
insert into table_name (column1,column2,...)
values ( value1,value2, ...)
說明:
1.若沒有指定column 系統(tǒng)則會(huì)按表格內(nèi)的欄位順序填入資料。
2.欄位的資料形態(tài)和所填入的資料必須吻合。
3.table_name 也可以是景觀 view_name。
insert into table_name (column1,column2,...)
select columnx,columny,... from another_table
說明:也可以經(jīng)過一個(gè)子查詢(subquery)把別的表格的資料填入。
2、查詢資料:
基本查詢
select column1,columns2,...
from table_name
說明:把table_name 的特定欄位資料全部列出來
select *
from table_name
where column1 = xxx
[and column2 > yyy] [or column3 <> zzz]
說明:
1.'*'表示全部的欄位都列出來。
2.where 之後是接條件式,把符合條件的資料列出來。
select column1,column2
from table_name
order by column2 [desc]
說明:order by 是指定以某個(gè)欄位做排序,[desc]是指從大到小排列,若沒有指明,則是從小到大
排列
組合查詢
組合查詢是指所查詢得資料來源并不只有單一的表格,而是聯(lián)合一個(gè)以上的
表格才能夠得到結(jié)果的。
select *
from table1,table2
where table1.colum1=table2.column1
說明:
1.查詢兩個(gè)表格中其中 column1 值相同的資料。
2.當(dāng)然兩個(gè)表格相互比較的欄位,其資料形態(tài)必須相同。
3.一個(gè)復(fù)雜的查詢其動(dòng)用到的表格可能會(huì)很多個(gè)。
整合性的查詢:
select count (*)
from table_name
where column_name = xxx
說明:
查詢符合條件的資料共有幾筆。
select sum(column1)
from table_name
說明:
1.計(jì)算出總和,所選的欄位必須是可數(shù)的數(shù)字形態(tài)。
2.除此以外還有 avg() 是計(jì)算平均、max()、min()計(jì)算最大最小值的整合性查詢。
select column1,avg(column2)
from table_name
group by column1
having avg(column2) > xxx
說明:
1.group by: 以column1 為一組計(jì)算 column2 的平均值必須和 avg、sum等整合性查詢的關(guān)鍵字
一起使用。
2.having : 必須和 group by 一起使用作為整合性的限制。
復(fù)合性的查詢
select *
from table_name1
where exists (
select *
from table_name2
where conditions )
說明:
1.where 的 conditions 可以是另外一個(gè)的 query。
2.exists 在此是指存在與否。
select *
from table_name1
where column1 in (
select column1
from table_name2
where conditions )
說明:
1. in 後面接的是一個(gè)集合,表示column1 存在集合里面。
2. select 出來的資料形態(tài)必須符合 column1。
其他查詢
select *
from table_name1
where column1 like 'x%'
說明:like 必須和後面的'x%' 相呼應(yīng)表示以 x為開頭的字串。
select *
from table_name1
where column1 in ('xxx','yyy',..)
說明:in 後面接的是一個(gè)集合,表示column1 存在集合里面。
select *
from table_name1
where column1 between xx and yy
說明:between 表示 column1 的值介於 xx 和 yy 之間。
3、更改資料:
update table_name
set column1='xxx'
where conditoins
說明:
1.更改某個(gè)欄位設(shè)定其值為'xxx'。
2.conditions 是所要符合的條件、若沒有 where 則整個(gè) table 的那個(gè)欄位都會(huì)全部被更改。
4、刪除資料:
delete from table_name
where conditions
說明:刪除符合條件的資料。
說明:關(guān)于where條件后面如果包含有日期的比較,不同數(shù)據(jù)庫有不同的表達(dá)式。具體如下:
(1)如果是access數(shù)據(jù)庫,則為:where mydate>#2000-01-01#
(2)如果是oracle數(shù)據(jù)庫,則為:where mydate>cast('2000-01-01' as date)
或:where mydate>to_date('2000-01-01','yyyy-mm-dd')
在delphi中寫成:
thedate='2000-01-01';
query1.sql.add('select * from abc where mydate>cast('+''''+thedate+''''+' as date)');
如果比較日期時(shí)間型,則為:
where mydatetime>to_date('2000-01-01 10:00:01','yyyy-mm-dd hh24:mi:ss');
相關(guān)文章
Mysql鎖內(nèi)部實(shí)現(xiàn)機(jī)制之C源碼解析
數(shù)據(jù)庫之所以要加鎖,因?yàn)閿?shù)據(jù)庫是一個(gè)多用戶使用的共享資源。當(dāng)多個(gè)用戶并發(fā)地存取數(shù)據(jù)時(shí),在數(shù)據(jù)庫中就會(huì)產(chǎn)生多個(gè)事務(wù)同時(shí)存取同一數(shù)據(jù)的情況。若對(duì)并發(fā)操作不加控制就可能會(huì)讀取和存儲(chǔ)不正確的數(shù)據(jù),破壞數(shù)據(jù)庫的一致性2022-08-08
MySQL/MariaDB 如何實(shí)現(xiàn)數(shù)據(jù)透視表的示例代碼
這篇文章主要介紹了MySQL/MariaDB 如何實(shí)現(xiàn)數(shù)據(jù)透視表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
MySQL數(shù)據(jù)庫入門之備份數(shù)據(jù)庫操作詳解
這篇文章主要介紹了MySQL數(shù)據(jù)庫入門之備份數(shù)據(jù)庫操作,結(jié)合實(shí)例形式詳細(xì)分析了MySQL備份數(shù)據(jù)庫基本操作命令與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-05-05
mysql使用from與join兩表查詢的區(qū)別總結(jié)
這篇文章主要給大家介紹了關(guān)于mysql使用from與join兩表查詢的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Windows?Server?2019?MySQL數(shù)據(jù)庫的安裝與配置理論+遠(yuǎn)程連接篇
mysql是一款關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由MySQL?AB公司開發(fā),目前屬于Oracle旗下產(chǎn)品,MySQL是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)之一。MySQL也是一款開源的SQL數(shù)據(jù)庫管理系統(tǒng),是眾多小型網(wǎng)站作為網(wǎng)站數(shù)據(jù)庫的首選數(shù)據(jù)庫2023-05-05
Mysql計(jì)算字段長(zhǎng)度函數(shù)之LENGTH函數(shù)
MySQL length是指在MySQL數(shù)據(jù)庫中,用于獲取字符串或二進(jìn)制數(shù)據(jù)類型的長(zhǎng)度,下面這篇文章主要給大家介紹了關(guān)于Mysql計(jì)算字段長(zhǎng)度函數(shù)之LENGTH函數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
mysql5.7.24 解壓版安裝步驟及遇到的問題小結(jié)
這篇文章主要介紹了mysql5.7.24 解壓版安裝步驟以及遇到的問題 ,文中給大家提出了解決方案,需要的朋友可以參考下2018-11-11

