1. 删除数据
DELEE
语句的基本语法形式如下:
DELETE
语句可以从一个表中删除一行或多行数据
DELETE
FROM table_or_name
WHERE search_condition
如果需要删除表结构,那么应该使用DROP TABLE
语句。
当使用TRUNCATE TABLE
语句删除表中的数据时,系统立即释放表中数据和索引所占的空间,并不把这种数据的变化记录在日志中。
2. 检索数据
为了提高SELECT
语句检索结果的可读性,可以通过在SELECT
关键字后面增加文字串。通常情况下,使用单引号将文字串引起来。
select 'the number of',Name,'product is',Number
from product
检索结果如下:
(无列名) | Name | (无列名) | Number | |
---|---|---|---|---|
1 | the number of | apple | is | AX-111 |
2 | the number of | …. | … | .. |
改变列标题有两种方法,一种方法是使用等号=
,另一种方法是使用AS
关键字
可以在SELECT
关键字后面列出的列项中使用各种运算符和函数。
这些运算符和函数包括算术运算符、数学函数、字符串函数、日期和时间函数及系统函数等。
eg:
SELECT ISBN, Title,
BKPrice = price,
minBKPrice = price * 0.7,
maxBKPrice = price * 1.2
FROM books
2.1 日期和时间函数的一些例子:
1.getdate()
//输出当前日期和时间
print getdate();
结果:
03 21 2017 2:33PM
2.convert()
//按格式输出时间
print convert(varchar(10),getdate(),120);
结果
2017-03-21
其中120
是设置格式的style
,详细如下:
http://www.w3school.com.cn/sql/func_convert.asp
3.datediff()
//比较时间的不同
print datediff(year,'2009-04-01',getdate());
print datediff(month,'2009-04-01',getdate());
print datediff(day,'2009-04-01',getdate());
print datediff(hh,'2009-04-01',getdate());
结果:
8
95
2911
69878
4.datepart()
//取出时间的一部分
print datepart(dd,getdate());
print datepart(mm,getdate());
print datepart(yy,getdate());
结果:
21
3
2017
2.2 all和distinct
SELECT ALL pressName
FROM books
ALL
关键字表示检索所有的数据,包括重复的数据行
SELECT DISTINCT pressName
FROM books
DISTINCT
关键字表示仅仅显示那些不重复的数据行,重复的数据行只是显示一次
由于ALL关键字是默认值,所以当没有显式使用ALL
或DISTINCT
关键字时,隐含着使用ALL关键字
3. 更改
alter
用于在已有的表中添加、修改或删除列。
eg:
1. 更改表中某一列的数据类型
alter table books alter column publishdate smalldatetime;
将表books
中的pulishdate
列的数据类型更改为smalldatetime
(将时间精致到00:00:00)
具体语法:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
2.在表中添加新的一行
alter books
add money decimal(3,2)
具体语法:
ALTER TABLE table_name
ADD column_name datatype
3.删除一行
alter books
drop column money
具体语法:
ALTER TABLE table_name
DROP COLUMN column_name
学习资料:
ALTER:http://www.w3school.com.cn/sql/sql_alter.asp
sql常用函数:http://blog.youkuaiyun.com/wang379275614/article/details/8817873