[Operation] MySQL database table

  1. Create a table
create table [if not exists] `表的名字`
( id int not null auto_increment primary key comment '主 键', 
account char(255) comment '⽤⼾名' default 'admin', 
pwd varchar(65535) comment '密码' not null ) 
engine=myisam charset=utf8mb4;

Note: If you do not specify the character set, the default character set engine library inherited default innodb

2. Select the database to see all the tables in order to view table

show tables;

3. Delete table

drop table [if exists] `表名`

4. displayed to build the table structure

desc `表名`; 
describe `表名`;
  1. Modify table
-- 修改表的名称 
alter table `old_name` rename `new_name`; 
-- 修改表的引擎 
alter table `表名` engine = innodb|myisam; 
-- 移动表 到指定的数据库 
alter table `表名` rename to 数据库名.表名;

6. Modify field

-- 增加⼀个新的字段 
alter table `表名` add `字段名` 数据类型 属性; 

-- 增加⼀个新的字段, 并放在⾸位 
alter table `表名` add `字段名` 数据类型 属性 first; 

-- 增加⼀个新的字段, 并放在某⼀个字段之后 
alter table `表名` add `字段名` 数据类型 属性 after 指定 字段; 

-- 修改字段的属性 
alter table `表名` modify `字段名` 数据类型 属性; 

-- 修改字段的名称 
alter table `表名` change `原字段名` `新的字段名` 数据 类型 属性; 

-- 修改字段的位置 
alter table `表名` change `原字段名` `新的字段名` 数据 类型 after `指定字段`; 

-- 删除字段 
alter table `表名` drop `字段名`;


7. Copy the table

  • Copy table, and copy the data
-- 执⾏下列语句 
create table `复制表的名称` select * from `原表名`; 
#特点: 完整的复制⼀个表,既有原表的结构,⼜有原表的数 据,不能复制主键
  • Copy table structure only, not copy the data
create table `复制表的名称` like `原表名`; 

#特点: 复制后的表结构与原表相同,但是⾥⾯没有数据,是 ⼀张空表,可以复制主键 -- 复制数据 
insert into `复制表的名称` select * from `原表名`;
Published 116 original articles · won praise 10 · views 1353

Guess you like

Origin blog.csdn.net/weixin_44727383/article/details/104999260