SQL基础语法

来源:互联网 发布:淘宝id是什么 编辑:程序博客网 时间:2024/04/30 02:01

select, order by, desc, asc, where, between and,is null,or,and,in,not in,like,%, _,distinct,
left/right/full outer join on

SQL(structured query language)
SQL 语句对大小写不敏感
SQL分类:
DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)
DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)
DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)

SELECT [DISTINCT] 列名称 FROM 表名称
INSERT INTO 表名称 VALUES (值1, 值2,....)
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
DELETE FROM 表名称 WHERE 列名称 = 值


一、基础
1、说明:创建数据库
CREATE DATABASE database-name
2、说明:删除数据库
drop database dbname
3、说明:备份sql server
--- 创建 备份数据的 device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:/mssql7backup/MyNwind_1.dat'
--- 开始 备份
BACKUP DATABASE pubs TO testBack
4、说明:创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [defult value] [not null],..)
根据已有的表创建新表:
A:create table tab_new like tab_old (使用旧表创建新表)
B:create table tab_new as select col1,col2… from tab_old;

5、说明:删除新表
drop table tabname
6、说明:增加一个列
Alter table tabname add column col type
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
7、说明:添加主键: Alter table tabname add primary key(col)
说明:删除主键: Alter table tabname drop primary key(col)
8、说明:创建索引:create [unique] index idxname on tabname(col….)
删除索引:drop index idxname
注:索引是不可更改的,想更改必须删除重新建。
9、说明:创建视图:create view viewname as select statement
删除视图:drop view viewname
10、说明:几个简单的基本的sql语句
选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
总数:select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1


12、说明:使用外连接
A、left outer join:
左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
B:right outer join:
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
C:full outer join:
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。

二、提升

1、说明:子查询(表名1:a 表名2:b)
select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)

2、说明:外连接查询(表名1:a 表名2:b)
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

3、说明:在线视图查询(表名1:a )
select * from (SELECT a,b,c FROM a) T where t.a > 1;

4、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 数值1 and 数值2

5、说明:in 的使用方法
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)

6、说明:两张关联表,删除主表中已经在副表中没有的信息
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

7、说明:四表联查问题:
select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

8、说明:初始化表table1
TRUNCATE TABLE table1



三、技巧与要注意地方
1、1=1,1=2的使用,在SQL语句组合时用的较多
"where 1=1" 是表示选择全部   "where 1=2"全部不选,

主键(复合主键)
主键值不允许相同,不能为空,不允许修改,主键值不能重用

SQL 使用单引号来环绕文本值

order by 应该为sql语句的最后一个子句
DESC(desccendting) 降序
ASC(ascending) 升序

where 字句允许的操作符
"=","<>"不等,"!=","<","<=","!<",">",">=","!>",BETWEEN AND,LIKE

多个where字句可以通过and or 来联合使用

in指明数据在指定的括号内,in的执行速度比or要快,in可以包含字句
与not组合(not in)功能强大,但Mysql不支持not格式
Mysql用exists(not exists)来代替in(not in)

通配符
%代表任意字符任意个数
_代表单个任意字符

[charlist]字符列中的任何单一字符 "Persons" 表中选取居住的城市以 "A" 或 "L" 或 "N" 开头的人'[ALN]%'
[^charlist]

常见文本函数
LEFT(),RIGHT(),LENGTH(),LOWER(),UPPER(),LTRIM(),RTRIM(),TRIM()

聚合函数
Avg(),count(),max(),min(),sum()
Avg()函数会忽略列值为NULL的行
Count函数2中使用方式
Count(*)对表中的行数进行统计,不管列值为null还是非空值
Count(column)对特定的列统计,忽略NULL值
上述聚合函数可以使用DISTINCT关键字
Select avg(distinct prod_price) as avg_price from productors where vend_id=’dll01’
分组;

Group by 使用规则
如果分组列中具有NULL值,则NULL值作为一个返回
Group by 出现在where字句之后,order by 之前
过滤分组having
与where区别 where 过滤行,having 过滤分组
Where在数据分组前进行过滤,having在数据分组后进行过滤
应该仅在与group by字句中结合才使用having
Select 子句顺序
Select from where group by having order by
 
子查询
select 语句中,子查询总是从内向外处理。子查询最常用的是使用where子句的in操作符

联合表
关联查询时,应采用完全限定名(表名.列名)
笛卡尔积
由没有联结条件的表关系返回的结果是笛卡尔积。检索出的行数是将是第一个表的行数乘以第二个表的行数。

内联,基于两表之间的相等测试。
select vend_name,prod_name,prod_price from vendors ,products where vendors.vend_id=products.vend_id;
select vend_name,prod_name,prod_price from vendors inner join products on vendors.vend_id=products.vend_id;
采用这中方式联合条件用On子句而不是where子句

使用别名
在oracle中没有as关键字,在oracle中使用别名直接指定即可

outer join on 语法
必须使用right或left或full关键字指定包含其所有行的表

insert into
插入检索的数据
insert into select
insert into customer select * from custNew;

update
使用update语句应注意不要省略where子句,否则会更新所有行
使用delete语句应注意不要省略where子句,否则会删除所有行

删除表的所有记录,更快的删除方式truncate table;


MySQL 语法
SELECT column_name(s) FROM table_name LIMIT number
SELECT * FROM Persons LIMIT 5

Oracle 语法
SELECT column_name(s) FROM table_name WHERE ROWNUM <= number

原创粉丝点击