sql ddl中key

This article explains SQL DDL commands in Microsoft SQL Server using a few simple examples.

本文使用一些简单示例说明Microsoft SQL Server中SQL DDL命令。

SQL commands broadly fit into four categories:

SQL命令大致分为四类:

  • DDL (Data Definition Language) DDL(数据定义语言)
  • DML (Data Manipulation Language) DML(数据处理语言)
  • DCL (Data Control Language) DCL(数据控制语言)
  • TCL (Transactional Control Language) TCL(交易控制语言)

This article only covers the SQL DDL commands.

本文仅介绍SQL DDL命令。

SQL DDL命令 (SQL DDL commands)

The DDL commands in SQL are used to create database schema and to define the type and structure of the data that will be stored in a database. SQL DDL commands are further divided into the following major categories:

SQL中的DDL命令用于创建数据库架构以及定义将存储在数据库中的数据的类型和结构。 SQL DDL命令进一步分为以下主要类别:

  • CREATE 创造
  • ALTER 改变
  • DROP 下降
  • TRUNCATE 截短

创造 (CREATE)

The CREATE query is used to create a database or objects such as tables, views, stored procedures, etc.

CREATE查询用于创建数据库或对象,例如表,视图,存储过程等。

建立资料库 (Creating a database)

The following example demonstrates how the CREATE query can be used to create a database in MS SQL Server:

下面的示例演示如何使用CREATE查询在MS SQL Server中创建数据库:

CREATE DATABASE LibraryDB

The script above creates a database named “LibraryDB” in MS SQL Server.

上面的脚本在MS SQL Server中创建一个名为“ LibraryDB”的数据库。

建立表格 (Creating a table)

The CREATE query is also used to add tables in an existing database as shown in the following script:

CREATE查询还用于在现有数据库中添加表,如以下脚本所示:

USE LibraryDB
CREATE TABLE Books
(
Id INT PRIMARY KEY IDENTITY(1,1),
Name VARCHAR (50) NOT NULL,
Price INT
)

The above script creates a table named “Books” in the “LibraryDB” database that we created earlier.

上面的脚本在我们之前创建的“ LibraryDB”数据库中创建了一个名为“ Books”的表。

The “Books” table contains three columns: Id, Name, and Price. The Id column is the primary key column and it cannot be NULL. A column with a PRIMARY KEY constraint must contain unique values. However, since we have set the IDENTITY property for the Id column, every time a new record is added in the Books table, the value of the Id column will be incremented by 1, starting from 1. You need to specify the values for the Name column as well as it cannot have NULL. Finally, the Price column can have NULL values.

“书籍”表包含三列:Id,名称和价格。 Id列是主键列,不能为NULL。 具有PRIMARY KEY约束的列必须包含唯一值。 但是,由于我们为Id列设置了IDENTITY属性,因此,每当在Books表中添加一条新记录时,Id列的值就会从1开始递增1。您需要为名称列及其不能为NULL。 最后,Price列可以具有NULL值。

To view all the tables in the LibraryDB, execute the following QL DDL script:

要查看LibraryDB中的所有表,请执行以下QL DDL脚本:

USE LibraryDB
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES
GO

You should see the following output:

您应该看到以下输出:

Similarly, to see all the columns in the Books table, run the following script:

同样,要查看Books表中的所有列,请运行以下脚本:

SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='Books'

Here is the output:

这是输出:

You can see how the CREATE query can be used to define the structure of a table and the type of data that will be stored in a table. Note, we have not added any record to the Books table yet as SQL DDL commands are only concerned with the structure of the database and not with the database records. The SQL DML commands are used for inserting, modifying and deleting database records.

您可以看到如何使用CREATE查询定义表的结构以及将存储在表中的数据类型。 注意,我们尚未将任何记录添加到Books表中,因为SQL DDL命令仅与数据库的结构有关,而与数据库记录无关。 SQL DML命令用于插入,修改和删除数据库记录。

改变 (ALTER)

The ALTER command in SQL DDL is used to modify the structure of an already existing table.

SQL DDL中的ALTER命令用于修改已经存在的表的结构。

添加新列 (Adding a new column)

For example, if we want to add a new column e.g. ISBN to the existing Books table in the LibraryDB database, the ALTER command can be used as follows:

例如,如果我们要向LibraryDB数据库中现有的Books表中添加一个新列(例如ISBN),则可以按以下方式使用ALTER命令:

USE LibraryDB
ALTER TABLE Books
ADD ISBN INT NOT NULL;

The syntax of the ALTER command is straightforward. The ALTER statement is used followed by the object type and the name of the object, which in this case are TABLE and Books, respectively.

