表的複製:
語法:create table 表名 as select語句;
將查詢結果當做表建立出來。
將查詢結果插入到一張表中:
insert into dept1 select * from dept;
將一張表中的dept查詢結果當做資料插入前面表中
修改資料:update(注意當中標點符號)
語法格式:update 表名 欄位名1=值1,欄位名2=值2。。。where 條件;
注意沒有條件整張表資料全部更新。
mysql> select * from t_student;
+------+------+------+------------+----------+
| no | name | *** | classno | birth |
+------+------+------+------------+----------+
| null | zs | 1 | null | null |
| 1 | jack | 0 | gaosan2ban | 2021-2-5 |
| 2 | rose | 1 | gs2b | 2021-2-5 |
| 3 | abc | 1 | gs2b | 2021-2-5 |
+------+------+------+------------+----------+
4 rows in set (0.00 sec)
mysql> update t_student set name='aaa',***='0' where no=3;
query ok, 1 row affected (0.01 sec)
rows matched: 1 changed: 1 warnings: 0
mysql> select * from t_student;
+------+------+------+------------+----------+
| no | name | *** | classno | birth |
+------+------+------+------------+----------+
| null | zs | 1 | null | null |
| 1 | jack | 0 | gaosan2ban | 2021-2-5 |
| 2 | rose | 1 | gs2b | 2021-2-5 |
| 3 | aaa | 0 | gs2b | 2021-2-5 |
+------+------+------+------------+----------+
更新所有記錄:
update t_student set classno=『da4』,birth=『2021-2-6』;
mysql> update t_student set classno='da4',birth='2021-2-6';
query ok, 4 rows affected (0.01 sec)
rows matched: 4 changed: 4 warnings: 0
mysql> select * from t_student;
+------+------+------+---------+----------+
| no | name | *** | classno | birth |
+------+------+------+---------+----------+
| null | zs | 1 | da4 | 2021-2-6 |
| 1 | jack | 0 | da4 | 2021-2-6 |
| 2 | rose | 1 | da4 | 2021-2-6 |
| 3 | aaa | 0 | da4 | 2021-2-6 |
+------+------+------+---------+----------+
刪除資料
語法格式:delete from 表名 where 條件;
注意:沒有條件全部刪除。
mysql> select * from t_student;
+------+------+------+---------+----------+
| no | name | *** | classno | birth |
+------+------+------+---------+----------+
| null | zs | 1 | da4 | 2021-2-6 |
| 1 | jack | 0 | da4 | 2021-2-6 |
| 2 | rose | 1 | da4 | 2021-2-6 |
| 3 | aaa | 0 | da4 | 2021-2-6 |
+------+------+------+---------+----------+
4 rows in set (0.00 sec)
mysql> delete from t_student where no=1;
query ok, 1 row affected (0.01 sec)
mysql> select * from t_student;
+------+------+------+---------+----------+
| no | name | *** | classno | birth |
+------+------+------+---------+----------+
| null | zs | 1 | da4 | 2021-2-6 |
| 2 | rose | 1 | da4 | 2021-2-6 |
| 3 | aaa | 0 | da4 | 2021-2-6 |
+------+------+------+---------+----------+
注意:我在刪除no=null時不成功,但用name='zs』時可刪除。
mysql> select * from t_student;
+------+------+------+---------+----------+
| no | name | *** | classno | birth |
+------+------+------+---------+----------+
| null | zs | 1 | da4 | 2021-2-6 |
| 2 | rose | 1 | da4 | 2021-2-6 |
| 3 | aaa | 0 | da4 | 2021-2-6 |
+------+------+------+---------+----------+
3 rows in set (0.00 sec)
mysql> delete from t_student where name='zs';
query ok, 1 row affected (0.01 sec)
mysql> select * from t_student;
+------+------+------+---------+----------+
| no | name | *** | classno | birth |
+------+------+------+---------+----------+
| 2 | rose | 1 | da4 | 2021-2-6 |
| 3 | aaa | 0 | da4 | 2021-2-6 |
+------+------+------+---------+----------+
刪除所有記錄:
select * from 表名;
怎麼刪除大表?(重點)
truncate table 表名;//表被截斷,不可回滾,永久丟失
資料庫的表查詢改刪
選擇資料庫 use mytest create tablemytest id int primary key auto increment unsigned engine innodb alter table student engine innodb 檢視資料庫當中有哪些表 檢視表結構 show ...
資料庫表複製
select into from 和 insert into select都是用來複製表,兩者的主要區別為 select into from 要求目標表不存在,因為在插入時會自動建立。insert into select from 要求目標表存在 備份表資料 create table emp as ...
資料庫表資料複製
同乙個資料庫中不同表之間的資料複製,比如我想將乙個資料庫demo中的table1表的資料複製到demo資料庫中表table2中 我們可以這樣寫 insert into table1 name,password money select name,password money from table2 ...