MySQL数据库SQL语法参考


  一、 材料定义 ddl(data definition language)

   材料定语言是指对 材料的 格局和 状态下定义的语言,他是每个 材料库要 构建时候时首先要面对的,举凡 材料分哪些表格关系、表格内的有什麽栏位主键、表格和表格中间 彼此参考的关系等等,都是在开始的时候所必须规划好的 。

  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

   注明:添加一个栏位(没有删除某个栏位的语法 。

alter table table_name

add primary key (column_name)

   注明:更改表得的定义把某个栏位设为主键 。

alter table table_name

drop primary key (column_name)

   注明:把主键的定义删除 。

  3、 构建索引 

create index index_name on table_name (column_name)

   注明:对某个表格的栏位 构建索引以添加 查问时的速度 。

  4、删除 

drop table_name

drop index_name

  二、 材料操作 dml (data manipulation language)

   材料定义好之後接下来的便是 材料的操作 。 材料的操作不外乎添加 材料(insert)、 查问 材料(query)、更改 材料(update) 、删除 材料(delete)四种模式,以下分 别介绍他们的语法:

  1、添加 材料:

insert into table_name (column1,column2,...)

values ( value1,value2, ...)

   注明:

  1.若没有指定column 系统则会按表格内的栏位顺序填入 材料 。

  2.栏位的 材料 状态和所填入的 材料必须吻合 。

  3.table_name 也 可以是景观 view_name 。

insert into table_name (column1,column2,...)

select columnx,columny,... from another_table

   注明:也 可以 通过一个子 查问(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 是指定以某个栏位做排序,[desc]是指从大到小罗列,若没有指明,则是从小到大

  罗列

  组合 查问

  组合 查问是指所 查问得 材料 起源并 不仅有单一的表格,而是联合一个以上的表格 威力够得到 后果的 。

select *

from table1,table2

where table1.colum1=table2.column1

   注明:

  1. 查问两个表格中其中 column1 值 雷同的 材料 。

  2.固然两个表格 彼此 比较的栏位,其 材料 状态必须 雷同 。

  3.一个复杂的 查问其动用到的表格可能会众多个 。

  整合性的 查问:

select count (*)

from table_name

where column_name = xxx

   注明:

   查问 相符条件的 材料共有几笔 。

select sum(column1)

from table_name

   注明:

  1.计算出总和,所选的栏位必须是可数的数字 状态 。

  2.除此以外还有 avg() 是计算 均匀、max()、min()计算最大最小值的整合性 查问 。

select column1,avg(column2)

from table_name

group by column1

having avg(column2) > xxx

   注明:

  1.group by: 以column1 为一组计算 column2 的 均匀值必须和 avg、sum等整合性 查问的 要害字一同 使用 。

  2.having : 必须和 group by 一同 使用作为整合性的 制约 。

  复合性的 查问

select *

from table_name1

where exists (

select *

from table_name2

where conditions )

   注明:

  1.where 的 conditions 可以是另外一个的 query 。

  2.exists 在此是指存在与否 。

select *

from table_name1

where column1 in (

select column1

from table_name2

where conditions )

   注明: 

  1. in 後面接的是一个 集中, 示意column1 存在 集中里面 。

  2. select 出来的 材料 状态必须 相符 column1 。

   其余 查问

select *

from table_name1

where column1 like 'x%'

   注明:like 必须和後面的'x%' 相 照顾 示意以 x为开头的字串 。

select *

from table_name1

where column1 in ('xxx','yyy',..)

   注明:in 後面接的是一个 集中, 示意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.更改某个栏位设定其值为'xxx' 。

  2.conditions 是所要 相符的条件、若没有 where 则整个 table 的那个栏位都会所有被更改 。

  4、删除 材料:

delete from table_name

where conditions

   注明:删除 相符条件的 材料 。

   注明:对于where条件后面假如包括有日期的 比较,不同数据库有不同的 抒发式 。具体如下:

  (1)假如是access数据库,则为:where mydate>#2000-01-01#

  (2)假如是oracle数据库,则为: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)');

  假如 比较日期 工夫型,则为:

where mydatetime>to_date('2000-01-01 10:00:01','yyyy-mm-dd hh24:mi:ss');