1. Create table
i. Normal
CREATE TABLE t_bookType(
id int primary key auto_increment,
bookTypeName varchar(20),
bookTyptDesc varchar(200)
);
ii. With index
create table t_book(
id int primary key auto_increment,
bookName varchar(20),
author varchar(10),
price decimal(6,2),
bookTypeId int,
constraint `forKey` foreign key (`bookTypeId`) references `t_bookType`(`id`)
);


2. Describe table
desc t_bookType;
show create table t_bookType;


3. Alter
i. Table rename
alter table t_book rename t_book2;


ii. Rename field
alter table t_book change bookName bookName2 varchar(20);


iv. Add field
alter table t_book add testField int after author;
alter table t_book add testField int first;
alter table tableName add fieldNameNew dataType [constraint condition] first|(after fieldNameOld)


v. Drop field
alter table t_book drop testField;


4. Drop
i. Drop table
drop table t_book;

Be careful:
The PARENT table could not be deleted, it has to delete the sub table first!

本文详细介绍了如何使用SQL进行表的基本操作,包括创建表、描述表结构、修改表结构(如重命名表、更改字段名、添加和删除字段)以及删除表等内容,并提供了具体的SQL语句示例。
1万+

被折叠的 条评论
为什么被折叠?



