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——增删改操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 就像那飞蛾扑向火是哪首歌啊?
- 下一篇: 判断是否过期的算法_铁观音多久过期,怎么