SQL常用语句

1、说明:创建数据库
Create DATABASE dbname

 

2、说明:删除数据库

drop database dbname

 

3、说明:创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [notnull],..)
根据已有的表创建新表:
A:create tabletab_new like tab_old (使用旧表创建新表)只是表结构,不包括数据
B:create tabletab_new as select col1,col2… from tab_old definition only 选择一些列


4、说明:删除新表
drop table tabname


5、说明:增加一个列
Alter table tabname add column col type

比如Alter table`useritems` add column dtime DATETIME

删除一列

Alter table `useritems` drop dtime

 

6、说明:添加主键: Alter table tabname add primary key(col)

组合主键 

alter table `table 22` add primary key (user_id, item_id)

``符号是怕表,视图,列的名字中有空格,或与mysql部分保留字冲突而加的。


说明:删除主键: Altertable tabname drop primary key(col)

7、说明:创建索引:create [unique] index idxname on tabname(col….)
删除索引:dropindex idxname
注:索引是不可更改的,想更改必须删除重新建。

8、说明:创建视图:create view viewname as select * from useritems where `acttype` = 4
删除视图:drop viewviewname

 

9、说明:几个简单的基本的sql语句
查询:select *from table1 where 范围
插入:insert intotable1(field1,field2) values(value1,value2)
删除:delete fromtable1 where 范围
更新:updatetable1 set field1=value1 where 范围

排序:select *from table1 order by field1 ASC,field2 DESC

10. 统计函数

总数:selectcount * as totalcount from table1
求和:selectsum(field1) as sumvalue from table1
平均:selectavg(field1) as avgvalue from table1
最大:selectmax(field1) as maxvalue from table1
最小:selectmin(field1) as minvalue from table1

 

11.表的集合操作

A: UNION 运算符,类似并集操作
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。

UNION ALL指定不消除重复行。

 

B: EXCEPT 运算符

EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。EXCEPT ALL不消除重复行。

 

C: INTERSECT 运算符 类似求交集
INTERSECT 运算符通过只包括TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。INTERSECT ALL不消除重复行。


注:使用运算词的几个查询结果对应的列必须是一致的。不然让别人怎么合并啊?

 

12、说明:使用连接

A、left outer join:
左外连接(左连接):以左边表为基准。包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行。如果右边表没有匹配,则填上默认或NULL。
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT JOIN b ON a.a = b.c

SELECT A.name, B.address
FROM A
LEFT JOIN B ON A.id = B.A_id

 

B:right outer join:正好和左连相反。

SELECT A.name, B.address
FROM A
RIGHT JOIN B ON A.id = B.A_id

 

C:全外连 full join或full outer join:

全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。不管另外一边的表中是否存在与它们匹配的行。

select * from A full join B

 

D:内连接 inner join

内连接inner join:只连接左右表中都有,且匹配的行。也是求交集。

select a.userid, b.name from a inner join bon a.userid = b.id

等价于selecta.userid, b.name from a, b where a.userid = b.id

 

E:交叉连接

 生成笛卡尔积-它不使用任何匹配或者选取条件,而是直接将一个数据源中的每个行与另一个数据源的每个行都一一匹配

select * from a cross join b

等价于

select * from a, b

 

注意:

1. on A.id = B.id 等同于 using(id)//这里字段名要相同
2. 当 MySQL 在从一个表中检索信息时,你可以提示它选择了哪一个索引。   
如果 EXPLAIN 显示 MySQL 使用了可能的索引列表中错误的索引,这个特性将是很有用的。   
通过指定 USE INDEX(key_list),你可以告诉 MySQL 使用可能的索引中最合适的一个索引在表中查找记录行。

   
IGNORE INDEX (key_list) 可被用于告诉 MySQL 不使用特定的索引。    

 

效率问题:
inner join比left join快。
注:innerjoin  内连接等价于下面的sql:SELECT A.name, B.address FROM A, B WHERE A.id = B.A_id 

连接字段建索引可以提高效率

例子:

1、两张表都有的记录如(1, 'A'); (2, 'B'); 
select a.* from t1 a inner join t2 b on a.id=b.id and a.val=b.val
 1、表一有表二没有的记录如(3, 'C'); 
select a.* from t1 a left join t2 b on a.id=b.id and a.val=b.val
where b.id is null
 3、表二有表一没有的记录如(1, 'C','22'); (4, 'D','44'); 
select b.* from t1 a right join t2 b on a.id=b.id and a.val=b.val
where a.id is null

 

13.模糊查找,通配符查找,正则表达式查找

查找:select *from table1 where field1 like ’%value1__’

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值