登录
首页 >  数据库 >  MySQL

MySQL——增删改操作

来源:SegmentFault

时间:2023-01-20 19:50:24 385浏览 收藏

你在学习数据库相关的知识吗?本文《MySQL——增删改操作》,主要介绍的内容就涉及到MySQL,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

插入语句

一次插入操作只插入一行数据

insert into [tablename](listname1,listname2,......) values (value1,value2,......);
/* 插入一条数据 */
insert into t_students(id,name,age) values(10,'敏敏',24);
/* 插入多条数据MySQL特有的 */
insert into t_students(id,name,age) values(10,'YY',24),(10,'MM',24),(10,'HH',24);
/* 插入查询结果 */
insert into t_students(name) select name from t_students;

更新操作

不能更改主键!!!

update [tablename] set [listname]=value where [条件];

/* 将年龄大于25的改为18 */
 update t_students set age=18 where age >= 25;
如果省略where,则整个表的数据都会被修改

删除操作

delete from [tablename] where [条件];

/* 删除年龄为18的数据 */
如果省略where,则整个表的数据都会被删除!!!

添加列

alter table [tablename] add [listname] [数据类型] after [listname插入位置]

/* 在表的最后追加列 address */
alter table students add address char(60);

/* 在名为 age 的列后插入列 birthday */
alter table students add birthday date after age;

修改列

 alter table [tablename] change [listname] [newlistname] [新数据类型];
 
 /* 将表 tel 列改名为 telphone */ 
 alter table students change tel telphone char(13) default "-";
 /*将 name 列的数据类型改为 char(16) */
 alter table students change name name char(16) not null;
 /* 当字段只包含空值时,类型大小都可以修改 */
 alter table [tablename] modify [listname] [数据类型];

删除列

alter table [tablename] drop [listname];

/* 删除 birthday 列 */
alter table students drop birthday;

重命名表

alter table [tablename] rename [newtablename];

/* 重命名 students 表为 workmates */
alter table students rename workmates;

以上就是《MySQL——增删改操作》的详细内容,更多关于mysql的资料请关注golang学习网公众号!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表