Coursera 笔记

Module 02 · Introduction to Relational Databases and Tables

Reading: Examples to CREATE and DROP tables阅读:创建和删除表的示例

Coursera ↗

Objective(s) 目标

At the end of this lab, you will be able to:
在本实验结束时,您将能够:

  • Create and Drop tables in the database.
    在数据库中创建和删除表。

Estimated time to complete: 5 minutes
预计完成时间:5 分钟

CREATE TABLE statement CREATE TABLE 语句

In the previous video, we saw the general syntax to create a table:
在之前的视频中,我们看到了创建表的一般语法:

CREATE TABLE TableName (
   COLUMN1 datatype,
   COLUMN2 datatype,
   COLUMN3 datatype,
...
);

Consider the following examples:
考虑以下示例:

  1. Create a TEST table with two columns - ID of type integer and NAME of type varchar. For this, we use the following SQL statement.
    创建一个 TEST 表,包含两列:ID(整数类型)和 NAME(可变字符类型)。为此,我们使用以下 SQL 语句。
CREATE TABLE TEST (
   ID int,
   NAME varchar(30)
);
  1. Create a COUNTRY table with an integer ID column, a two-letter country code column, and a variable length country name column. For this, we may use the following SQL statement.
    创建一个 COUNTRY 表,包含一个整数 ID 列、一个两字母国家代码列和一个可变长度国家名称列。为此,我们可以使用以下 SQL 语句。
CREATE TABLE COUNTRY (
   ID int,
   CCODE char(2),
   Name varchar(60)
);
  1. In the example above, make ID a primary key. Then, the statement will be modified as shown below.
    在上述示例中,将 ID 设置为主键。然后,语句将按以下方式修改。
CREATE TABLE COUNTRY (
   ID int NOT NULL,
   CCODE char(2),
   Name varchar(60),
   PRIMARY KEY (ID)
);

In the above example, the ID column has the NOT NULL constraint added after the datatype, meaning that it cannot contain a NULL or an empty value. This is added since the database does not allow Primary Keys to have NULL values.
在上面的示例中,ID 列在数据类型之后添加了 NOT NULL 约束,这意味着它不能包含 NULL 或空值。这是添加的,因为数据库不允许主键有 NULL 值。

DROP TABLE

If the table you are trying to create already exists in the database, you will get an error indicating table XXX.YYY already exists. To circumvent this error, create a table with a different name or first DROP the existing table. It is common to issue a DROP before doing a CREATE in test and development scenarios.
如果你尝试创建的表已经在数据库中存在,你会得到一个错误,提示表 XXX.YYY 已经存在。要绕过这个错误,请使用不同的名称创建表,或者首先 DROP 现有的表。在测试和开发场景中,通常在执行 CREATE 之前会发出 DROP 命令。

The syntax to drop a table is:
删除表的语法是:

DROP TABLE TableName;

For example, consider that you wish to drop the contents of the table COUNTRY if a table exists in the dataset with the same name. In such a case, the code for the last example becomes
例如,假设您希望在数据集中存在同名的表时删除表 COUNTRY 的内容。在这种情况下,最后一个示例的代码变为

DROP TABLE COUNTRY;
CREATE TABLE COUNTRY (
   ID int NOT NULL,
   CCODE char(2),
   Name varchar(60),
   PRIMARY KEY (ID)
);

WARNING: Before dropping a table, ensure it doesn't contain important data that can't be recovered easily.
警告:在删除表之前,请确保它不包含无法轻松恢复的重要数据。

Note that if the table does not exist and you try to drop it, you will see an error like XXX.YYY is an undefined name. You can ignore this error if the subsequent CREATE statement is executed successfully.
请注意,如果表不存在而您尝试删除它,您将看到类似 XXX.YYY 是未定义名称的错误。如果后续的 CREATE 语句执行成功,您可以忽略此错误。

In a hands-on lab later in this module, you will practice creating tables and other SQL statements.
在本模块后面的实践实验室中,您将练习创建表和其他 SQL 语句。