Windows server 2016——SQL server T-SQL query statement

  • About the author: A cloud computing network operation and maintenance personnel, sharing the technology and dry goods of network and operation and maintenance every day. 

  • Official account: Network Dou

  •  Motto: Keep your head down and hurry on your way, be respectful

  • Personal homepage:  Homepage of Netdou

Table of contents

written in front

introduce

1. Introduction to SQL

1.SQL和T-SQL

2. Composition of T-SQL

2. Use T-SQL statements to manipulate data tables

1. Insert data

2. Update data

EDIT 3. Delete data

(1) DELETE statement

(2) Truncate Table statement

(3) The difference between Delete and Truncate table

3. Use T-SQL statement to query data

1.select syntax structure

2. Conditional expressions

3. Logical expressions

4. Query columns

 5. Change the query result set column name

6. Query result sorting

7. Use SELECT to generate new data  


written in front

This series of articles will explain the server T-SQL query statement in SQL server, and will synchronize the video to explain the installation.

Video Tutorial: T-SQL Query Statement Tutorial

Previous review: Windows server 2016 - SQL server database and table management


introduce

SQL Server is a relational database management system developed by Microsoft, and it is now one of the mainstream databases in the world. It has the advantages of ease of use, good scalability, and a high degree of integration of related software. It can be run from a single laptop or based on a high-power cloud server cluster, or any way in between.

1. Introduction to SQL

1.SQL和T-SQL

SQL (Structured Query Language)

  • Standard language for relational databases
  • non-procedural language
  • unified language

T-SQL is the abbreviation of Transact-SQL, which is an enhanced version of SQL on Microsoft SQL Server, and it is the main language used to allow applications to communicate with SQL Server. T-SQL provides standard SQL DDL and DML functions, plus extended functions, system stored procedures, and programming structures (such as IF and WHILE) to make programming more flexible.


2. Composition of T-SQL

DML: Data Manipulation Language

  • Query, insert, delete and modify data

DDL: Data Definition Language

  • Create a database, database objects and define its columns

DCL: Data Control Language

  • Control the storage permissions of database components, storage permissions, etc.

2. Use T-SQL statements to manipulate data tables

1. Insert data

insert [INTO] <表名> [列名] values <值列表>
#      可选    必须   可选
  • If [ column name ] is omitted , the < list of values > matches the order of the fields in the table
  • Multiple column names and multiple value lists separated by commas

example:

Insert a row of data into the employee table

insert into employee (姓名, 身份证号, 职务, 出生日期, 基本工资) 
    VALUES ('郭靖', '111222333444555666', 
                    '运维工程师, '1995/1/1', 8000)

2. Update data

UPDATE  <表名>  SET  <列名 = 更新值>  [WHERE  <更新条件>]
#                                    可选,用来限制更新条件
  •  If the WHERE clause is omitted, all data rows in the table will be updated

example:

Change the basic salary of Huang Rong in the employee table to 11000

update employee SET 基本工资='11000' 
WHERE 姓名='黄蓉'

3. Delete data

(1) DELETE statement

DELETE  FROM  <表名>  [WHERE <删除条件>]
#                     可选的, 用来限制删除条件

  • If the WHERE clause is omitted, all data rows in the table will be deleted

example:

Delete the record of Yang Guo in the employee table

DELETE FROM employee WHERE 姓名='杨过'

(2) Truncate Table statement

Truncate  table <表名>

 example:

Delete all records in the employee table

Truncate  table  employee

(3) The difference between Delete and Truncate table

conditional delete

record transaction log

reset identifier column

foreign key constraints

Delete

Delete by condition using where clause

Yes, data can be recovered

no

Can be used for tables with foreign key constraints

Truncate table

Can only clear the entire table

No, the data cannot be recovered

Reset identifier column to 0

Cannot be used for tables with foreign key constraints

  • Truncate Table executes faster and is used to clear large data tables
  • Make sure the data can be deleted before executing Truncate Table

3. Use T-SQL statement to query data

1.select syntax structure

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] ]  指定查询结果的排序方式

2. Conditional expressions

  • Not equal to: <> or ! =
  • Specify the range of values: between... and ....
  • Is it empty: isnull
  • Fuzzy query: like, often used with wildcards % and _.
  • Inside the data range: in()

comparison operator

meaning

=

equal

>

more than the

<

less than

>=

greater than or equal to

<=

less than or equal to

<>

not equal to

!=

not equal to

BETWEEN

Specify the inclusive range ( inclusive boundary ) of the value , use And to separate the start value and end value

IS [Not] NULL

Specifies whether to search for null or non-null values

LIKE

Fuzzy query, pattern matching with the specified string

IN

Is it in the data range

3. Logical expressions

Join conditions with logical operators

The result of the operation is a logical value

  • TRUE or FALSE

Logical Operators

meaning

AND

Combines two conditions and evaluates to True if both conditions are true

OR

Combines two conditions and evaluates to True if one of the two conditions is True

NOT

Used with other operators, the negated operation

4. Query columns

