MySQL修改数据的超详细教程

吾爱主题 阅读:155 2022-11-14 16:02:00 评论:0

更新/修改数据

只改想改的数据:

update 表名 set 字段名=值 where 条件子句;

如:

?
1 update c1 set name = "库里30" where id=1;

修改全部数据:

?
1 update 表名 set 字段名1=值1,字段名2=值2...,字段名n=值n;

对于NULL不能用=符号,要用is null

修改表结构:

alter table 表名 修改的动作语法;

如:

?
1 alter table c1 modify name varchar (20) not null ;

说明:设置姓名不为空

修改数据类型:

alter table 表名 modify 字段 新数据类型;

如:

?
1 alter table c1 modify name varchar (20) not null ;

修改字段名:

alter table 表名 change 旧字段名 新字段名 新数据类型;

如:

?
1 alter table c3 change name name1 varchar (30) not null ;

修改表之增加主键:

alter table 表名 add constraint 约束名字 约束类型[字段];

如:

?
1 alter table c5 add constraint PK_c5_id primary key (id);

说明:PK_c5_id是约束名(指定主键约束为PK_c5_id,对大部分数据库有效但对于MySql无效,此主键约束名仍为primary)
在建表时给主键个性化名字较好

修改表名:

rename table 旧表名 to 新表名;

如:

?
1 rename table c5 to cc55;

建表后添加唯一性约束:

alter table 表名 add unique(字段名)

如:

?
1 alter table c9 add unique (id);

建表后添加默认值约束:

alter table 表名 alter 列名 set default’默认值’;

如:

?
1 alter table c11 alter name set default “欧”;

建表后添加非空约束:

alter 表名 modify 字段名字段类型not null

如:

?
1 alter table c12 modify id int not null ;

建表以后添加外键:

alter table 表名称 add foreign key (列名称) references关联表名称(列名称);

如:

?
1 altertable stuInfo add foreign key (scode) references score(studentID);

补充:mysql修改某个字段(替换关键字内容)

mysql修改某个字段(替换关键字内容),UPDATE 表名 SET 字段名= REPLACE( 替换前的字段值, '替换前关键字', '替换后关键字' ) WHERE 条件。

举例:

?
1 update goods_table SET goods_name = REPLACE ( goods_name, '2017' , '2018' ) where goods_name like '%2017%' ;

原来字段“2017新款”,执行之后“2018新款”

 总结

到此这篇关于MySQL修改数据的文章就介绍到这了,更多相关MySQL修改数据内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/shuang_waiwai/article/details/121194050

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容