创建表
表是 SQL 中组织和存储数据的基本单位。每个表都有一个名称,例如 author、book_mast、purchase 或 orders。表类似于非数据库系统中的文件。
表分为行和列。每行代表一条记录,每列都可以被视为代表该记录的一个组件。
语法:
CREATE TABLE <table_name>( column1 data_type[(size)], column2 data_type[(size)], ...);
Parameters:
Name | Description |
---|---|
table_name | 存储数据的表的名称 |
column1,column2 | 表的列名 |
data_type | Char, varchar, integer, decimal, date and more. |
size | 表的列的最大长度 |
SQL CREATE TABLE 语句的最佳实践
以下是有关 “SQL CREATE TABLE” 语句的一些要点和提示:
Table Name: 为您的表选择一个描述性的相关名称。它应该反映它所代表的实体或概念。
Column Definitions: 定义表中的每一列及其名称和数据类型。考虑所存储数据的性质并选择合适的数据类型(例如 INTEGER、VARCHAR、DATE 等)。
Constraints 约束: 使用约束对表中的数据实施规则或条件。常见约束包括 PRIMARY KEY、FOREIGN KEY、UNIQUE、NOT NULL 和 CHECK 约束。
Primary Key 主键: 为表选择一个主键以唯一标识每一行。这通常是一列(或列的组合),每行都有唯一的值。
Indexes 索引: 考虑在搜索条件中经常使用的列上添加索引,以提高查询性能。
Normalization 原则: 设计表结构以遵循规范化原则,以减少冗余并提高数据完整性。
Comments 注释: 使用注释来记录表的用途,以及各个列和约束。这有助于其他开发人员了解 Schema 及其预期用途。
Data Integrity 数据完整性: 通过在数据库级别实施约束和验证数据来确保数据完整性。这可以防止插入不正确或不一致的数据。
Table Options 表选项: 一些数据库系统提供额外的表选项,如存储参数、压缩设置或分区选项。熟悉这些选项,并根据需要使用它们进行性能优化和数据管理。
Version Control 版本控制: 将 SQL 脚本(包括 CREATE TABLE 语句)存储在版本控制系统中。这有助于跟踪随时间的变化并促进团队成员之间的协作。
Testing 测试: 在将 CREATE TABLE 语句部署到生产环境之前,请对其进行全面测试。检查语法错误、约束冲突和性能影响。
Permissions 权限: 确保执行 CREATE TABLE 语句的用户具有在目标数据库架构中创建表所需的权限。
Backup 备份: 定期备份数据库架构定义,包括 CREATE TABLE 语句,以防止在意外删除或损坏时丢失数据。
SQL create table basic 语句
Field Name | Data Type | Size | Decimal Places | NULL |
---|---|---|---|---|
agent_code | char | 6 | No | |
agent_name | char | 40 | No | |
working_area | char | 35 | No | |
commission | decimal | 10 | 2 | No |
phone_no | char | 15 | Yes |
-- Creating a table named 'mytest'
CREATE TABLE mytest (-- 固定长度的字符数据类型,在这种情况下,它最多允许 6 个字符。
agent_code CHAR(6),
agent_name CHAR(40),
working_area CHAR(35),-- DECIMAL 是一种可以存储定点数的数值数据类型。在这里,它被指定为总共有 10 位数字,其中 2 位保留用于小数位。
commission DECIMAL(10, 2),
phone_no CHAR(15) NULL
);
要查看所创建表的结构:
DESCRIBE mytest;
PRIMARY KEY 主键
The SQL PRIMARY KEY is a column in a table which must contain a unique value which can be used to identify each and every row of a table uniquely.
SQL PRIMARY KEY是表中的一列,它必须包含一个唯一的值,该值可用于唯一地标识表中的每一行。
However, SQL supports primary keys directly with the PRIMARY KEY constraint.
但是,SQL支持直接使用PRIMARY KEY约束的主键。
Functionally, it is the same as the UNIQUE constraint, except that only one PRIMARY KEY can be defined for a given table. PRIMARY KEY's will not allow NULL values.
从功能上讲,它与UNIQUE约束相同,不同之处在于只能为给定的表定义一个PRIMARY KEY。PRIMARY KEY不允许NULL值。
A primary key is used to identify each row identically in a table. It may be a part of the actual record itself.
主键用于在表中标识相同的每一行。它可能是实际记录本身的一部分。
The SQL PRIMARY KEY can be made up by one or more fields on a table and when it happens, they are called a composite key.
SQL PRIMARY KEY可以由表中的一个或多个字段组成,当它发生时,它们被称为复合键。
Primary keys can be specified at the time of CREATING TABLE or the time of changing the structure of the existing table using ALTER TABLE statement.
主键可以在CREATING TABLE或使用ALTER TABLE语句更改现有表的结构时指定。
This constraint is a combination of a NOT NULL constraint and a UNIQUE constraint. This constraint ensures that the specific column or combination of two or more columns for a table have a unique identity which helps to find a particular record in a table more easily and quickly.
此约束是NOT NULL约束和UNIQUE约束的组合。此约束确保表的特定列或两列或多列的组合具有唯一标识,这有助于更轻松快速地在表中查找特定记录。
CREATE TABLE <table_name> column1 data_type[(size)] NOT NULL PRIMARY KEY, column2 data_type[(size)], ...);
Good practice for primary keys in tables
表中主键的良好实践
- Primary keys should be as small as necessary. Prefer a numeric type because numeric types are stored in a much more compact format than character formats.
主键应该尽可能小。首选数字类型,因为数字类型以比字符格式更紧凑的格式存储。 - Primary keys should never change.主键永远不会改变。
- Do not use passport number, social security number, or employee contract number as "primary key" as these "primary key" can change for real world situations.
不要使用护照号、社会保险号或员工合同号作为“主键”,因为这些“主键”可能会因真实的世界情况而改变。
Example:范例:
Suppose, we are going to create a table named 'agent1'. It contains the columns and data types which shown bellow. For each row of 'agent1' table, it is required to identify each agent with a unique code, because the name of two or more agents of a city of a country may be same.
假设我们要创建一个名为“agent1”的表。它包含下面显示的列和数据类型。对于'agent1'表的每一行,需要用唯一的代码标识每个代理,因为一个国家的一个城市的两个或多个代理的名称可能相同。
So, it is not a good choice to create PRIMARY KEY on 'agent_name'. The 'agent_code' could be the only and exclusive choice for a PRIMARY KEY for this table.
因此,在'agent_name'上创建PRIMARY KEY并不是一个好的选择。“agent_code”可以是此表的PRIMARY KEY的唯一和排他选择。
Field Name | Data Type | Size | Decimal Places | NULL | Constraint |
---|---|---|---|---|---|
agent_code | char | 6 | No | PRIMARY KEY | |
agent_name | char | 40 | No | ||
working_area | char | 35 | Yes | ||
commission | decimal | 10 | 2 | Yes | |
phone_no | char | 17 | Yes |
创建表时,可以使用 列级主键约束或表级约束。下面是上述表格中的两个示例:
Column-level primary-key constraint :列级主键约束:
CREATE TABLE agent1 (
-- Column: agent_code with a data type CHAR(6), NOT NULL, and PRIMARY KEY agent_code CHAR(6) NOT NULL PRIMARY KEY,
-- Column: agent_name with a data type CHAR(40) and NOT NULL constraint
agent_name CHAR(40) NOT NULL,
-- Column: working_area with a data type CHAR(35)
working_area CHAR(35),
-- Column: commission with a decimal data type (10 digits, 2 decimal places)
commission DECIMAL(10, 2),
-- Column: phone_no with a data type CHAR(17)
phone_no CHAR(17)
);
Table-level primary-key constraint:表级主键约束:
-- Creating a table named 'agent1'
CREATE TABLE agent1 (
-- Column: agent_code with a data type CHAR(6), NOT NULL
agent_code CHAR(6) NOT NULL,
-- Column: agent_name with a data type CHAR(40), NOT NULL
agent_name CHAR(40) NOT NULL,
-- Column: working_area with a data type CHAR(35)
working_area CHAR(35),
-- Column: commission with a decimal data type (10 digits, 2 decimal places)
commission DECIMAL(10, 2),
-- Column: phone_no with a data type CHAR(17)
phone_no CHAR(17),
-- Defining a primary key constraint named pk_agent_code on the agent_code column
CONSTRAINT pk_agent_code PRIMARY KEY (agent_code)
);
CONSTRAINT pk_agent_code PRIMARY KEY(agent_code):此行在“agent_code”列上定义名为“pk_agent_code”的主键约束。它确保“agent_code”列中的每个值都是唯一的,并且不为null,从而有效地使其成为表的主键。
SQL CREATE TABLE with PRIMARY KEY CONSTRAINT
SQL PRIMARY KEY CONSTRAINT是NOT NULL约束和UNIQUE约束的组合。此约束确保表的特定列或两列或多列的组合具有唯一标识,这有助于更轻松快速地在表中查找特定记录。
SQL CREATE TABLE with PRIMARY KEY和UNIQUE CONSTRAINT
-- Creating a table named 'mytest'
CREATE TABLE mytest (
-- Column: cust_code with a data type CHAR(6), NOT NULL, and PRIMARY KEY
cust_code CHAR(6) NOT NULL PRIMARY KEY,
-- Column: cust_name with a data type CHAR(25), NOT NULL, and UNIQUE constraint
cust_name CHAR(25) NOT NULL UNIQUE,
-- Column: cust_city with a data type CHAR(25), NOT NULL
cust_city CHAR(25) NOT NULL,
-- Column: grade with a data type INTEGER
grade INTEGER,
-- Column: agent_code with a data type CHAR(6), NOT NULL
agent_code CHAR(6) NOT NULL
);
cust_name CHAR(25)NOT NULL UNIQUE,:该列被指定为NOT NULL,这意味着它的每一行都必须有一个值,并且它还有一个UNIQUE约束,确保该列中的每个值在表中的所有行中都是唯一的。
SQL CREATE TABLE with PRIMARY KEY CONSTRAINT on more columns
-- Creating a table named 'mytest'
CREATE TABLE mytest (
-- Column: cust_code with a data type CHAR(6), NOT NULL
cust_code CHAR(6) NOT NULL,
-- Column: cust_name with a data type CHAR(25), NOT NULL
cust_name CHAR(25) NOT NULL,
-- Column: cust_city with a data type CHAR(25)
cust_city CHAR(25),
-- Column: grade with a data type INTEGER
grade INTEGER,
-- Column: agent_code with a data type CHAR(6), NOT NULL
agent_code CHAR(6) NOT NULL,
-- Defining a composite primary key on cust_code and cust_city columns
PRIMARY KEY (cust_code, cust_city)
);