MySql之alter

MySql之alter

不想破坏之前的数据库表,那就新建一个,再把数据复制过来

建表语句

create table m_blog_two
(
    id          bigint       not null
        primary key,
    user_id     bigint       not null,
    title       varchar(255) not null,
    description varchar(255) not null,
    content     longtext     null,
    created     datetime     not null on update CURRENT_TIMESTAMP,
    status      tinyint      null
)
    charset = utf8mb4;

插入数据

INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (1, 1, '文章一', '第一篇文章', '第一篇文章第一篇文章第一篇文章', '2021-06-17 17:36:11', 0);
INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (2, 1, '文章一2', '第一篇文章', '第一篇文章第一篇文章第一篇文章', '2021-06-17 17:36:11', 0);
INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (3, 2, '文章一3', '第一篇文章', '第一篇文章第一篇文章第一篇文章', '2021-06-18 11:38:56', 0);
INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (4, 3, '文章一4', '第一篇文章', '第一篇文章第一篇文章第一篇文章', '2021-06-18 11:38:56', 0);
INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (5, 2, '文章一5', '第一篇文章', '第一篇文章第一篇文章第一篇文章', '2021-06-18 11:38:56', 0);
INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (6, 4, '文章一6', '第一篇文章', '第一篇文章第一篇文章第一篇文章', '2021-06-18 11:38:56', 0);
INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (7, 4, '文章一7', '第一篇文章', '第一篇文章第一篇文章第一篇文章', '2021-09-29 17:45:29', 0);
INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (8, 5, '文章一8', '', '第一篇文章第一篇文章第一篇文章', '2021-09-29 17:50:52', null);
INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (9, 5, '文章一8', '第一篇文章', '第一篇文章第一篇文章第一篇文章', '2021-09-29 17:46:21', null);
INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (10, 5, '文章一8', '<第一篇文章>', '第一篇文章第一篇文章第一篇文章', '2021-09-29 17:46:21', null);
INSERT INTO testgroup.m_blog_two (id, user_id, title, description, content, created, status) VALUES (11, 6, '文章一8', '第一篇文章', '第一篇文章第一篇文章第一篇文章', '2021-09-29 17:45:29', null);

在这里插入图片描述

新增

新增一列hello,非空 int类型数据,初始值为100

alter table  testgroup.m_blog_two add hello INT NOT NULL DEFAULT 100;

发现加在了最后
在这里插入图片描述

新增一列newtime,默认为空 varchar类型数据,加在首列

alter table testgroup.m_blog_two add newtime varchar(255) NULL first ;

在这里插入图片描述

新增一列oldtime,时间类型,时间会根据数据变更自动更新为最新系统时间,放在newtime后

alter table testgroup.m_blog_two add oldtime datetime not null on update current_timestamp after newtime ;

在这里插入图片描述

新增一列threetime,时间类型,只会在新增的时候自动为当前时间,修改是不变的,放在newtime后

alter table testgroup.m_blog_two add threetime datetime DEFAULT CURRENT_TIMESTAMP;

在这里插入图片描述

删除

alter table testgroup.m_blog_two drop newtime;

在这里插入图片描述

改字段类型,新的属性应该和旧的属性是可以互换的。

比如int可以修改为bigint,但是varchar不可以修改为int,datetime也不可以修改为int。

change的语法:change后面的第一个字段为被修改的字段,第二个字段为修改后的字段,第三个字段为对应的属性。

alter table testgroup.m_blog_two change hello hello bigint;

改字段名称

alter table testgroup.m_blog_two change hello helloone bigint;

在这里插入图片描述

修改表名

alter table testgroup.m_blog_two rename to testgroup.m_blog_three;

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值