Create a SQL table

SQL table creation is achieved through SQL CREATE TABLE statement, the statement is DDL  SQL statements . CREATE TABLE statement to create a table for storing data. Create a table, a column may be defined integrity constraints primary key, unique and foreign keys and the like. Integrity constraints can be defined in a column or table level. For different RDBMS, CREATE statement and realization of the syntax is different.

 

CREATE TABLE statement syntax

CREATE TABLE table_name (column_name1 datatype, column_name2 datatype,... column_nameN datatype); 
SQL
  • table_name- is the name of the table
  • column_name1, column_name2 ... .- is the name of the column

  • datatype - is the data type of the column, such as char, date, number, etc.

For example: If you want to create the employee table, the statement would look like this:

CREATE TABLE employee ( id number(5), name char(20), dept char(10), age number(2), salary number(10), location char(10)); 
SQL

In the Oracle database, the data type integer column is represented as "number". In Sybase, which is represented as "int".

Oracle provides another way to create the table.

CREATE TABLE temp_employee SELECT * FROM employee
SQL

In the above statement, the employee table using the same number of columns and data types created temp_employee table.

CREATE TABLE statement example

For example, we want the players to create a table, a table named player, there are two fields, one is player_id, it is of type int, the other player_name field is varchar (255) type. These two fields are not empty and player_id is increasing.

So when you create can be written as:

CREATE TABLE player ( player_id int(11) NOT NULL AUTO_INCREMENT, player_name varchar(255) NOT NULL ); 
SQL

Note that, the last statement with a semicolon (;) as a terminator, after the last defined field no comma. Data type int (11) represents an integer of type, the length of the display 11, 11 parameters in parentheses represent the maximum effective length of the display, regardless of the type comprising a numerical range of sizes. varchar (255) represents the maximum length of string type 255 of the variable. NOT NULL indicates that the entire field is not null, it is a data constraint. AUTO_INCREMENT represents the primary key to grow automatically.

In fact, we are rarely write their own DDL statements, you can use some visual tools to create and manipulate databases and data tables. Here I recommend using Navicat, it is a database management and design tools, cross-platform, supports a variety of database management software, such as MySQL, Oracle, MariaDB and so on. Basically column mentioned database software can use Navicat to manage.

If this table is for the player, we want to design the following fields:

Create a SQL table

Player_id wherein the data table is the primary key player, and automatically increase, i.e. player_id starts from 1 and then increments by one. player_id, team_id, player_name these three fields are not null, height field can be empty.

According to the above design requirements, we can use the Navicat software design, as follows:

Create a SQL table

Then, we can also index the player_name field, type the index is Unique. Use Navicat set as follows:

Create a SQL table

Such a player on the table by good design visualization tools. We can put out this form guide, you can look at this table corresponding SQL statement is like. The method is selected in the left side of the Navicat right player this table, and then select the "Dump SQL File" → "Structure Only", so you can see the exported SQL files, code is as follows:

