SQLServer优化大全\SQL语句精解-数据库经典教材.pdf
### SQL Server 优化大全与SQL语句精解 #### 数据库操作基础 1. **创建数据库**: - **语法**:`CREATE DATABASE database-name;` - **说明**:此命令用于创建一个新的数据库。 - 示例:`CREATE DATABASE MyDatabase;` 2. **删除数据库**: - **语法**:`DROP DATABASE db_name;` - **说明**:此命令用于删除一个已存在的数据库。 - 示例:`DROP DATABASE MyDatabase;` 3. **备份数据库**: - **语法**:使用`sp_addumpdevice`存储过程来添加备份设备,并通过`BACKUP DATABASE`命令进行备份。 - 添加备份设备: ```sql USE master; EXEC sp_addumpdevice 'disk', 'testBack', 'C:\mssql7backup\MyNwind_1.dat'; ``` - 备份数据库: ```sql BACKUP DATABASE pubs TO testBack; ``` 4. **创建表**: - **语法**:`CREATE TABLE table_name (column1 data_type [NOT NULL] [PRIMARY KEY], column2 data_type [NOT NULL], ...);` - **说明**:此命令用于定义表结构。 - 示例:`CREATE TABLE Employees (ID INT PRIMARY KEY, Name VARCHAR(50) NOT NULL);` 5. **删除表**: - **语法**:`DROP TABLE table_name;` - **说明**:此命令用于删除一个表。 - 示例:`DROP TABLE Employees;` 6. **修改表结构**: - **语法**:`ALTER TABLE table_name ADD COLUMN column_name data_type;` - **说明**:此命令用于在现有表中添加新列。 - 示例:`ALTER TABLE Employees ADD Salary DECIMAL(10,2);` - **添加主键**: - **语法**:`ALTER TABLE table_name ADD PRIMARY KEY (column_name);` - **删除主键**: - **语法**:`ALTER TABLE table_name DROP CONSTRAINT PK_table_name;` 7. **创建索引**: - **语法**:`CREATE [UNIQUE] INDEX index_name ON table_name (column1, column2, ...);` - **说明**:此命令用于提高查询性能。 - 示例:`CREATE INDEX IX_Employees_Name ON Employees (Name);` 8. **删除索引**: - **语法**:`DROP INDEX index_name;` - **说明**:此命令用于删除索引。 - 示例:`DROP INDEX IX_Employees_Name;` 9. **创建视图**: - **语法**:`CREATE VIEW view_name AS SELECT statement;` - **说明**:此命令用于创建基于查询结果的视图。 - 示例:`CREATE VIEW EmployeeList AS SELECT ID, Name FROM Employees;` 10. **删除视图**: - **语法**:`DROP VIEW view_name;` - **说明**:此命令用于删除视图。 - 示例:`DROP VIEW EmployeeList;` #### SQL 基本操作 1. **基本的SQL语句**: - **选择记录**: - **语法**:`SELECT * FROM table_name WHERE condition;` - 示例:`SELECT * FROM Employees WHERE Department = 'Sales';` - **插入记录**: - **语法**:`INSERT INTO table_name (column1, column2) VALUES (value1, value2);` - 示例:`INSERT INTO Employees (ID, Name) VALUES (1, 'John Doe');` - **更新记录**: - **语法**:`UPDATE table_name SET column1 = value1 WHERE condition;` - 示例:`UPDATE Employees SET Salary = 50000 WHERE ID = 1;` - **删除记录**: - **语法**:`DELETE FROM table_name WHERE condition;` - 示例:`DELETE FROM Employees WHERE ID = 1;` 2. **高级查询**: - **模糊匹配**: - **语法**:`SELECT * FROM table_name WHERE column1 LIKE pattern;` - 示例:`SELECT * FROM Employees WHERE Name LIKE '%Smith%';` - **排序**: - **语法**:`SELECT * FROM table_name ORDER BY column1, column2 [DESC];` - 示例:`SELECT * FROM Employees ORDER BY Name DESC;` - **聚合函数**: - **计数**: - **语法**:`SELECT COUNT(*) AS total_count FROM table_name;` - 示例:`SELECT COUNT(*) AS total_count FROM Employees;` - **求和**: - **语法**:`SELECT SUM(column1) AS sum_value FROM table_name;` - 示例:`SELECT SUM(Salary) AS total_salary FROM Employees;` - **平均值**: - **语法**:`SELECT AVG(column1) AS avg_value FROM table_name;` - 示例:`SELECT AVG(Salary) AS average_salary FROM Employees;` - **最大值**: - **语法**:`SELECT MAX(column1) AS max_value FROM table_name;` - 示例:`SELECT MAX(Salary) AS highest_salary FROM Employees;` - **最小值**: - **语法**:`SELECT MIN(column1) AS min_value FROM table_name;` - 示例:`SELECT MIN(Salary) AS lowest_salary FROM Employees;` #### 连接操作 1. **连接类型**: - **左外连接**: - **语法**:`SELECT * FROM table1 LEFT OUTER JOIN table2 ON table1.column = table2.column;` - 示例:`SELECT * FROM Employees LEFT OUTER JOIN Departments ON Employees.DepartmentID = Departments.ID;` - **右外连接**: - **语法**:`SELECT * FROM table1 RIGHT OUTER JOIN table2 ON table1.column = table2.column;` - 示例:`SELECT * FROM Employees RIGHT OUTER JOIN Departments ON Employees.DepartmentID = Departments.ID;` - **全连接**: - **语法**:`SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.column = table2.column;` - 示例:`SELECT * FROM Employees FULL OUTER JOIN Departments ON Employees.DepartmentID = Departments.ID;` 2. **分组查询**: - **语法**:`SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name;` - **说明**:此命令用于对数据进行分组并执行聚合操作。 - 示例:`SELECT Department, COUNT(*) AS EmployeeCount FROM Employees GROUP BY Department;` 以上内容涵盖了SQL Server中数据库管理和数据操作的基础知识点,包括创建、修改、删除数据库和表的操作,以及基本的SQL查询语句,如选择、插入、更新、删除记录等。此外,还涉及了更高级的查询技术,例如连接操作和分组查询,这些技术可以帮助用户更灵活地处理和分析数据。通过这些知识点的学习,用户可以更加高效地管理数据库系统,并实现复杂的数据处理需求。
- 一一狗熊的尾巴2020-07-22我还以为是书。。。
- weioxwei2019-01-08可以用来借鉴,使用起来方便
- 粉丝: 0
- 资源: 3
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助