SQL自学,mysql从入门到精通 --- 第 9天,INSERT、UPDATE、DELETE 的使用

INSERT VALUES

root@mysqldb 14:51:  [d1]> INSERT INTO bikes 
    -> VALUES
    -> ("AA","AA","10");
Query OK, 1 row affected (0.00 sec)

root@mysqldb 14:54:  [d1]> INSERT INTO bikes
    -> (type,prIce)
    -> VALUES
    -> ("B","100");
Query OK, 1 row affected (0.01 sec)


root@mysqldb 14:54:  [d1]> SELECT * FROM bikes;
+------+-------------+-------+
| type | description | price |
+------+-------------+-------+
| SS   | SHUSHIXING  |   200 |
| ZG   | ZUNGUIXING  |   500 |
| JC   | JICHUXING   |   150 |
| SDX  | SHANDIXING  |  1000 |
| AA   | AA          |    10 |
| B    | NULL        |   100 |
+------+-------------+-------+
6 rows in set (0.00 sec)


INSERT SELECT 语句

root@mysqldb 14:58:  [d1]> SELECT * FROM worker1;
+------+
| name |
+------+
|1  |
|2  |
|3  |
|4  |
|5  |
|6  |
+------+
6 rows in set (0.00 sec)

root@mysqldb 14:58:  [d1]> INSERT INTO worker1
    -> SELECT * FROM worker2;
Query OK, 6 rows affected (0.09 sec)
Records: 6  Duplicates: 0  Warnings: 0



root@mysqldb 14:59:  [d1]> SELECT * FROM worker1;
+------+
| name |
+------+
|1  |
|2  |
|3  |
|4  |
|5  |
|6  |
|1  |
|2  |
|3  |
|4  |
|5  |
|6  |
+------+
12 rows in set (0.00 sec)

root@mysqldb 15:01:  [d1]> INSERT INTO worker1 (name)
    -> SELECT name FROM worker2 WHERE name = '吕6';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0


root@mysqldb 15:03:  [d1]> SELECT * FROM worker1;
+------+
| name |
+------+
|1  |
|2  |
|3  |
|4  |
|5  |
|6  |
|1  |
|2  |
|3  |
|4  |
|5  |
|6  |
|6  |
+------+
13 rows in set (0.00 sec)


root@mysqldb 15:07:  [d1]> CREATE TABLE NN2 (  
    -> A1 INT,  
    -> B2 INT,  
    -> C3 INT  
    -> );
Query OK, 0 rows affected (0.01 sec)

root@mysqldb 15:07:  [d1]> SELECT * FROM nnum;
+------+------+------+------+
| n1   | n2   | n3   | n4   |
+------+------+------+------+
|    1 |    2 |    3 |    4 |
|   11 |   12 |   13 |   14 |
|   22 |   23 |   24 |   25 |
|    6 |    5 |   10 |    9 |
+------+------+------+------+
4 rows in set (0.00 sec)

root@mysqldb 15:07:  [d1]> INSERT INTO NN2
    -> (A1, B2, C3)
    -> SELECT n2, n3, n4 FROM nnum;
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

root@mysqldb 15:09:  [d1]> SELECT * FROM NN2;
+------+------+------+
| A1   | B2   | C3   |
+------+------+------+
|    2 |    3 |    4 |
|   12 |   13 |   14 |
|   23 |   24 |   25 |
|    5 |   10 |    9 |
+------+------+------+
4 rows in set (0.00 sec)


UPDATE 语句

root@mysqldb 15:19:  [d1]> UPDATE NN2
    -> SET A1='900' 
    -> WHERE A1='2';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

root@mysqldb 15:21:  [d1]> SELECT * FROM NN2;
+------+------+------+
| A1   | B2   | C3   |
+------+------+------+
|  900 |    3 |    4 |
|   12 |   13 |   14 |
|   23 |   24 |   25 |
|    5 |   10 |    9 |
+------+------+------+
4 rows in set (0.00 sec)

root@mysqldb 15:21:  [d1]> UPDATE NN2
    -> SET B2='800', C3='700'
    -> WHERE A1='900';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

root@mysqldb 15:23:  [d1]> SELECT * FROM NN2;
+------+------+------+
| A1   | B2   | C3   |
+------+------+------+
|  900 |  800 |  700 |
|   12 |   13 |   14 |
|   23 |   24 |   25 |
|    5 |   10 |    9 |
+------+------+------+
4 rows in set (0.00 sec)

-- 如果UPDATE语句中省略了WHERE子句,那么给定的表中所有的记录都会更新。
root@mysqldb 15:23:  [d1]> UPDATE NN2
    -> SET C3='500';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

root@mysqldb 15:25:  [d1]> SELECT * FROM NN2;
+------+------+------+
| A1   | B2   | C3   |
+------+------+------+
|  900 |  800 |  500 |
|   12 |   13 |  500 |
|   23 |   24 |  500 |
|    5 |   10 |  500 |
+------+------+------+
4 rows in set (0.01 sec)

root@mysqldb 15:25:  [d1]> UPDATE NN2 SET C3=C3*2;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

root@mysqldb 15:26:  [d1]> SELECT * FROM NN2;
+------+------+------+
| A1   | B2   | C3   |
+------+------+------+
|  900 |  800 | 1000 |
|   12 |   13 | 1000 |
|   23 |   24 | 1000 |
|    5 |   10 | 1000 |
+------+------+------+
4 rows in set (0.00 sec)


DELETE 语句

root@mysqldb 15:26:  [d1]> DELETE FROM NN2 WHERE A1=900;
Query OK, 1 row affected (0.00 sec)

root@mysqldb 15:27:  [d1]> SELECT * FROM NN2;
+------+------+------+
| A1   | B2   | C3   |
+------+------+------+
|   12 |   13 | 1000 |
|   23 |   24 | 1000 |
|    5 |   10 | 1000 |
+------+------+------+
3 rows in set (0.00 sec)

root@mysqldb 15:27:  [d1]> DELETE FROM NN2 WHERE A1>10;
Query OK, 2 rows affected (0.00 sec)

root@mysqldb 15:29:  [d1]> SELECT * FROM NN2;
+------+------+------+
| A1   | B2   | C3   |
+------+------+------+
|    5 |   10 | 1000 |
+------+------+------+
1 row in set (0.00 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.L-OAM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值