Addition, deletion and modification of SQL statements

Create a student table

mysql> desc student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | tinyint(4)  | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | NO   |     | NULL    |                |
| age   | tinyint(4)  | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

add table record

1. Add a record insert

语法:insert [into] tab_name (field1,filed2,.......) values (value1,value2,.......)

insert into student (id,name,age) values (1,'sumcet',20);
Query OK, 1 row affected (0.01 sec)

Display query ok, indicating that the addition is successful

2. Insert multiple pieces of data

mysql> insert into student (id,name,age) values (2,'bbu',20),
    -> (3,'sum',21),
    -> (4,'sam',22);
Query OK, 3 rows affected (0.02 sec)

3, set insert:

Syntax: insert [into] tab_name set field name=value

insert into student set name='samq',age=21;
Query OK, 1 row affected (0.01 sec)

Modify table records

Syntax: update tab_name set field1=value1,field2=value2,......[where statement]

mysql> select * from student;
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
|  1 | sumcet |  20 |
|  2 | bbu |  20 |
|  3 | sum    |  21 |
|  4 | sam    |  22 |
|  5 | samq |  21 |
+----+--------+-----+
5 rows in set (0.00 sec)

mysql> update student set name='qwer' where id=5;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
|  1 | sumcet |  20 |
|  2 | bbu |  20 |
|  3 | sum    |  21 |
|  4 | sam    |  22 |
|  5 | qwer   |  21 |
+----+--------+-----+
5 rows in set (0.00 sec)

The UPDATE syntax updates columns in the original table row with new values.

The SET clause indicates which columns to modify and which values ​​to give.

The WHERE clause specifies which rows should be updated. If there is no WHERE clause, all rows are updated.

delete table record

语法:delete from tab_name [where ....]

mysql> delete from student where id=5;
Query OK, 1 row affected (0.04 sec)

mysql> select * from student;
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
|  1 | sumcet |  20 |
|  2 | bbu |  20 |
|  3 | sum    |  21 |
|  4 | sam    |  22 |
+----+--------+-----+
4 rows in set (0.00 sec

If you do not follow the where statement, delete the data in the entire table

delete can only be used to delete a row of records

The delete statement can only delete the contents of the table, not the table itself. If you want to delete the table, use the drop table table name;

TRUNCATE TABLE can also delete all data in the table, the word statement first destroys the table, and then creates a new table. Data deleted in this way cannot be

In-transaction recovery

truncate table table name;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325651835&siteId=291194637