MYSQL study notes five (operating table)

First, create a table (CREATE TABLE statement)
1.1 syntax to create tables
using the CREATE TABLE statement syntax is:

CREATE TABLE <table name> ([Table definitions Option]) [Table Option] [partitioning options];
wherein [Table definitions Option] format: <column name 1> <Type 1> [, ...] <column name n > <type n>
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 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.

1.2 Create a table Demo
here to create a database table named testTable the table, the table contains four fields (id, NAME, deptId, salary ), which data types are: INT (11), VARCHAR ( 5), INT (11 ), FLOAT. Detailed commands are as follows:

The CREATE TABLE IF the NOT EXISTS testTable
(
the above mentioned id INT (11),
NAME VARCHAR (25),
deptId INT (11),
salary FLOAT
);
on the table, right-click, pop-up menu, select to change the table, you can view detailed tables structural information.
MYSQL study notes five (operating table)
MYSQL study notes five (operating table)
Second, see table definition
there are four ways to view table definitions, as follows:

A method
show create table table_name; ## can use this table to look at the code.
MYSQL study notes five (operating table)
Method Two
show full columns from table_name; ## specified table shows all the columns
MYSQL study notes five (operating table)
Method three
show columns from table_name; all columns specified in Table ## show the
MYSQL study notes five (operating table)
Method Four
describe table_name; ## view the table definition
1

Third, update the table structure
3.1 The basic syntax for
using the ALTER TABLE statement to modify tables in MySQL. The specific grammatical structure is as follows:

ALTER TABLE <表名> [修改选项]
{ ADD COLUMN <列名> <类型>
| CHANGE COLUMN <旧列名> <新列名> <新列类型>
| ALTER COLUMN <列名> { SET DEFAULT <默认值> | DROP DEFAULT }
| MODIFY COLUMN <列名> <类型>
| DROP COLUMN <列名>
| RENAME TO <新表名> }

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

ALTER TABLE testtable ##testtable为表明
ADD COLUMN col1 INT FIRST ## col1 为列名 FIRST插入列的顺序
MYSQL study notes five (operating table)
注意:在上述案例中,是将新添加的列添加到第一列,采用的是First。如果需要新添加一列,列名为test,并将test列添加到name列之后,脚本语句的写法如下:

ALTER TABLE tb_emp1
ADD COLUMN test INT AFTER name;

3.3 修改字段类型
修改字段时,一般指的是修改表的字段的数据类型。其语法规则如下:

ALTER TABLE <表名> MODIFY <字段名> <数据类型>
##表名指的是修改数据类型的字段所在表的名称,字段名指需要修改的字段,数据类型指修改后字段的新数据类型。

eg:
修改表 testtable的结构,将 name 字段的数据类型由 VARCHAR(25) 修改成 VARCHAR(30),其语句如下:

ALTER TABLE testtable
MODIFY name VARCHAR(30);

命令执行完成之后,可以查看表定义情况(如下图),可以看到数据类型已经修改成功:
MYSQL study notes five (operating table)
3.4 删除字段
删除字段就是指将数据表中冗余的字段从表中移除,语法格式如下:

ALTER TABLE <表名> DROP <字段名>;
##字段名指需要从表中删除的字段的名称。

eg:
修改表 testtable的结构,删除 col1 字段,其详细的语法如下:

ALTER TABLE tb_emp1
DROP col1;

以上命令运行完成之后,直接可以看看表定义(如下图),可以看到col1的列已经不存在了。
MYSQL study notes five (operating table)
3.5 修改字段名称
MySQL 中修改表字段名的语法规则如下:

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

eg:
修改表 testtable的结构,将 name 字段名称改为 fullname,同时将数据类型变为 CHAR(30)。详细语句如下:

ALTER TABLE testtable
CHANGE name fullname CHAR(30);

以上命令运行完成之后,直接可以看看表定义(如下图),可以看到name的列已经变成fullname了。
MYSQL study notes five (operating table)
注意:由于不同类型的数据在机器中的存储方式及长度并不相同,修改数据类型可能会影响数据表中已有的数据记录,因此,当数据表中已经有数据时,不要轻易修改数据类型。

3.6 修改表名
MySQL 实现表名的修改,语法规则如下:

ALTER TABLE <旧表名> RENAME [TO] <新表名>;
TO 为可选参数,使用与否均不影响结果。

eg:
将数据表 testtable改名为 testtable2,其sql语句如下:

ALTER TABLE testtable
RENAME TO testtable2;

After completion of this change, the method can be used to refresh the left F5 database, the results shown below.
MYSQL study notes five (operating table)
Fourth, the table delete
4.1 Delete table syntax
requires the use of DROP TABLE statement in MYSQL remove the table to complete, syntax is as follows:

DROP TABLE [IF EXISTS] <table name> [<table 1>, <table 2>] ...
1
<table name>: the name of the table to be deleted.
Note:
DROP TABLE statement can delete multiple tables at the same time, the user must have access to this command.
When the table is deleted, all table data and table definitions will be canceled, so use this statement to be careful.
When the table is dropped, user privileges on the table will not automatically be deleted.
Parameters IF EXISTS for judgment before deleting the deleted table exists, after adding this parameter, when you delete the table, if the table does not exist, SQL statements can be executed successfully, but will warn (warning).

Table 4.2 Delete Demo
delete data table testtable2, detailed statement is as follows:

TABLE testtable2 DROP;
1
execution results can be seen, testtable2 table firstdb database does not exist successful deletion.
MYSQL study notes five (operating table)

Guess you like

Origin blog.51cto.com/14525650/2436571
Recommended