Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

SQL(3) sql SQLServer SQL语句 DML语句

$
0
0
SQL(3)
DML语句

DML(Data Manipulation Language) 语句是对数据库中表记录的操作,主要包括对表的插入(insert)、删除(delete)、更新(update)、查询(select)等。

insert

语法: INSERT INTO tablename(field1,field2,…fieldn) values(value1, value2,…valuen);

语法: INSERT INTO tablename (field1,field2,…fieldn) values
(record1_value1, record1_value2,…record1_valuen),
(record2_value1, record2_value2,…record2_valuen),

(recordn_value1, recordn_value2,…recordn_valuen);
//example
//向表emp中插入记录
insert into emp (ename,hiredate,sal,deptno) values ('lisa', '2017-02-21', 5000, 1);
//可以不用制定表的字段名,但是values后面的顺序应该和表的字段顺序一致
insert into emp values ('dony', '2017-02-21', 3000, 2);
//对于空字段、非空有默认值字段以及自增字段,可以不用在字段列表出现,自动设置为NULL、默认值以及自增的数字
//emp表中 ename hiredate sal deptno都是非空字段
insert into emp (ename, sal) values ('fisher', 1000);
//一次性插入多条语句
insert into emp (ename, hiredate, sal, deptno) values \
('lisa', '2017-02-21', 5000, 1),\
('dony', '2017-02-21', 3000, 2),\
('fisher', '2017-02-21', 1000, 3);
delete
语法: DELETE FROM tablename [WHERE CONDITION]
语法: DELETE t1, t2,..tn FROM t1, t2,…tn [WHERE CONDITION]
//example
//删除emp表中deptno=2的字段
delete from emp where deptno=2;
//多表删除 删除表emp dept中deptno=2的字段
delete a, b from emp a, dept b where a.deptno=b.deptno and a.deptno=2;
update
语法: UPDATE tablename SET field1=value1, field2=value2,…fieldn=valuen [WHERE CONDITION]
语法: UPDATE t1,t2,…tn SET t1.field1=expr1, t2.field1=expr2,…tn.fieldn=exprn [WHERE CONDITION]
//example
//更新表emp中ename为lisa的sal为5000
update emp set sal=5000 where ename='lisa';
//多表更新
update emp a, dept b set a.sal=5000, b.deptname='mordor' where a.deptno=b.deptno and a.deptno=2;
select
语法: select * from tablename [WHERE CONDITION]
//example
//查询emp中的所有数据
select * from emp;
//与上面的查询语句等价
select ename,hiredate,sal,deptno from emp;
//查询单个字段
select ename from emp;
//查询emp表中deptno=1的数据
select * from emp where deptno=1;
查询不重复的记录

关键字 distinct

//example
//查询emp表中的deptno 去重
select distinct deptno from emp;
条件查询
比较运算符 =、!=、>、<、>=、<=
逻辑运算符 or、 and
排序
语法: SELECT * FROM tablename [WHERE CONDITION] [ORDER BY field1 [DESC|ASC], field2 [DESC|ASC],…fieldn [DESC|ASC]]

关键字: DESC(降序) ASC(升序) 默认为升序

//example
//将emp表的数据按照sal升序排序,deptno降序
select * from emp order by sal, deptno desc;
聚合
语法: SELECT [field1, field2,…,fieldn] fun_name FROM tablename [WHERE where_condition] \
[GROUP BY field1, field2,…,fieldn [WITH ROLLUP]] [HAVING where_condition]
//example
//查询表emp中的总人数
select count(1) from emp;
//统计各部门的人数
select deptno, count(1) from emp group by deptno;
//统计各部门的人数以及总人数
select deptno, count(1) from emp group by deptno with rollup;
//统计人数大于1的部门
select deptno, count(1) from emp group by deptno having count(1)>1;
//统计所有的薪水总额以及最高、最低薪水
select sum(sal),max(sal),min(sal) from emp;
表连接

表连接分为内连接和外连接,主要区别内连接只显示互相匹配的部分,而外连接也会选出不匹配的记录。

//example
//查询emp和dept中的ename和deptname 条件是deptno相同
select ename,deptname from emp, dept where emp.deptno=dept.deptno;
外连接分为左连接和右连接
左连接:包含所有左表中的记录甚至是右表中没有和它匹配的记录
右连接 : 包含所有右表中的记录甚至是左表中没有和它匹配的记录
//example
//左连接
select ename,deptname from emp left join dept on emp.deptno=dept.deptno;
//右连接
select ename,deptname from dept a right join dept b on a.deptno=b.deptno;
子查询

关键字: in、 not in、=、 !=、exists、not exists

//example
//从emp表中查询所有deptno在dept中的记录
select * from emp where deptno in (select deptno from dept);
记录联合
语法: SELECT * FROM t1 UNION|UNION ALL SELECT * FROM t2 …UNION|UNION ALL SELECT * FROM Tn;
union 与union all的区别:union all直接把结果合并,union 会把结果distinct。
//example
select deptno from emp union select deptno from dept;

Viewing all articles
Browse latest Browse all 3160

Latest Images

Trending Articles