MySQL learning 13: records in the operation data table (1)

Source: Internet
Author: User
Operations on data tables in the MySQL database include record insertion, record modification, record deletion, and record query. In short, it is the addition, deletion, modification, and query of records in the data table. The insert record of a data table in the MySQL database includes three forms. We have used the insert record before operating the data table. That

Operations on data tables in the MySQL database include record insertion, record modification, record deletion, and record query. Simply put, it is the addition, deletion, modification, and query of records in the data table. The insert record of a data table in the MySQL database includes three forms. We have used the insert record before operating the data table. That

Operations on data tables in the MySQL database include record insertion, record modification, record deletion, and record query. To put it simply

Is the addition, deletion, modification, and query of records in the data table.

1. Insert record

There are three types of insert records for data tables in the MySQL database. We have used insert records when operating data tables.

This is just one of the most commonly used methods. Next let's look at the three insert methods of records:

(1) INSERT command

The first syntax format for inserting records in a data table in MySQL database is also the most common syntax format:

INSERT [INTO] table_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT },...), (...),...;

Example:

When writing records to a data table, we first create a data table and view all fields:

Create table users3 (

Id smallint unsigned AUTO_INCREMENT primary key,

Username VARCHAR (20) not null,

Password VARCHAR (32) not null,

Age tinyint unsigned not null default 10,

Sex BOOLEAN

);

DESC users3;


1) omitting all fields

The insert record below lists various insert fields:

INSERT users3 VALUES (NULL, 'Tom ', '123', 123 );

INSERT users3 VALUES (NULL, 'john', '123456', DEFAULT, 0 );

INSERT users3 VALUES (DEFAULT, 'Rose ', '123', 323 );

INSERT users3 VALUES (DEFAULT, 'Paul ', '000000', 23-5 + 123 );

SELECT * FROM users3;


INSERT users3 VALUES (DEFAULT, 'Tom ', '123', 25 );


The error above indicates that the inserted records do not match the number of columns. Therefore, if we omit all field names when inserting records

Insert values of the same number as the number of columns in sequence. Otherwise, the writing of records fails. Remember this when writing records to the data table.

2) Insert a specified field:

INSERT users3 (username, password, age) VALUES ('kobe', '123', 23 );

SELECT * FROM users3;


3) Insert multiple records:

INSERT users3 VALUES (NULL, 'Dave ', '123', 456), (NULL, 'jack', '123', 24, 1 );

SELECT * FROM users3;


(2) insert set command

The second syntax format for inserting records into a data table in MySQL database is:

INSERT [INTO] table_name SET col_name = {expr | DEFAULT },...;

Note: The difference between this method and the first method of inserting records is that SubQuery can be used in this method ). There are three scenarios for triggering subqueries:

There is a case called a subquery caused by a comparison operator. Here the equal sign (=) is also a typical comparison operator. This method also has

The difference between one method is that the first method can insert multiple records at a time, but the second method can insert only one record at a time.

Example:

INSERT users3 SET username = 'jord', password = '000000 ';

SELECT * FROM users3;


(3) insert select command (superoutline)

The third syntax format for inserting records into a data table in MySQL database is:

INSERT [INTO] table_name [(col_name,...)] SELECT ...;

Description: This method can insert query results to a specified data table. Since we know little about SELECT statements, it is not appropriate now.

But we still write the example as usual. after learning the query records, we can go back and learn again.

Example:

1. Create an empty data table test before inserting a record:

Create table test (

Id tinyint unsigned primary key AUTO_INCREMENT,

Username VARCHAR (20)

);

2. Insert the username of the record whose age is greater than 25 in Table users3 to table test:

INSERT test (username) SELECT username FROM users3 WHERE age> 25;

3. query the test record of the data table:

SELECT * FROM test;


2. New Record

Updating data table records in the MySQL database is not uncommon. Simply put, it is to change some fields of some records or assign values again.

And so on. Update record methods include single-Table update and multi-Table update. Here we only learn single-Table update, and multi-Table update is described later.

Let's take a look at the syntax format for updating a single table:

UPDTAE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1 = {expr1 | DEFAULT} [, col_name2 =

{Expr2 | DEFAULT}]... [WHERE where_condition];

(1) omitting the WHERE keyword

If the keyword WHERE is omitted, all records will be updated.

Example:

1) Update the column: SELECT * FROM users3;

UPDATE users3 SET age = age + 5;

SELECT * FROM users3;


2) Update multiple columns:

SELECT * FROM users3;

UPDATE users3 SET age = age-id, sex = 0;

SELECT * FROM users3;


(2) specifying several records under the WHERE keyword

UPDATE users3 SET age = age + 10 WHERE id % 2 = 0;

SELECT * FROM users3;


Iii. delete records

Since the records of data tables in the MySQL database are inserted and updated, there are certainly deletion records. There are also two ways to delete records: single

Table deletion and multi-Table deletion are also described here only for single table deletion.

Syntax format for deleting a single table:

Delete from table_name [WHERE where_condition];

Example:

Specify the conditions for deletion. If you do not specify which records to delete, all records will be deleted.

Delete from users3 WHERE id = 6;

SELECT * FROM users3;


Suppose we re-insert a record, what is the value of the id field of the inserted record?

INSERT users3 VALUES (NULL, 'bob', '123', 234 );

SELECT * FROM users3;


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.