mysql-ddl常用操作(ddl-对于表结构的更改)

-- 删除表
drop table if exists student;
drop table student;
-- create table 带注释
create table student(
id int comment '主键',
name varchar(32),
age int,
score double(4,1),
brithday date,
insert_time timestamp
)COMMENT='测试表';

-- create table不带注释
create table stu(
id int ,
name varchar(32),
age int,
score double(4,1),
brithday date,
insert_time timestamp
);
-- 创建新表
create table stud like student;


-- 修改表名 to要不要都不会有影响
alter table studen rename to stud;
-- 给表加/修改注释
alter table student COMMENT '学生表';

-- 可以看到表的结构信息以及表的字符集 latin1
show create table stud;
CREATE TABLE `stud` (
`id` int(11) DEFAULT NULL,
`name` varchar(32) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`score` double(4,1) DEFAULT NULL,
`brithday` date DEFAULT NULL,
`insert_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='测试哇'

-- 修改字符集
alter table stud character set utf8;
-- 添加一列
alter table stud add column gender varchar(2);
-- 给表字段添加/修改注释
alter table stud modify column gender varchar(2) COMMENT '性别';
-- 更改表字段长度
alter table stud modify gender varchar(20);
-- 更改表字段类型
alter table stud modify gender int;
-- 更改列名 并更改列名类型或是长度
alter table stud change name v_name varchar(20);
alter table stud change v_name c_name int;

-- 删除一列
alter table stud drop column gender;

posted @ 2020-03-08 01:39  caocx  阅读(554)  评论(0)    收藏  举报