Home > Database > Mysql Tutorial > body text

Summary of basic operations of mysql table (3)_MySQL

WBOY
Release: 2016-09-09 08:13:43
Original
1178 people have browsed it

Basic operations for mysql tables, the details are as follows

1. Create table:
Syntax form for creating a table:

CREATE TABLE table_name(
 属性名 数据类型,
 属性名 数据类型,
 ...
 属性名 数据类型
)

Copy after login

Example:

CREATE TABLE t_dept(
 deptno int,
 dname varchar(20),
 loc varchar(20)
Copy after login

Note: Before creating a table, you usually need to use the USE library name; such a statement selects a library and creates the table in the selected library. The table name identifier cannot be a MySQL keyword, such as CREATE, USE, etc. It is recommended that the table name identifier be t_xxx or tab_xxx; each attribute is separated by a comma, and no comma is required after the last attribute.

2. View table structure:
2.1 DESCRIBE statement to view the definition of the table
USE library name; //Select a library
DESCRIBE table name; //View the definition information of the table, DESCRIBE can be replaced by DESC
2.2 SHOW CREATE TABLE statement to view the detailed definition of the table
USE library name; //Select a library
SHOW CREATE TABLE table name G table name; //View table definition information
Note: When displaying the detailed definition information of the table, you can use the ";" "g" "G" symbols to end. In order to make the results display more beautiful and facilitate the user's viewing, it is best to use G to end.

3. Delete table:
The syntax form of deleting a table:
USE library name; //Select a library
DROP TABLE table name; //Delete the table with the specified table name

4. Modify table:
4.1 Modify table name
For the tables that have been created, some structural modifications are required after using them for a period of time, that is, table modification operations. Why not delete the table directly and create the table according to the new table definition? The reason is that if a large amount of data already exists in the table, a lot of additional work will be required after reconstruction, such as data reloading. In order to solve the above problems, mysql provides the ALTER TABLE statement to modify the table structure.
Modify the syntax of the table name: ALTER TABLE old_table_name REANME [TO] new_table_name;
4.2 Add fields
4.2.1 Add a field at the last position of the table
The syntax is: ALTER TABLE table_name ADD attribute name attribute type;
4.2.2 Add a field at the first position of the table
The syntax is: ALTER TABLE table_name ADD attribute name attribute type FIRST;
4.2.3 Add fields after the specified fields in the table
The syntax is: ALTER TABLE table_name ADD attribute name attribute type AFTER attribute name;
4.3 Delete field
Syntax for deleting fields: ALTER TABLE table_name DROP attribute name;
4.4 Modify fields (modify the data type and order must be MODIFY, modify the name or name and attributes must be CHANGE)
4.4.1 Modify the data type of the field
The syntax is: ALTER TABLE table_name MODIFY attribute name data type; //The data type is the modified data type
4.4.2 Modify the name of the field
The syntax is: ALTER TABLE table_name CHANGE old attribute name new attribute name old data type;
​ 4.4.3 Modify the name and attributes of the field at the same time
The syntax is: ALTER TABLE table_name CHANGE old attribute name new attribute name new data type;
4.4.4 Modify the order of fields
The syntax is: ALTER TABLE table_name MODIFY attribute name 1 data type FIRTST | AFTER attribute name 2;
//"Attribute name 1" parameter indicates the field name to be adjusted in order, "FITST" parameter indicates adjusting the field to the first position of the table, "AFTER attribute name 2" indicates adjusting the field to the position after the attribute name 2 field .

5.操作表的约束:
5.1 MySQL支持的完整性约束
所谓完整性是指数据的准确性和一致性,而完整性检查是指检查数据的准确性和一致性。MySQL提供了一种机制来检查数据库表中的数据是否满足规定的条件,以保证数据库中数据的准确性和一致性,这种机制就是约束。MySQL除了支持标准SQL的完整性约束外,还扩展增加了AUTO_INCREMENT约束。
1. NOT NULL //约束字段的值不能为空
2. DEFAULT //设置字段的默认值
3. UNIQUE KEY(UK) //约束字段的值是唯一
4. PRIMARY KEY(PK) //约束字段为表的主键,可以作为该表记录的唯一标识
5. AUTO_INCREMENT //约束字段的值为自动增长
5.2 设置非空约束(NOT NULL, NK)
在创建数据库表时,为某些字段加上”NOT NULL”约束条件,保证所有记录中的该字段都是有值的。 设置非空约束的语法形式为:

 CREATE TABLE table_name ( 
  属性名 数据类型 NOT NULL, 
 ); 
Copy after login

5.3 设置字段的默认值(DEFAULT)
当为数据库表中插入一条新记录时,如果没有为某个字段赋值,那么数据库系统会自动为这个字段插入默认值。设置数据库表中某个字段的默认值语法形式为:

 CREATE TABLE table_name ( 
  属性名 数据类型 DEFAULT 默认值, 
 );
Copy after login

5.4 设置唯一约束(UNIQUE, UK)
当数据库表中的某个字段上的内容不允许重复时,则可以使用UK约束进行设置。即UK约束在创建数据库表时为某些字段加上”UNIQUE”约束条件,保证所有记录中该字段上的值不重复。
设置唯一约束语法形式为:

CREATE TABLE table_name(
 属性名 数据类型 UNIQUE,
);
Copy after login

例如:

CREATE TABLE t_dept(
 deptno INT ,
 dname VARCHAR(20) UNIQUE,
 loc VARCHAR(40) 
);
Copy after login

如果想给字段dname上的UK约束设置一个名字,可以执行SQL语句CONSTRAINT,示例如下:

CREATE TABLE t_dept(
 deptno INT,
 dname VARCHAR(20),
 loc VARCHAR(40),
 CONSTRAINT uk_dname UNIQUE(dname)
);
//在为约束设标识符时,推荐使用“约束缩写_字段名",因此设置为uk_dname;

Copy after login

5.5 设置主键约束(PRIMARY KEY , PK)
当想用数据库表中的某个字段来唯一标识所有记录时,则可以使用PK约束进行设置。在数据库表中之所以设置主键,是为了便于快速的查找到表中的记录。在具体设置主键约束时,必须要满足主键字段的值是唯一、非空的。主键可以使单一字段,也可以是多个字段,因此分为单字段主键和多字段主键。主键约束相当于 非空约束 加上 唯一约束。
5.5.1 单字段主键
设置PK约束,语法形式如下:

CREATE TABLE table_name(
 属性名 数据类型 PRIMARY KEY,
);

Copy after login

如果想给字段deptno上的PK约束设置一个名字,可以使用CONSTRAINT,示例如下:

CREATE TABLE table_name(
 deptno INT,
 dname VARCHAR(20),
 loc VARCHAR(40),
 CONSTRAINT pk_deptno PRIMARY KEY(deptno)
);
Copy after login

5.5.2 多字段主键
当主键有多个字段组合而成时,则需要通过SQL语句CONSTRAINT来实现,其语法形式如下:

CREATE TABLE table_name(
 属性名 数据类型,
 ......
 [CONSTRAINT 约束名]PRIMARY KEY (属性名,属性名......)
);

Copy after login

5.6 设置字段值自动增加(AUTO_INCREMENT)
AUTO_INCREMENT 是MySQL唯一扩展的唯一性约束,当为数据库表中插入新记录时,字段上的值会生成唯一的ID,在数据库表中只能有一个字段使用该约束,该字段的数据类型必须是整数类型,由于设置自增长字段会生成唯一的ID,所以该字段也经常设置成PK主键。
设置自增长语法形式如下:

CREATE TABLE table_name(
 属性名 数据类型 AUTO_INCREMENT, //默认字段的值是从1开始增加,每增加一条在前一条的基础上加1
 deptno INT PRIMARY KEY AUTO_INCREMENT, //通常都会和PK一起写
);

Copy after login

5.7 设置外键约束(FOREIGN KEY ,FK)
前面的完整性约束都是在单表中进行设置,而外键约束通常保证两个表之间的参照完整性,即构建于两个表的两个字段之间的参照关系。在具体设置FK约束时,设置FK约束的字段必须依赖于数据库中已经存在的”一”(一对多中的一)的一方的主键,同时外键可以设置为null。
设置FK约束的语法形式如下:

CREATE TABLE table_name(
 属性名 数据类型,
 属性名 数据类型,
 [CONSTRAINT 外键约束名] FOREIGN KEY (属性名1) REFERENCES 表名 (属性名2)
//备注:"外键约束名"用来标识约束名,"属性名1"参数是"多"的一方表中设置外键的字段名,"属性2"中,参数是"一"的一方设置主键约束的字段名。
)
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template