Deletions MySQL table change search operation

Table structure modification operation

Adding a field book table;

Format: alter table table add Field Name Field Type;

mysql> alter table book add count int;
Query OK, 0 rows affected (0.42 sec)
Records: 0  Duplicates: 0  Warnings: 0

  View table structure

mysql> desc book;  #简写
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| num   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
| datel | date        | YES  |     | NULL    |       |
| price | double(5,2) | YES  |     | NULL    |       |
| count | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
mysql> describe book;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| num   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
| datel | date        | YES  |     | NULL    |       |
| price | double(5,2) | YES  |     | NULL    |       |
| count | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

  To delete a field operation

mysql> alter table book drop count;
Query OK, 0 rows affected (0.41 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe book;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| num   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
| datel | date        | YES  |     | NULL    |       |
| price | double(5,2) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

  Modify the field operation

mysql> alter table book modify price int;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe book;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| num   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
| datel | date        | YES  |     | NULL    |       |
| price | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

  Insert

Format: insert into table (you want to insert the field names .....) values ​​(want to insert the value of the field);

insert into table values ​​(the values ​​of all fields in the table)

mysql> insert into book(num) values(1);
Query OK, 1 row affected (0.02 sec)

mysql> select * from book;
+------+------+-------+-------+
| num  | name | datel | price |
+------+------+-------+-------+
|    1 | NULL | NULL  |  NULL |
+------+------+-------+-------+
1 row in set (0.00 sec)

  Inserting two operation fields

mysql> insert into book(num,name) values(2,'chenxi');
Query OK, 1 row affected (0.03 sec)

mysql> select * from book;
+------+--------+-------+-------+
| num  | name   | datel | price |
+------+--------+-------+-------+
|    1 | NULL   | NULL  |  NULL |
|    2 | chenxi | NULL  |  NULL |
+------+--------+-------+-------+
2 rows in set (0.00 sec)

When you want to insert the data for all fields in this table. You can not write in front of the field name. But values must correspond to all the tables inside the field name .

The default format will be in control of your data table insert one to one.

mysql> insert into book values(3,'cv','2020.4.4','34');
Query OK, 1 row affected (0.39 sec)
mysql> select * from book;
+------+--------+------------+-------+
| num  | name   | datel      | price |
+------+--------+------------+-------+
|    1 | NULL   | NULL       |  NULL |
|    2 | chenxi | NULL       |  NULL |
|    3 | cv     | 2020-04-04 |    34 |
+------+--------+------------+-------+
3 rows in set (0.00 sec)

 Delete the operation 

Empty Table

mysql> DELETE FROM tf;
Query OK, 0 rows affected (0.00 sec)

  Delete this record num value of 1

mysql> delete from book where num=1;
Query OK, 1 row affected (0.04 sec)

mysql> select * from book;
+------+--------+------------+-------+
| num  | name   | datel      | price |
+------+--------+------------+-------+
|    2 | chenxi | NULL       |  NULL |
|    3 | cv     | 2020-04-04 |    34 |
+------+--------+------------+-------+
2 rows in set (0.00 sec)

  

Modify the record table

grammar:

update table set field name = new field values, ......

UPDATE table name SET column name = value WHERE column name = new old values

Modify price field of all values;

mysql> update book set price = 90;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from book;
+------+--------+------------+-------+
| num  | name   | datel      | price |
+------+--------+------------+-------+
|    2 | chenxi | NULL       |    90 |
|    3 | cv     | 2020-04-04 |    90 |
+------+--------+------------+-------+
2 rows in set (0.00 sec)

Modify where num = price 3 to 9

mysql> update book set price = 99 where num = 3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from book;
+------+--------+------------+-------+
| num  | name   | datel      | price |
+------+--------+------------+-------+
|    2 | chenxi | NULL       |    90 |
|    3 | cv     | 2020-04-04 |    99 |
+------+--------+------------+-------+
2 rows in set (0.00 sec)

Modifying a plurality of record fields. (Only separated number)

mysql> update book set name = 'linux', num = '1' where num = 3;
Query OK, 1 row affected (0.11 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from book;
+------+--------+------------+-------+
| num  | name   | datel      | price |
+------+--------+------------+-------+
|    2 | chenxi | NULL       |    90 |
|    1 | linux  | 2020-04-04 |    99 |
+------+--------+------------+-------+
2 rows in set (0.00 sec)

  

Guess you like

Origin www.cnblogs.com/rdchenxi/p/12630371.html