DROP TABLE IF EXISTS `player`; CREATE TABLE `player` ( `player_id` int(11) NOT NULL AUTO_INCREMENT, `team_id` int(11) NOT NULL, `player_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `height` float(3, 2) NULL DEFAULT 0.00, PRIMARY KEY (`player_id`) USING BTREE, UNIQUE INDEX `player_name`(`player_name`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; 
SQL

You can see the DDL handle the entire SQL file, first delete the player table (if the table exists in the database), and then re-create the player table, which tables and fields are used in anti-quotation marks, this is to avoid them MySQL reserved field name and the same data table and field names are added back quotes.

Which character set player_name field is utf8, collation is utf8_general_ci, on behalf of not case sensitive, if set to utf8_bin, on behalf of case-sensitive, there are many other collation is not introduced here.

Since player_id provided for a primary key, used in the DDL PRIMARY KEY predetermined, while the index Methods BTREE.

因为我们对player_name字段进行索引,在设置字段索引时,我们可以设置为UNIQUE INDEX(唯一索引),也可以设置为其他索引方式,比如NORMAL INDEX(普通索引),这里我们采用UNIQUE INDEX。唯一索引和普通索引的区别在于它对字段进行了唯一性的约束。在索引方式上,你可以选择BTREE或者HASH,这里采用了BTREE方法进行索引。我会在后面介绍BTREE和HASH索引方式的区别。

整个数据表的存储规则采用InnoDB。之前我们简单介绍过InnoDB,它是MySQL5.5版本之后默认的存储引擎。同时,我们将字符集设置为utf8,排序规则为utf8_general_ci,行格式为Dynamic,就可以定义数据表的最后约定了:

ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; 
SQL

你能看出可视化工具还是非常方便的,它能直接帮我们将数据库的结构定义转化成SQL语言,方便数据库和数据表结构的导出和导入。不过在使用可视化工具前,你首先需要了解对于DDL的基础语法,至少能清晰地看出来不同字段的定义规则、索引方法,以及主键和外键的定义。

数据表的常见约束

当我们创建数据表的时候,还会对字段进行约束,约束的目的在于保证RDBMS里面数据的准确性和一致性。下面,我们来看下常见的约束有哪些。

首先是主键约束

主键起的作用是唯一标识一条记录,不能重复,不能为空,即UNIQUE+NOT NULL。一个数据表的主键只能有一个。主键可以是一个字段,也可以由多个字段复合组成。在上面的例子中,我们就把player_id设置为了主键。

其次还有外键约束

外键确保了表与表之间引用的完整性。一个表中的外键对应另一张表的主键。外键可以是重复的,也可以为空。比如player_id在player表中是主键,如果你想设置一个球员比分表即player_score,就可以在player_score中设置player_id为外键,关联到player表中。

除了对键进行约束外,还有字段约束。

唯一性约束。

唯一性约束表明了字段在表中的数值是唯一的,即使我们已经有了主键,还可以对其他字段进行唯一性约束。比如我们在player表中给player_name设置唯一性约束,就表明任何两个球员的姓名不能相同。需要注意的是,唯一性约束和普通索引(NORMAL INDEX)之间是有区别的。唯一性约束相当于创建了一个约束和普通索引,目的是保证字段的正确性,而普通索引只是提升数据检索的速度,并不对字段的唯一性进行约束。

NOT NULL约束。

对字段定义了NOT NULL,即表明该字段不应为空,必须有取值。

DEFAULT,表明了字段的默认值。如果在插入数据的时候,这个字段没有取值,就设置为默认值。比如我们将身高height字段的取值默认设置为0.00,即DEFAULT 0.00。

CHECK约束

用来检查特定字段取值范围的有效性,CHECK约束的结果不能为FALSE,比如我们可以对身高height的数值进行CHECK约束,必须≥0,且<3,即CHECK(height>=0 AND height<3)。

设计数据表的原则

我们在设计数据表的时候,经常会考虑到各种问题,比如:用户都需要什么数据?需要在数据表中保存哪些数据?哪些数据是经常访问的数据?如何提升检索效率?

如何保证数据表中数据的正确性,当插入、删除、更新的时候该进行怎样的约束检查?

如何降低数据表的数据冗余度,保证数据表不会因为用户量的增长而迅速扩张?

如何让负责数据库维护的人员更方便地使用数据库?

除此以外,我们使用数据库的应用场景也各不相同,可以说针对不同的情况,设计出来的数据表可能千差万别。那么有没有一种设计原则可以让我们来借鉴呢?这里我整理了一个“三少一多”原则:

1.数据表的个数越少越好

RDBMS的核心在于对实体和联系的定义,也就是E-R图(Entity Relationship Diagram),数据表越少,证明实体和联系设计得越简洁,既方便理解又方便操作。

2.数据表中的字段个数越少越好

The more the number of field, the greater the likelihood of data redundancy. Smaller number setting field of each field is independently provided, instead of a field value may be calculated from other fields. Of course, less number of fields are relative, we typically be balanced and redundant data retrieval efficiency.

3. The number of fields combined data table primary key as possible

A primary key is provided to determine uniqueness, when a field of the time can not be determined uniquely, require the use of primary key way (i.e. with a plurality of fields to define a primary key). The more co-primary key field, the greater the space occupied by the index, will not only increase the difficulty of understanding, it will increase uptime and index space, and therefore the number of field primary key, the better.

4. Use the primary and foreign keys better

Database design actually define relationships between various tables, as well as a variety of fields. The more these relationships prove that the lower the degree of redundancy between these entities, the higher the degree of utilization. The benefit of this is that not only guarantees the independence between the data table, but also enhance the association between utilization of each other.

You should be able to see the core of the "three little over one" principle it is simple and reusable. Simply refers to the table with fewer, fewer fields, fewer primary key field data table to complete the design. Reusable is to enhance multiplexing rate among the data table primary key, foreign key is used. Because a primary key to understanding is representative of a table. Key design the more, the higher the utilization rate of proof between them.

Geeks tutorial provides SQL DDL statements associated with the table (TABLE) of:

Guess you like

Origin www.cnblogs.com/numpycomcn/p/11790243.html