ALTER命令的语法很简单。 使用ALTER语句,后跟对象类型和对象名称,在这种情况下分别为TABLE和Books。

Next, you need to specify the operation that you need to perform, which is ADD in our case. Let’s now again SELECT the columns from the Books table and see if the ISBN column has been added to the Books table:

接下来,您需要指定所需执行的操作,在本例中为ADD。 现在,让我们再次从Books表中选择列,并查看ISBN列是否已添加到Books表中:

SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='Books'

Here is the result set:

这是结果集:

In the output, you can see the newly added ISBN column.

在输出中,您可以看到新添加的ISBN列。

修改现有列 (Modifying an existing column)

Let’s see another case where ALTER command can be useful. Suppose, instead of adding a new column to a table, you want to modify an existing column. For example, you want to change the data type of the ISBN column from INT to VARCHAR (50). The ALTER query can be used as follows:

让我们看一下ALTER命令可能有用的另一种情况。 假设要修改现有列,而不是向表中添加新列。 例如,您要将ISBN列的数据类型从INT更改为VARCHAR(50)。 ALTER查询可以按如下方式使用:

USE LibraryDB
ALTER TABLE Books
ALTER COLUMN ISBN VARCHAR(50);

You can see that to modify an existing column within a table, we first have to use the ALTER command with the table name and then another ALTER command with the name of the column that is to be modified.

您可以看到,要修改表中的现有列,我们首先必须使用带有表名的ALTER命令,然后再使用带有要修改的列名的另一个ALTER命令。

If you again select the column names, you will see the updated data type (VARCHAR) for the ISBN column.

如果再次选择列名,则将看到ISBN列的更新的数据类型(VARCHAR)。

下降 (DROP )

The DROP command is a type of SQL DDL command, that is used to delete an existing database or an object within a database.

DROP命令是一种SQL DDL命令,用于删除现有数据库或数据库中的对象。

删除数据库 (Deleting a database)

The following DROP command deletes the LibraryDB database that we created earlier:

以下DROP命令删除我们之前创建的LibraryDB数据库:

DROP DATABASE LibraryDB

Note: If you execute the above command, the LibraryDB database will be deleted. To execute the rest of the queries in this article, you will again need to CREATE the LibraryDB database, along with the Books table.

注意:如果执行上述命令,则LibraryDB数据库将被删除。 要执行本文中其余的查询,您将再次需要创建LibraryDB数据库以及Books表。

删除表格 (Deleting a table)

The DROP command is a type of SQL DDL command that is used to delete an existing table. For instance, the following command will delete the Books table:

DROP命令是一种SQL DDL命令,用于删除现有表。 例如,以下命令将删除Books表:

DROP TABLE Books

删除列 (Deleting a column)

To delete a column within a database, the DROP query is used in combination with the ALTER query. The ALTER query specifies the table that you want to delete whereas the DROP query specifies the column to delete within the table specified by the ALTER query. Let’s drop the ISBN column from the Books:

要删除数据库中的列,将DROP查询与ALTER查询结合使用。 ALTER查询指定要删除的表,而DROP查询指定要在ALTER查询指定的表内删除的列。 让我们从书籍中删除ISBN列:

ALTER TABLE Books
DROP COLUMN ISBN

截短 (TRUNCATE )

The TRUNCATE command in SQL DDL is used to remove all the records from a table. Let’s insert a few records in the Books table:

SQL DDL中的TRUNCATE命令用于从表中删除所有记录。 让我们在Books表中插入一些记录:

INSERT INTO Books
VALUES ('Book-A', 100),
('Book-B', 200),
('Book-C', 150)

Let’s see if the records have been actually inserted:

让我们看看是否实际插入了记录:

SELECT * FROM Books

Here is the result set:

这是结果集:

You can see the three records that we inserted in the Books table.

您可以看到我们在Books表中插入的三个记录。

The TRUNCATE command will remove all the records from the Books table as shown below:

TRUNCATE命令将从“书籍”表中删除所有记录,如下所示:

TRUNCATE TABLE Books

If you again select all the records from the Books table, you will see that the table is empty.

如果再次从“书籍”表中选择所有记录,则将看到该表为空。

结论 (Conclusion )

In this article, you saw how to use SQL DDL commands to create a database schema and to define and modify the structure of your database. You saw how to execute SQL DDL commands in MS SQL Server with the help of different examples. Have anything to say about the article? Please feel free to comment.

