On the T-SQL statements manipulate the data table

SQL is Structured Query Language, is the standard language for relational databases, various types of databases support SQL as a query language.
T-SQL is the standard SQL enhanced version, in addition to the standard SQL commands, SQL commands are also many expansion. It provides the basic functionality is similar to a programming language. As explained variable, flow control, performance function like.
When we install the database on which operations often do nothing more than plug (increase), delete, change, these four categories, today we have to talk about the four operations.
Insert data:

insert into **表名** (列名1,列名2.....)    其中into为可选项,可以省略   #多个列名和多个值列表用逗号隔开
values (列值1,列值2......)    值列表中的顺序与数据表中的字段顺序保持一致

Update data (modifying data):

update **表名** set 列名='更新值'  set后面可以紧随多个数据列的更新值
where **更新条件**  where 是可选的,用来限制条件,如果不限制,则整个表的所有数据行都将被更新

delete data:

delete from **表名 **   
where **删除条件**    如果不加删除条件,那么就是删除整个表中的所有记录
truncate  table **表名**   删除整个表的记录,执行速度更快,用于清空大数据量表
注意,使用 truncate 前要确保数据可删除

On the T-SQL statements manipulate the data table
select grammatical structure (query data):

select *select_list*   # 指定查询内容
into *new_table_name*    # 把查询结果存放到一个新表中
from *table_name*  # 指定查询源
where *search_conditions*    # 指定查询条件
group by *group_by_expression*   # 指定查询结果的分组条件
having *search_conditions*   # 指定分组搜索条件与group by 子句一起使用
order by *order_expression* [asc|desc]  # 指定查询结果的排序方式

Conditional Expression:
1, constant: represents a single specified data symbols worth
of letters, numbers or symbols
2, column name: the name of the table in column
3, unary operators: there is only one operand operator
"+" denotes a positive number, "-" represents a negative
4, binary operators: the operator performs two operand combination operators
arithmetic operators, bitwise operators, logic operators, comparison operators
On the T-SQL statements manipulate the data table
On the T-SQL statements manipulate the data table
On the T-SQL statements manipulate the data table
query example:

select * from **表名**     # 查询表中所有列
select **列名1,列名2,列名3**.....  from  **表名**  # 查询表中特定列
select  **列** from 表名 where *search_conditions* (如:职务='经理') # 查询表中特定行
select * from test where 基本工资 between 8000 and 10000 #查询test表中基本工资8000到10000之间的员工所有信息
select * from test where 基本工资<10000 or 基本工资>20000 #查询表中基本工资低于10000或高于20000的员工所有信息
select * from test where 基本工资 in (8000,9000,10000) #查询表中工资为8000,9000,和10000的员工所有信息。
select * from test where ×××号 like '66%' #查询test表中×××号以66开头的员工所有信息。
select * from test where 姓名 like '杨%' and 职务='运维工程师' #查询表中姓杨的运维工程师的信息
select * from test where 备注 is not null #查询表中备注不为空的员工所有信息。
select top 5 * from test #查询表中前5行的数据。
select * from test order by 基本工资 desc #查询test表中所有的信息,并按照基本工资从高到低显示查询结果。
select distinct 职务 from test #查询test表中有哪些职务

select into use keyword:
select 姓名,×××号,职务 into new01 from test #将test表中所有员工的姓名、×××号和职务生成一个新表new01。
INSERT use select keywords:
insert into new1 (姓名,职务,出生日期) select 姓名,职务,出生日期 from test where 基本工资>=15000 #将test表中所有基本工资大于等于15000的员工的姓名,职务,和出生日期保存到 new1表中(注意,这里的 new1表中需要提前建立)
Use the keyword union:
insert into new2 (姓名,职务,出生日期) select '张三','运维','1995-01-01' union select '李四','运维','1996-01-01' union select 姓名,职务,出生日期 from test #将test表中所有员工的姓名、职务和出生日期,以及新输入的2名员工相关信息,一起保存到新表new2

Guess you like

Origin blog.51cto.com/14227204/2408143