欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 人文社科 > 生活经验 >内容正文

生活经验

mysql 插入删除操作_MySQL——增删改操作

发布时间:2023/11/27 生活经验 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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——增删改操作的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。