MySql database data update operations on its advanced application _ MySQL

Source: Internet
Author: User
MySql database data update operation its advanced application bitsCN.com

MySql database data update operations on its advanced applications

There are three types of data update operations: add data to the table, modify data in the table, and delete data in the table.

The data table used for demonstration uses three data tables: student, course, and SC. the specific content of the data table is in the PHP and MySql databases. how to obtain the total number of records for each category

Insert data

Insert tuples (one piece of data)

Format:

INSERT <表名> [( <属性列1> [, <属性列2> ]…)]

VALUES ( <常量1> [, <常量2> ]…);

If the attribute column does not appear in the INTO clause, the new element group takes null values.

However, table definitions indicate that columns with the not null attribute cannot take NULL values. Otherwise, an error occurs.

If the INTO Clause does not specify any column name, the new element group must have a value for each attribute.

Example 1.1

[SQL]

Insert into Student (Sno, Sname, Ssex, Sdept, Sage)

VALUES (

'123456', 'Chen Dong', 'male', 18, 'IS'

)

When inserting data from all columns in the corresponding table, the column name can be omitted without writing.

Therefore, the insert statement can also be written as follows:

[SQL]

Insert into Student

VALUES (

'20140901', 'chensi', 'male', 22, 'IS'

)

Insert multiple data entries

When inserting multiple rows of data, it is similar to inserting a piece of data, but each piece of data needs to be expanded in parentheses and separated by commas.

When inserting data from all columns in the corresponding data table, the column name can be omitted without writing.

Example 1.2

[SQL]

Insert into Student

VALUES (

'000000', 'caasec', 'male', 25, 'CS'

),(

'123', 'line', 'female ', 18, 'CS'

),(

'20140901', 'Taobao', 'male', 30, 'CS'

)

Insert subquery results

Subqueries can be nested in INSERT statements to generate batch data to be inserted.

Format:

INSERT

INTO <表名> [( <属性列1> , <属性列2> , <属性列3> )]

Subquery;

Example 1.3

Requirement: calculate the average score of the students for each department and store the results in the database.

First, create a database:

[SQL]

Create table Dept_age

(Sdept char (15), Avg_age smallint );

Group the student table by department, calculate the average age of each department, and store the department name and average age in the new table.

[SQL]

Insert into dept_age (Sdept, Avg_age)

SELECT Sdept, AVG (Sage)

FROM Student

Group by Sdept

Insert result:

Example 1.4

Requirement: count the number of people in each department and store the results in the database.

First, create a data table that stores the query results:

[SQL]

Create table Sdept_num

(Sdept char (15 ),

Snum int );

Group the student table by department, calculate the total number of student tables, and store the department name and total number of student tables to the new table.

[SQL]

Insert into sdept_num (Sdept, Snum)

SELECT Sdept, COUNT (Sno)

FROM student

Group by Sdept

Insert result:

Modify data

Data modification is also called an update operation. the Statement format is as follows:

UPDATE <表名>

SET <列名> = <表达式> [, <列名> = <表达式> ]…

[WHERE <条件> ];

Modify the value of a tuples

Change the student's age to 22

[SQL]

UPDATE Student SET Sage = 20 WHERE Sname = 'kakaxi'

Modify values of multiple tuples

Increase the age of all students by one year

[SQL]

UPDATE Student SET Sage = Sage + 1

Modify statement of the query

Subqueries can also be nested in UPDATE statements to construct modified conditions.

Set the score of all students in the computer science department to zero.

[SQL]

Update SC SET Grade = 0 WHERE EXISTS (

SELECT *

FROM Student

Where SC. Sno = Student. Sno

AND Student. Sdept = 'ais'

)

Delete data

Format:

DELETE

FROM <表名>

[WHERE <条件> ];

Deletes the value of a specified tuples.

Delete the data of Kakashi students

[SQL]

Delete from Student WHERE Sname = 'kakaxi'

Delete values of multiple tuples

Delete course selection records of all students (that is, to clear the SC table, but not to delete it)

[SQL]

Delete from SC;

Clearing a table is a very dangerous operation. make sure that the data in the table is useless.

The deletion statement of the query.

Subqueries can also be nested in the DELETE statement to construct conditions for performing the DELETE operation.

Delete course selection records for all students in the Computer Science Department

[SQL]

Delete from SC WHERE EXISTS (

SELECT *

FROM Student

Where SC. Sno = Student. Sno

AND Student. Sdept = 'ais'

)

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.