[MySQL] Basic operations of MySQL on data

foreword

The first two blogs have concluded MySQL’s query on data, including basic and advanced queries. We finally finished the query part, but it’s still very, very important to talk about this part of the query. With the foundation of this part, we can continue Learn the knowledge behind MySQL. If you haven't learned this part yet, I suggest you read it first, and then come back to continue learning.

Portal:
[MySQL] Basic query of database .
[MySQL] Advanced query of database .

Next, we will formally learn this part

INSERT statement

After the database is created, we need to insert and exit data into the database. In MySQL, you can use the INSERTstatement to insert one or more rows of data into the existing database table of the database.

INSERTThere are two grammatical forms of statement, namely INSERT..VALUES....statement and INSERT...SET...statement. Next, we will summarize their similarities and differences respectively.

INSERT LALUES syntax format:

INSERT INTO  表名 (列名1,列名2,列名3...)VALUES(数值1,数值2,数值3...);

If you need to insert data into all the columns in the table, you can directly ignore the column name and directly use:

INSERT 表名 VALUES(数值1,数值2,数值3...);

INSERT SET syntax format:

INSERT INTO 表名 SET 列名1 =1,列名2=2,列名3=3...;

This kind of SQL statement is a dialect in MySQL, that is, it can only be used in MySQL, and it is more suitable for inserting data in a single row.

To summarize INSERTthe characteristics of the sentence:

  1. Using the INSERT…VALUESstatement , you can insert a row of data into the table, or you can insert multiple rows of data;
  2. Using the INSERT…SETstatement , you can specify the value of each column in the inserted row, or you can specify the value of some columns;
  3. INSERT…SELECTThe statement inserts data from other tables into the table;
  4. The value of some columns can be inserted into the table using the INSERT…SETstatement , which is more flexible;
  5. INSERT…VALUESStatements can insert multiple pieces of data at one time;
  6. Handling multiple inserts with a single statement is faster INSERTthan using multiple INSERTstatements;
  7. When INSERTinserting multiple rows with a single statement, you only need to enclose each row of data in parentheses;

Example:

create  table student(
   id    INT PRIMARY KEY NOT NULL,
   name  VARCHAR(4) NOT NULL,
   age   INT NOT NULL,
   sex   CHAR(1) NOT NULL,
   class VARCHAR(10) ,
   birthplace  varchar(10),
   id_teach INT UNSIGNED NOT NULL
);
INSERT student VALUE
('88201','张三',18,'男','软件211','浙江杭州',689),
('88202','李四',19,'男','软件212','河南郑州',898),
('88203','小红',18,'女','计算机211','北京',758),
('88204','王五',16,'男','软件214','浙江杭州',589);

output:
insert image description here
insert image description here

UPDATE statement

UPDATEThe function is to update the data in the table. The syntax for this is INSERTsimilar to the second usage of .must provideThe table name and SETexpression can be followed by a WHEREstatement to limit the range of records to be updated.

Syntax format:

UPDATE 表名 SET 字段1=1,字段2=2... WHERE 条件; 

Example: Zhang San became a woman

UPDATE student SET sex="女" WHERE name="张三";

output:
insert image description here

If you do not add a WHERE clause to restrict the condition UPDATE will update the value of all records in the table, such as:

UPDATE student SET sex="女" ;

Output:
insert image description here
That is to say, the SQL statement WHEREselects which rows of data need to be updated according to the statement, and then selects which fields need to be modified according to the SET statement.

DELETE statement

In MySQL, you can use the DELETEstatement to delete one or more rows of data from a table.

Syntax format:

DELETE FROM 表名 WHERE 条件

Example: Zhang San was expelled from school

DELETE FROM student WHERE name="张三";

output:
insert image description here

Note: When the WHEREclause condition is not used, all data will be deleted. as follows:

DELETE FROM student ;

output:
insert image description here
insert image description here

It should be noted here that the data in the table is deleted instead of the table, which DROPis different from the previous statement. The table here still exists, but it is just an empty table.

Epilogue

The SQL used to operate the database is generally divided into two types, one is the query statement, which is what we call the SELECTstatement, and the other is the update statement, also called the data manipulation statement, which is the INSERT、UPDATEsum DELETEstatement we are learning today.

So far, we have almost finished learning the SQL language, and the rest is to practice and master it a lot, persevere and work hard!

The next notice: the use of MySQL basic functions

Continually updated…

Guess you like

Origin blog.csdn.net/apple_51673523/article/details/122518070