栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MySql之alter

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/281940.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号