首先我们先来说说数据库中DML、DDL、DCL。
DML(data manipulation language):
它们是SELECT、UPDATE、INSERT、DELETE,就象它的名字一样,这4条命令是用来对 数据库 里的数据进行操作的语言。
DDL(data definition language):
DDL比DML要多,主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定义或改变表(TABLE)的结构,数据类型,表之间的链接和约束等初始化工作上,他们大多在建立表时使用。
DCL(Data Control Language):
是数据库控制功能。是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句。在默认状态下,只有sysadmin,dbcreator,db_owner或db_securityadmin等人员才有权力执行DCL。
1.order by
以逆字母顺序显示公司名称,并以数字顺序显示顺序号:
select Company, OrderNumber from Orders order by Company desc, OrderNumber asc;
2.update
更新某一行的多列
update 表名称 set 列1名称=新值1,列2名称=新值2 where 列名称=某值
3.distinct
关键词 DISTINCT 用于返回唯一不同的值
select distinct Company from Orders;
4.delete
删除所有行,但表的结构、属性和索引都是完整的;
delete from table_name or delete * from table_name
1.top 规定要返回记录的数目,与MySQL的limit用法和Oracle的rownum用法类似
select top 2 * from student; //返回前两条记录
= MySQL:select * from student limit 2;
= Oracle: select * from student where rownum <= 2;
select top 50 * percent from student; //返回50%的记录
2.like
select * from student where name like 'q%' //以q开头
select * from student where name like '%q' //遍历以q结尾的
select * from student where name like '%q%' // 遍历该列包含q的
select * from student where name not like 'q%' //遍历该列不以q开头的记录
select * from student where name like '_ming' //name第一个字符之后是‘ming’的学生
select * from student where name like 'D_m_ng' //name第一个字符是D,之后一个字符随意,后一个字符是m,m后一个字符 随意,以ng结尾的学生
select * from student where name like '[ALN]%' // name以A或L或N开头的学生
select * from student where name like '[!ALN]%' // name不以A或L或N开头的学生
3. in 比较简单,在where语句中规定多个值
SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,...)
4.between
操作符 BETWEEN ... AND 会选取介于两个值之间的数据范围。这些值可以是数值、文本或者日期
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2 (有些数据库会包括value2,有些数据库不包括value2的值)
SELECT column_name(s) FROM table_name WHERE column_name not BETWEEN value1 AND value2 //不在value1和value2之间
5.Aliases as 别名
select * from student as s;
select name as n from student;
6.JOIN
SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name
SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name
7.UNION
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。
SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2; //默认地,UNION 操作符选取不同的值,即重复的就会删除;
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2; //UNION ALL 命令和 UNION 命令几乎是等效的,不过 UNION ALL 命令会列出所有的值。
8.Constraints 约束 一般与Create Table 语句并用 (因为约束常用在建表时,测试一般不需要建表,估有时间再整理)
常用有以下几种约束
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
MYSQL:
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (Id_P)
)
Oracle:
CREATE TABLE Persons
(
Id_P int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
9. alter
ALTER TABLE 语句用于在已有的表中添加、修改或删除列。
与update的区别是,alter是改变表结构,而update是改变表中某列的值
10.Nulls 判断某个字段是否为空,或不为空
SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NULL;
SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NOT NULL;
SQL 函数
合计函数 Aggregate 函数的操作面向一系列的值,并返回一个单一的值。
1.avg(返回某列平均值)
SELECT Customer FROM Orders
WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders)
2.count
SELECT COUNT(*) AS NumberOfOrders FROM Orders 返回所有行的个数
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders 返回该列不重复个数
3.first 返回该列第一个记录
SELECT FIRST(column_name) FROM table_name
4.last 返回该列最后一个记录
SELECT LAST(column_name) FROM table_name
5.max 返回该列最大值
SELECT MAX(column_name) FROM table_name
6.min 返回该列最小值
SELECT MIN(column_name) FROM table_name
7.sum 返回该列所有值得总数
SELECT SUM(column_name) FROM table_name
8.group by
GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
但这个where语句可有可无,如:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
对于一列以上应用group by ,写法如下:
SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders
GROUP BY Customer,OrderDate
9.having
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
10 format 对字段进行格式化
SELECT FORMAT(column_name,format) FROM table_name
SELECT ProductName, UnitPrice, FORMAT(Now(),'YYYY-MM-DD') as PerDate
FROM Products //显示每天日期所对应的名称和价格(日期的显示格式是 "YYYY-MM-DD")。