query all columns in the table

SELECT * FROM table_name

Query all employee information in the employee table,

SELECT * FROM employee

example:

Query the content of the name, title, and basic salary columns in the employee table

SELECT 姓名,职务,基本工资 FROM employee

 Query specific rows in the table - conditional query

SELECT select_list FROM table_name WHERE search_conditions

example:

Query the names of all operation and maintenance engineers

SELECT 姓名 FROM employee WHERE 职务=’运维工程师’

Query all information of employees whose basic salary is 8000~10000

SELECT * FROM employee 
WHERE 基本工资 BETWEEN 8000 AND 10000

 Query all information of employees whose basic salary is <10000 or >20000

SELECT * FROM employee 
WHERE 基本工资<10000 OR 基本工资>20000

 Query all the information of the employees whose basic salary is 8000 , 9000 and 1000

SELECT * FROM employee WHERE 基本工资 IN (8000,9000,10000)

 Query all information of employees whose ID number starts with 66

SELECT * FROM employee WHERE 身份证号 LIKE “66%”

 Query the information of the operation and maintenance engineer surnamed Yang

SELECT * FROM employee 
WHERE 姓名 LIKE '杨%' AND 职务=’运维工程师’

 Query all information of employees whose remarks are not empty

SELECT * FROM employee WHERE 备注 is not NULL

Query the data of the first 5 rows in the employee table 

SELECT top 5 * FROM employee

 5. Change the query result set column name

SELECT column_name AS column_alias    FROM table_name
#                  改变结果集的列名称

List:

Query the data in the two columns of name and ID number in the employee table

SELECT 姓名 AS name, 身份证号 as idcard  FROM employee

6. Query result sorting

SELECT select_list 
FROM table_name 
ORDER BY column_name [  ASC  |   DESC ]
#                      升序      降序

The default is ascending ( ASC ) sorting 

example:

Query all employee information in the employee table, and display the query results according to the basic salary from high to low

SELECT * FROM employee ORDER BY 基本工资 DESC

deduplication

SELECT DISTINCT column_name FROM table_name

Query all positions of employees in the employee table 

SELECT DISTINCT  职务 FROM employee

7. Use SELECT to generate new data  

SELECT using the INTO keyword
SELECT select_list  INTO new_table_name #把一个表中的数据经过筛选插入到另一个表中
FROM table_name

example:

Create a new table new01 with the names, ID numbers and positions of all employees in the employee table

SELECT 姓名,身份证号,职务 INTO new01 FROM employee

Use the UNION keyword

INSERT  INTO table-name  [column_name] 
SELECT select_list1   UNION
SELECT select_list2   UNION
……
SELECT select_listn

UNION combines multiple different data or query results into a new result set

Save the names, titles and dates of birth of all employees in the employee table, as well as the information about the newly entered 2 employees, to the new table new03

INSERT INTO new03 (姓名,职务,出生日期) 
SELECT '欧阳锋','人事经理','1988-08-08' UNION
SELECT '一灯','财务经理','1977-07-07' UNION
SELECT 姓名,职务,出生日期 FROM employee

Practical case

Material: SQL server 2008 material

  • 3. Query all employee information in the employee table
  • 4. Query the contents of the name, title, and basic salary columns in the employee table
  • 5. Query the names of all operation and maintenance engineers
  • 6. Query all information of employees whose basic salary is 8000~10000
  • 7. Query all information of employees whose basic salary is <10000 or >20000
  • 8. Query all information of employees whose basic salary is 8000, 9000 and 1000
  • 9. Query all information of employees whose ID number starts with 66
  • 10. Query the information of the operation and maintenance engineer surnamed Yang
  • 11. Query all information of employees whose remarks are not empty
  • 12. Query the data of the first 5 rows in the employee table
  • 13. Query the data in the two columns of "name" and "ID card number" in the employee table, the name of the query result "name" column is displayed as "name", and the name of the "ID card number" column is displayed as "idcard"
  • 14. Query all employee information in the employee table, and display the query results according to the basic salary from high to low
  • 15. Query which jobs are in the employee table (remove duplicate jobs)
  • 16. List in the employee table that the third digit from the left of the ID number is 0. Except for the CTO, the name, ID number, position and basic salary of all employees, where the name field is displayed as name, and the query results are in accordance with Base salaries are ranked from high to low.
  • 17. Create a new table new01 with the names, ID numbers and positions of all employees in the employee table
  • 18. Save the names, titles and dates of birth of all employees whose basic salary is greater than or equal to 15,000 in the employee table to the new table new02. (Create table new02 in advance)
  • 19. Save the names, titles and dates of birth of all employees in the employee table, as well as the relevant information of the two newly entered employees, to the new table new02. (Create table new02 in advance)
  • The information of the newly entered 2 employees is as follows:
  •     'Ouyang Feng','Personnel Manager','1988-08-08'
  •     'One Light', 'Financial Manager', '1977-07-07'

Guess you like

Origin blog.csdn.net/yj11290301/article/details/132646988
Recommended