在本文中,您了解了如何使用SQL DDL命令创建数据库架构以及定义和修改数据库的结构。 通过不同的示例,您了解了如何在MS SQL Server中执行SQL DDL命令。 关于这篇文章有什么要说的吗? 请随意发表评论。

翻译自: https://www.sqlshack.com/sql-ddl-getting-started-with-sql-ddl-commands-in-sql-server/

sql ddl中key

sql ddl中key_SQL DDL:SQL Server中SQL DDL命令入门相关推荐

  1. sql分区表上创建索引_SQL Server中分区表和索引的选项

    sql分区表上创建索引 介绍 (Introduction) I work for a large, multinational financial institution. Like most com ...

  2. sql 时态表的意义_SQL Server中的时态表

    sql 时态表的意义 Temporal tables have been a feature of SQL Server since version 2016. SQL Server professi ...

  3. access 删除字符串中的字符_SQL Server中的字符串分割函数

    您是否知道从SQL Server 2016开始,系统就内置STRING_SPLIT函数,该函数用于将字符串分隔的变量拆分为一个可用列表. 对于经常需要分割字符串的技术人员,建议您查看此功能. STRI ...

  4. amp sqlserver中 什么意思_sql server中创建表时各个数据类型时什么意思??

    (1)整数型 整数包括bigint.int.smallint和tinyint,从标识符的含义就可以看出,它们的表示数范围逐渐缩小. lbigint:大整数,数范围为-263(-922337203685 ...

  5. SQL Server 中WITH (NOLOCK)浅析

    概念介绍 开发人员喜欢在SQL脚本中使用WITH(NOLOCK), WITH(NOLOCK)其实是表提示(table_hint)中的一种.它等同于 READUNCOMMITTED . 具体的功能作用如 ...

  6. 如何在SQL Server中使用触发器

    触发器是一种特殊的存储过程,在使用触发器之前可以与存储过程进行比较,触发器主要是通过事件进行触发而被执行的,而存储过程可以通过存储过程名称而被直接调用. 触发器主要优点如下: 触发器是自动的:当对表中 ...

  7. SQL Server中常用的SQL语句

    1.概述 2.查询概述 3.单表查询 4.连接查询 5.带有exists的相关子查询 6.SQL的集合操作 7.插入操作 8.删除操作 9.修改操作 10.数据定义 11.视图 1.概述 名词 笛卡尔 ...

  8. SQL Server中的角色(服务器级别和数据库级别角色)

    参考文献 http://msdn.microsoft.com/zh-cn/library/ms188659.aspx 服务器级别角色 为帮助您管理服务器上的权限,SQL Server 提供了若干角色. ...

  9. sql 触发器嵌套条件_SQL Server中的嵌套触发器

    sql 触发器嵌套条件 Nested Triggers in SQL Server are actions that automatically execute when a certain data ...

最新文章

  1. 搞死了 报错【libc-client.a: could not read symbols: ...
  2. IOS之TableViewCell重用机制避免重复显示问题
  3. 如果你想成为年入 50w+ 的架构师,我建议你先看看这个...
  4. DHCP中继代理;DHCP突破vlan限制
  5. webstorm基础使用总结
  6. super 与 this 关键字
  7. C#生成CHM文件(中级篇)
  8. 把旧系统迁移到.Net Core 2.0 日记(1) - Startup.cs 解析
  9. 标准数据库事务完整性约束
  10. html 编辑器插件安装,最新版CKEditor的配置方法及插件(Plugin)编写示例
  11. [3D数学基础:图形与游戏开发]读书笔记 第10章(3D中方位与角位移)
  12. 卫星通信中信关站、关口站、地面终端的区分
  13. VXLAN技术学习笔记
  14. 中学计算机课小课题,中学信息技术课题题目
  15. QT中修改图标(图片)颜色
  16. 解密Zynga:专注 流水线 数据控
  17. Arcgis空间分布方向性分析
  18. 芯天下8bit MCU
  19. 高德开放平台地图Flutter插件的使用
  20. htons(), htonl(), ntohs(), ntohl()

热门文章

  1. H3C 典型数据链路层标准
  2. Confluence 6 示例 - https://confluence.atlassian.com/
  3. 用PredicateBuilder实现Linq动态拼接查询
  4. [目标检测]YOLO原理
  5. HDU 5136 Yue Fei's Battle
  6. javascrip学习之 数据类型和变量
  7. LeetCode(257)——二叉树的所有路径(JavaScript)
  8. oracle 截取指定时间戳,oracle时间函数(包括截取时间)
  9. 股票冲高回落意味着什么?
  10. 发生心梗后,家属做些什么才能保证患者获救,降低死亡?