增删改
删除表
命令:drop table <表名>
例如:删除表名为 MyClass 的表
mysql> drop table MyClass;
插入数据
命令:insert into <表名> [( <字段名 1>[,..<字段名 n > ])] values ( 值 1 )[, ( 值 n )]
例子:
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
查询表中的数据
查询所有行
mysql> select * from MyClass;
查询前几行数据
例如:查看表 MyClass 中前 2 行数据
mysql> select * from MyClass order by id limit 0,2;
或者
mysql> select * from MyClass limit 0,2;
删除表中数据
命令:delete from 表名 where 表达式
例如:删除表 MyClass 中编号为 1 的记录
mysql> delete from MyClass where id=1;
修改表中数据
命令:update 表名 set 字段=新值,... where 条件
mysql> update MyClass set name='Mary' where id=1;
在表中增加字段
命令:alter table 表名 add 字段 类型 其他;
例如:在表 MyClass 中添加了一个字段 passtest,类型为 int(4),默认值为 0
mysql> alter table MyClass add passtest int(4) default '0'
更改表名
命令:rename table 原表名 to 新表名;
例如:在表 MyClass 名字更改为 YouClass
mysql> rename table MyClass to YouClass;
更新字段内容
命令:update 表名 set 字段名 = 新内容
update 表名 set 字段名 = replace(字段名, '旧内容', '新内容');
例如:文章前面加入 4 个空格
update article set content=concat(' ', content);
【修改表的语法】
一张表,创建完毕,有了N列.
之后还有可能要增加或删除或修改列
Alter table 表名 add 列名称 列类型 列参数; [加的列在表的最后]
例: alter table m1 add birth date not null default '0000-00-00';
Alter table 表名 add 列名称 列类型 列参数 after 某列 [把新列加在某列后]
例: alter table m1 add gender char(1) not null default '' after username;
Alter table 表名 add 列名称 列类型 列参数 first [把新列加在最前面]
例: alter table m1 add pid int not null default 0 first;
删除列:
Alter table 表名 drop 列名
修改列类型:
Alter table 表名 modify 列名 新类型 新参数
例:alter table m1 modify gender char(4) not null default '';
修改列名及列类型
Alter table 表名 change 旧列名 新列名 新类型 新参数
例:alter table m1 change id uid int unsigned;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)