您的位置:首页 > 数据库 > MySQL

mySQL学习入门教程——3.SQL的设计和编写

2017-07-15 10:22 495 查看
一、SQL语句分类

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

数据操作语言(DML):【和表中的数据记录有关的语言】
用于操作数据库对象中所包含的数据。例如:INSERT、UPDATE、DELETE语句。

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

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

二、SQL语句应用案例
1、DDL【可以按照以下格式写入一个文件,然后再粘贴到MySQL数据库中】
create table if not exists cats (
id int not null auto_increment,
pid int not null default '0',
name varchar(30) not null default '',
desn text not null default '',
primary key(id),
index name(name,pid)
);
create table if not exists products(
id int not null auto_increment,
cid int not null default 0,
name varchar(60) not null default '',
price double (7,2) not null default 0.00,
num int not null default 0,
desn text,
ptime int not null default 0,
primary key(id),
key pname(name,price)
);

2、DML
a)insert插入表数据
insert into 表名([字段列表])values(值列表),(值列表2),(值列表3),...,(值列表n);
特点:
1.如果在表名后没有给出字段列表,则值列表必须填充所有字段的值,必须按表中默认的顺序插入
2.所有需要写字段名的地方都不加单引号或双引号,但是建议所有值都要以字符形式使用
3.建议在插入数据时,最好给出字段列表,则值只要和字段列表一一对应即可,可以不按表中字段的顺序
b)update 表名 set字段='值'[,字段2='值2',...,字段n='值n'][条件]#条件指定需要更改的记录
update cats set pid='3' where id='1';
update cats set pid='99' where id >= '1' && id <= '3';

c)delete from 表名 [条件]
delete from cats; #清空数据表
truncate cats; #也可以清空数据表,效率更高,truncate将...截断

d)where条件
无论更新、删除、查找,只要写对条件就能准确找到要管理的一条或多条数据
【都可以使用各种运算符号,可以把字段当作一个变量来使用】

3、DQL【select】
SELECT[ALL | DISTINCT]  //distinct有区别的,无重复的字段
{*|table.*|[table.]field1 [as alias1][,[table.]field2[as alias2]][.....]}
FROM 表名
[WHERE...]
[GROUP BY...]
[HAVING...]
[ORDER BY ...]
[LIMIT count]
使用SELECT查询语言,目的是可以按用户的想法将数据查出来,将结果返回!

 

1、字段要列出要查询的字段
e.g. select name,price from products;
select price,name from products;
select * from products;
select products.* from products; #单表其实不需要使用表名

2、可以为每个字段起个别名【后面会用到(关键字,多表查询)】【表也可起别名(多表查询)】
e.g. select name as bookname,price as bookprice from products;#使用别名,也可不加as;注意别名中有空格时,需要加单引号;

3、使用distinct作用与整个记录,取消重复的数据,只返回一个,而不是单独的一列
e.g. select distinct price 'book price' from products;

4、在SQL语句中使用表达式的列(可以使用算术运算符,条件运算符,逻辑运算符...)
e.g. select 1+2*3;
select 8%5
update products set num = num + 1 where id = 22;
select name,price,price*0.7 as 'discount price' from products where id <=15;
5、WHERE可以在SELECT/UPDATE/DELETE中
a)可使用的逻辑运算符号(将多个条件组合)
&&/AND ||/OR !/NOT
b)可使用的比较运算符号
=  #判断是否相等,与程序中的==作用相同
<=>  #判断是否相等,与=一致,但可以用于与NULL比较
!= / <>   #不等号
<
<=
>
>=
c)程序中没有的运算符
IS NULL   #与'<=>NULL' 相等
IS NOT NULL
BETWEEN ... AND ...
e.g.

select * from products where id between 10 and 20;
与 “select * from products where id >= 10 && id <= 20;”作用相同
NOT BETWEEN ... AND ...
IN
e.g.

select * from products where id in(5,10,15,20);
update products set num = 77 where id in(5,10,15,20);
delete from 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的数据

NOT LIKE
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结尾的数据

6、多表查询(连接查询),比较常用 #ambiguous
e.g.

select cats.name,products.name from cats,products;
select c.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p;

#将A表中的记录与B表中的记录依次匹配,得到A*B种结果【笛卡尔乘积】,该结果是没有意义的。
select c.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p where c.id=p.cid;
select c.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p where c.id=p.cid and c.id=3;
select a.id aid,a.name aname,b.id bid,b.name bname from cats a,cats b; #将单表分为多表,进行查询
select a.id aid,a.name aname,b.id bid,b.name bname from cats a,cats b where a.pid = b.id;

7、嵌套查询子查询
e.g. select * from products where cid in(select id from carts where name regexp'^j') ;
select * from products where cid in(select id from carts where name like'j%'); #作用相同

8、order by 字段 [asc正序]desc倒序
e.g. select* from order by name;
select* from order by price;  #按价格非递减排序
select* from order by price desc;  #递减排序
select* from where cid > 5 order by price desc;   #与where结合使用
9、limit count【限制显示个数】
e.g. select * from limit 7;
select * from order by id desc limit 7;
select * from where id < 10 order by id desc limit 7;
select * from where id > 14 order by id asc limit 0,1;   #limit0,1表示从第0个开始取,取1个

10、group by 字段【分组】(聚合函数:对一组值执行计算并返回单一的值)
常用函数:
count() #一个字段的总数:count(1)
sum()
avg()#平均值
max()
min()
e.g.

select count(*),sum(price),avg(price),max(price),min(price) from products;
select cid,count(price),sum(price),avg(price),max(price),min (price) from products group by cid;
select cid,count(price),sum(
4000
price),avg(price),max(price),min (price) from products group by cid having avg(price) > 50;  #加having条件,与where类似
#having必须与group by结合才能使用

 

http://blog.csdn.net/zjf280441589/article/details/19496417

http://blog.csdn.net/zjf280441589/article/details/19496527

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: