MySQL's data sheets to build / add / change / delete

A, MySQL create a data table

After the database is created, the next step is to create a data table in the database. The so-called creation of data tables, referring to the establishment of a new table in the database has been created.

Process is the process of creating a data table of a predetermined attribute data columns, and implements the data integrity (including physical integrity, domain integrity and referential integrity) process constraints.

The basic syntax

In MySQL, you can use the  CREATE TABLE  create table statement. The syntax is:

CREATE TABLE <table name> ([Table definitions Option]) [Table Option] [Partition Options];

Among them, [表定义选项]the format is:

<1 column name> <type 1> [, ...] <column name n> <type n>

CREATE TABLE command more grammar, which is mainly to create definitions (create-definition), table options (table-options) and partitioning options (partition-options) composed by the table.

The main grammar and how to use the CREATE TABLE statement is as follows:

  • CREATE TABLE: used to create a table given name, must have the permission of the CREATE TABLE.
  • <Table name>: Specifies the name of the table to be created, after the CREATE TABLE given, must comply with the identifier naming rules. Table name is specified in order to create tables in a specific database for db_name.tbl_name,. Whether or not there is a current database, you can create in this way. When you create a table in the current database, you can be omitted db-name. If you use a distinguished name in quotation marks, it should have the database and table names are quoted. For example, 'mydb'. 'Mytbl' is legal, but 'mydb.mytbl' illegal.
  • <Table Definition Options>: Create a table is defined by the column name (col_name), column definitions (column_definition) and possibly null explanation, integrity constraints or the composition of the index table.
  • By default, the table is created in the current database. If the table already exists, there is no current database or database does not exist, an error occurs.

Tip: Use CREATE TABLE to create a table, you must specify the following information:

  • Name of the table to be created is not case sensitive, you can not use SQL language keywords, such as DROP, ALTER, INSERT and so on.
  • Data table each column (field) names and data types, if you create multiple columns, separated by commas.

Create a data table in the specified database

Data table belongs to a database, before creating the data table, use the statement "USE <database>" specifies the operation in which the database, if the database is not selected, it will be thrown No database selected mistake.

Or use the command SELECT DATABASE (); view the database is currently located:

 

 

 show tables see the current database already created a database table

 

 

 CREATE TABLE <table name> to create a data table

 

 

 describe <table name> See Data Table Structure

 

 

 Wherein the meaning of each field is as follows:

  • Null: indicate whether the column can store NULL values.
  • Key: Indicates whether the column is indexed. PRI denotes the column part of the table is the primary key, UNI represents the column index is part UNIQUE, MUL represents a given value in a column with multiple occurrences allowed.
  • Default: Indicates whether the column has a default value, if any, value is.
  • Extra: indicates additional information associated with a given column can be obtained, such as AUTO_INCREMENT like.

Or show create table <table name> See specific configuration

 

 

 

Tip: Use the CREATE TABLE statement can not only SHOW view detailed statement when you create a table, and you can view the storage engine and character encoding. If you do not add "\ G" parameters, the displayed results may be very confusing, then add "\ G" parameter, you can show results more intuitive, easier to read.
Two, MySQL modify data table
For the purpose of standardized design database tables, sometimes you need to watch previously created structural modifications or adjustments.
Common syntax is as follows:

ALTER TABLE <table name> [edit options]

Modify option syntax is as follows:

{ ADD COLUMN <列名> <类型>
| CHANGE COLUMN <旧列名> <新列名> <新列类型>
| ALTER COLUMN <列名> { SET DEFAULT <默认值> | DROP DEFAULT }
| MODIFY COLUMN <列名> <类型>
| DROP COLUMN <列名>
| RENAME TO <新表名> }

添加字段

修改前的数据库表结构如下:

ALTER TABLE <表名> ADD <新字段名> <数据类型> [约束条件] [FIRST|AFTER 已存在的字段名];

【实例 1】使用 ALTER TABLE 修改表 tb_test1 的结构,在表的第一列添加一个 int 类型的字段 co1

修改后的数据库表结构如下:

提示:“FIRST 或 AFTER 已存在的字段名”用于指定新增字段在表中的位置,如果 SQL 语句中没有这两个参数,则默认将新添加的字段设置为数据表的最后列。

【实例 2】使用 ALTER TABLE 修改表 tb_test1 的结构,在一列 name 后添加一个 int 类型的字段 col2。

 

 

 修改字段数据类型

ALTER TABLE <表名> MODIFY <字段名> <数据类型>

其中,表名指要修改数据类型的字段所在表的名称,字段名指需要修改的字段,数据类型指修改后字段的新数据类型。

【实例 3】使用 ALTER TABLE 修改表 tb_test1 的结构,将 name 字段的数据类型由 VARCHAR(22) 修改成 VARCHAR(30)

 

 删除字段

ALTER TABLE <表名> DROP <字段名>;

其中,字段名指需要从表中删除的字段的名称。

【实例 4】使用 ALTER TABLE 修改表 tb_test1 的结构,删除 col2 字段

 

 修改字段名称

ALTER TABLE <表名> CHANGE <旧字段名> <新字段名> <新数据类型>;

其中,旧字段名指修改前的字段名;新字段名指修改后的字段名;新数据类型指修改后的数据类型,如果不需要修改字段的数据类型,可以将新数据类型设置成与原来一样,但数据类型不能为空。

【实例 5】使用 ALTER TABLE 修改表 tb_test1 的结构,将 co1 字段名称改为 col3

 

提示:由于不同类型的数据在机器中的存储方式及长度并不相同,修改数据类型可能会影响数据表中已有的数据记录,因此,当数据表中已经有数据时,不要轻易修改数据类型。

修改表名

ALTER TABLE <旧表名> RENAME [TO] <新表名>;

其中,TO为可选参数,使用与否均不影响结果。

【实例 6】使用 ALTER TABLE 将数据表 tb_test1 改名为 test

 

 

 三、删除数据表

当需要删除一个表的时候,可以使用 DROP TABLE 语句来完成,语法格式如下:

DROP TABLE [IF EXISTS] <表名> [ , <表名1> , <表名2>] …

语法说明如下:

  • <表名>:被删除的表名。DROP TABLE 语句可以同时删除多个表,用户必须拥有该命令的权限。
  • 表被删除时,所有的表数据和表定义会被取消,所以使用本语句要小心。
  • 表被删除时,用户在该表上的权限并不会自动被删除。
  • 参数IF EXISTS用于在删除前判断删除的表是否存在,加上该参数后,在删除表的时候,如果表不存在,SQL 语句可以顺利执行,但会发出警告(warning)。

 

 

Guess you like

Origin www.cnblogs.com/ccstu/p/12157662.html