设为首页 加入收藏

TOP

Mysql研究之SQL语言的设计与编写完全解析(一)
2014-11-23 23:40:24 来源: 作者: 【 】 浏览:11
Tags:Mysql 研究 SQL 语言 设计 编写 完全 解析

一、SQL语句分类

数据定义语言(DDL):

用于定义和管理数据对象,包括数据库、数据表、视图、索引等。例如:CREATE、DROP、ALTER等语句。

数据操作语言(DML):【和表中的数据记录有关的语言】

用于操作数据库对象中所包含的数据。例如:INSERT、UPDATE、DELETE语句。

数据查询语言(DQL):

用于查询数据库对象中所包含的数据,能够进行单表查询、连接查询、嵌套查询,以及集合查询等各种复杂程度不同的数据库查询,并将数据返回到客户机中显示。例如:SELECT语句(占60%)。

数据控制语言(DCL):

是用来管理数据库的语言,包含管理权限及数据更改。例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句。

二、SQL语句应用案例

1、DDL【可以按照以下格式写入一个文件,然后再粘贴到MySQL数据库中】

createtable if not exists cats (

idint not null auto_increment,

pidint not null default ’0′,

namevarchar(30) not null default ”,

desntext not null default ”,

primarykey(id),

indexname(name,pid)

);

createtable if not exists products(

idint not null auto_increment,

cidint not null default 0,

namevarchar(60) not null default ”,

pricedouble (7,2) not null default 0.00,

numint not null default 0,

desntext,

ptimeint not null default 0,

primarykey(id),

keypname(name,price)

);

2、DML

a)insert,插入表数据

insertinto 表名([字段列表])values(值列表),(值列表2),(值列表3),…,(值列表n);

特点:

1.如果在表名后没有给出字段列表,则值列表必须填充所有字段的值,必须按表中默认的顺序插入

2.所有需要写字段名的地方都不加单引号或双引号,但是建议所有值都要以字符形式使用

3.建议在插入数据时,最好给出字段列表,则值只要和字段列表一一对应即可,可以不按表中字段的顺序

b)update表名 set字段=’值‘[,字段2='值2',...,字段n='值n'][条件]#条件指定需要更改的记录

e.g. updatecats set pid=’3′ where id=’1′;

updatecats set pid=’99′ where id >= ’1′ && id <= ’3′;

c)deletefrom 表名 [条件]

deletefrom cats; #清空数据表

truncatecats; #也可以清空数据表,效率更高,truncate将…截断

d)where条件

无论更新、删除、查找,只要写对条件就能准确找到要管理的一条或多条数据

【都可以使用各种运算符号,可以把字段当作一个变量来使用】

3、DQL【select】

SELECT[ALL | DISTINCT]

{*|table.*|[table.]field1[asalias1][,[table.]field2[as alias2]][.....]}

FROM 表名

[WHERE...]

[GROUPBY...]

[HAVING...]

[ORDERBY ...]

[LIMITcount]

使用SELECT查询语言,目的是可以按用户的想法将数据查出来,将结果返回!

SELECT[ALL | DISTINCT] #distinct 明显的,清楚的,有区别的

{*|table.*|[table.]field1[asalias1][,[table.]field2[as alias2]][.....]} #alias 别名,化名

FROM 表名

[WHERE...]

[GROUPBY...]

[HAVING...]

[ORDERBY ...]

[LIMITcount]

使用SELECT查询语言,目的是可以按用户的想法将数据查出来,将结果返回!

1、字段要列出要查询的字段

e.g. selectname,price from products;

selectprice,name from products;

select* from products;

selectproducts.* from products; #单表其实不需要使用表名

2、可以为每个字段起个别名【后面会用到(关键字,多表查询)】【表也可起别名(多表查询)】

e.g. selectname as bookname,price as bookprice from products;#使用别名;也可不加as;注意别名中有空格时,需要加单引号;

3、使用distinct作用与整个记录,取消重复的数据,只返回一个,而不是单独的一列

e.g. selectdistinct price ‘book price’ from products;

4、在SQL语句中使用表达式的列(可以使用算术运算符,条件运算符,逻辑运算符…)

游戏编程网www.cgzhw.com有详细的说明,这里就不再重复了。

5、WHERE可以在SELECT/UPDATE/DELETE中

a)可使用的逻辑运算符号(将多个条件组合)

&&/AND ||/OR !/NOT

b)可使用的比较运算符号

=#判断是否相等,与程序中的==作用相同

<=>#判断是否相等,与=一致,但可以用于与NULL比较

!=/ <> #不等号

<

<=

>

>=

c)程序中没有的运算符

ISNULL #与‘<=>NULL’ 相等

ISNOT NULL

BETWEENAND

e.g. select* from products where id between 10 and 20;

与 “select* from products where id >= 10 && id <= 20;”作用相同

NOTBETWEEN AND

IN

e.g. select* from products where id in(5,10,15,20);

updateproducts set num = 77 where id in(5,10,15,20);

deletefrom products where id in(5,10);

d)模糊查询

LIKE _(任意一个字符)和%(0个或多个任意字符)两个通配符号

e.g. select* from products where name like ‘______’; #查找任意名字为6个字符的数据

select* from products where name like ‘%java%’; #查询名字中包含有java的数据

NOTLIKE

e.g. select* from products where name not like ‘%java%’; #查询名字中不包含java字样的数据。

REGEXP/RLIKE【正则表达式】 #RegExp 正则表达式

e.g. select* from products where name regexp ‘^java’; #查找所有以java开头的数据

select* from products where name regexp ‘s$’; #查找所有以s结尾的数据

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇记一次mysql的调优 下一篇C语言连接MySQL数据库(课程设计..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: