﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-toserver</title><link>http://www.cnitblog.com/toserver/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 05 May 2026 14:56:59 GMT</lastBuildDate><pubDate>Tue, 05 May 2026 14:56:59 GMT</pubDate><ttl>60</ttl><item><title>MySQL数据库SQL语法参考</title><link>http://www.cnitblog.com/toserver/archive/2006/08/07/14884.html</link><dc:creator>toserver</dc:creator><author>toserver</author><pubDate>Mon, 07 Aug 2006 14:47:00 GMT</pubDate><guid>http://www.cnitblog.com/toserver/archive/2006/08/07/14884.html</guid><wfw:comment>http://www.cnitblog.com/toserver/comments/14884.html</wfw:comment><comments>http://www.cnitblog.com/toserver/archive/2006/08/07/14884.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/toserver/comments/commentRss/14884.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/toserver/services/trackbacks/14884.html</trackback:ping><description><![CDATA[一、资料定义 ｄｄｌ（data definition language) 
<p>　　资料定语言是指对资料的格式和形态下定义的语言，他是每个资料库要建立时候时首先要面对的，举凡资料分哪些表格关系、表格内的有什麽栏位主键、表格和表格之间互相参考的关系等等，都是在开始的时候所必须规划好的。 </p><p>　　１、建表格： </p><p>create table table_name( <br />column1 datatype [not null] [not null primary key], <br />column2 datatype [not null], <br />...);</p><p>　　说明：　 </p><p>datatype --是资料的格式，详见表。 <br />nut null --可不可以允许资料有空的（尚未有资料填入）。 <br />primary key --是本表的主键。 </p><p>　　２、更改表格　 </p><p>alter table table_name <br />add column column_name datatype </p><p>　　说明：增加一个栏位（没有删除某个栏位的语法。 </p><p>alter table table_name <br />add primary key (column_name) </p><p>　　说明：更改表得的定义把某个栏位设为主键。 </p><p>alter table table_name <br />drop primary key (column_name) </p><p>　　说明：把主键的定义删除。 </p><p>　　３、建立索引　 </p><p>create index index_name on table_name (column_name) </p><p>　　说明：对某个表格的栏位建立索引以增加查询时的速度。 </p><p>　　４、删除　 </p><p>drop table_name <br />drop index_name </p><p>　　二、资料操作 ｄｍｌ （data manipulation language) </p><p>　　资料定义好之後接下来的就是资料的操作。资料的操作不外乎增加资料（insert)、查询资料（query）、更改资料（update) 、删除资料（delete）四种模式，以下分 别介绍他们的语法： </p><p>　　１、增加资料： </p><p>insert into table_name (column1,column2,...) <br />values ( value1,value2, ...) </p><p>　　说明： </p><p>　　1.若没有指定column 系统则会按表格内的栏位顺序填入资料。 </p><p>　　2.栏位的资料形态和所填入的资料必须吻合。 </p><p>　　3.table_name 也可以是景观 view_name。 </p><p>insert into table_name (column1,column2,...) <br />select columnx,columny,... from another_table </p><p>　　说明：也可以经过一个子查询（subquery）把别的表格的资料填入。 </p><p>　　２、查询资料： </p><p>　　基本查询 </p><p>select column1,columns2,... <br />from table_name </p><p>　　说明：把table_name 的特定栏位资料全部列出来 </p><p>select * <br />from table_name <br />where column1 = xxx <br />[and column2 &gt; yyy] [or column3 &lt;&gt; zzz] </p><p>　　说明： </p><p>　　1.'*'表示全部的栏位都列出来。 </p><p>　　2.where 之後是接条件式，把符合条件的资料列出来。 </p><p>select column1,column2 <br />from table_name <br />order by column2 [desc] </p><p>　　说明：order by 是指定以某个栏位做排序，[desc]是指从大到小排列，若没有指明，则是从小到大 </p><p>　　排列 </p><p>　　组合查询 </p><p>　　组合查询是指所查询得资料来源并不只有单一的表格，而是联合一个以上的表格才能够得到结果的。 </p><p>select * <br />from table1,table2 <br />where table1.colum1=table2.column1 </p><p>　　说明： </p><p>　　1.查询两个表格中其中 column1 值相同的资料。 </p><p>　　2.当然两个表格相互比较的栏位，其资料形态必须相同。 </p><p>　　3.一个复杂的查询其动用到的表格可能会很多个。 </p><p>　　整合性的查询： </p><p>select count (*) <br />from table_name <br />where column_name = xxx </p><p>　　说明： </p><p>　　查询符合条件的资料共有几笔。 </p><p>select sum(column1) <br />from table_name </p><p>　　说明： </p><p>　　1.计算出总和，所选的栏位必须是可数的数字形态。 </p><p>　　2.除此以外还有 avg() 是计算平均、max()、min()计算最大最小值的整合性查询。 </p><p>select column1,avg(column2) <br />from table_name <br />group by column1 <br />having avg(column2) &gt; xxx </p><p>　　说明： </p><p>　　1.group by: 以column1 为一组计算 column2 的平均值必须和 avg、sum等整合性查询的关键字一起使用。 </p><p>　　2.having : 必须和 group by 一起使用作为整合性的限制。 </p><p>　　复合性的查询 </p><p>select * <br />from table_name1 <br />where exists ( <br />select * <br />from table_name2 <br />where conditions ) </p><p>　　说明： </p><p>　　1.where 的 conditions 可以是另外一个的 query。 </p><p>　　2.exists 在此是指存在与否。 </p><p>select * <br />from table_name1 <br />where column1 in ( <br />select column1 <br />from table_name2 <br />where conditions ) </p><p>　　说明：　 </p><p>　　1. in 後面接的是一个集合，表示column1 存在集合里面。 </p><p>　　2. select 出来的资料形态必须符合 column1。 </p><p>　　其他查询 </p><p>select * <br />from table_name1 <br />where column1 like 'x%' </p><p>　　说明：like 必须和後面的'x%' 相呼应表示以 x为开头的字串。 </p><p>select * <br />from table_name1 <br />where column1 in ('xxx','yyy',..) </p><p>　　说明：in 後面接的是一个集合，表示column1 存在集合里面。 </p><p>select * <br />from table_name1 <br />where column1 between xx and yy </p><p>　　说明：between 表示 column1 的值介於 xx 和 yy 之间。 </p><p>　　３、更改资料： </p><p>update table_name <br />set column1='xxx' <br />where conditoins </p><p>　　说明： </p><p>　　1.更改某个栏位设定其值为'xxx'。 </p><p>　　2.conditions 是所要符合的条件、若没有 where 则整个 table 的那个栏位都会全部被更改。 </p><p>　　４、删除资料： </p><p>delete from table_name <br />where conditions </p><p>　　说明：删除符合条件的资料。 </p><p>　　说明：关于where条件后面如果包含有日期的比较，不同数据库有不同的表达式。具体如下： </p><p>　　(1)如果是access数据库，则为：where mydate&gt;#2000-01-01# </p><p>　　(2)如果是oracle数据库，则为：where mydate&gt;cast('2000-01-01' as date) 或：where mydate&gt;to_date('2000-01-01','yyyy-mm-dd') <br />在delphi中写成： </p><p>thedate='2000-01-01'; <br />query1.sql.add('select * from abc where mydate&gt;cast('+''''+thedate+''''+' as date)'); </p><p>　　如果比较日期时间型，则为： </p><p>where mydatetime&gt;to_date('2000-01-01 10:00:01','yyyy-mm-dd hh24:mi:ss'); </p><img src ="http://www.cnitblog.com/toserver/aggbug/14884.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/toserver/" target="_blank">toserver</a> 2006-08-07 22:47 <a href="http://www.cnitblog.com/toserver/archive/2006/08/07/14884.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>