MySQL Chapter 5 Adding, Deleting, Modifying, and Checking MySQL Table Data

Chapter 5 Adding, Deleting, Modifying, and Checking MySQL Table Data

5-1 DML statement inserting table data
5-2 Worm replication
5-3 DML statement table data modification and deletion
5-4 DQL statement simple query

5-1 Insert table data of DML statement

DML statement

DML (Data Manipulation Language) is used to add, delete, and modify data in tables in the database. Keywords: insert,
delete, update, etc.

insert record

1. Keyword description

INSERT INTO table name-indicates which table to add data to
(field name 1, field name 2, –) – which fields to change
VALUES (value 1, value 2, –); – no value

2. Pay attention

  • Values ​​and fields must correspond to the same number and type
  • The data size of the value must be within the length of the field
  • Except for numeric types, values ​​of other field types must be enclosed in quotation marks. (single quotes recommended)
  • If you want to insert a null value, you can leave the field blank, or insert null

Insert all fields

  • All field names are written out
    INSERT INTO table name (field name 1, Ning Duan name 2, field name 3...) VALUES (value 1, value 2, value 3);
  • Do not write field name
    INSERT INTO table name VALUES C value 1, value 2, value 3-);

insert partial data

INSERT INTO table name (field name 1, field name 2, ...) VALUES (value 1, value 2, ...);
fields without added data will use NULL

Specific operation:

Create a db2 database and use it.

CREATE DATABASE db2 ;
usE db2;

Create a complete student information form, including the student's id, name, age, gender, home address, phone number, birthday, math scores, English scores

CREATE TABLE student (
id int,
name varchar(20),
age int,
sex char(1) ,
address varchar(200),
phone varchar(20),
birthday date,
math doub1e,
english double
);

Insert some data, add id, name, age, sex, address data to the student table

INSERT INTO student (id , name ,age,sex, address)values(1, 'Zhang San', 19, 'Male', 'Beijing');

Tip: Use SELECT * FROM table name; test if the insert is successful
insert image description here

5-2 Worm Replication

What is worm replication

Based on the existing data, copy the original data and insert it into the corresponding table

Syntax format: INSERT INTO table name 1SELECT * FROM table name 2;

Function: Copy the data in table name 2 to table name 1

Specific operation:

  • Create the student2 table, the student2 structure is the same as the student table structure

CREATE TABLE student LIKE student2;

  • Add data from student table to student2 table

INSERT INTO student SELECT * FROM student2;

Note: If you only want to copy the name and age field data in the student table to the student2 table, use the following format

INSERT INTO student2(name ,age)SELECT name ,age FROM student;

5-3 Modification and deletion of table data in DML statements

Update table records
1. Modify data without conditions

UPDATE table name SET field name = value;

2. Modify data with conditions

UPDATE table name SET field name = value WHERE field name = value;

3. Keyword Description

UPDATE: Modify data
SET: Modify which fields
WHERE: Specify conditions

4. Specific operation:

Modify the data without conditions and change all genders to female

UPDATE student SET sex ='woman';

Modify multiple columns at a time, change the age of the student whose id is 3 to 26 years old, and change the address to Beijing

UPDATE student SET age=26,address=‘北京’ WHERE id=3;

delete table record

1. Delete data DELETEFROM without conditions;
2. Delete data with conditions DELETEFROM table name WHERE field name = value;

3. Specific operation

  • Delete data with conditions, delete the record with id 3
    DELETE FROM student WHERE id=3 ;
  • Delete data without conditions, delete all data in the table
    DELETE FROM student;

I
truncate delete table records

TRUNCATE TABLE table name;

The difference between truncate and delete:

  • delete is to delete the data in the table one by one
  • Truncate is to destroy the entire table and recreate a new table, the new table structure is exactly the same as the original table structure

5-4 Simple query of DQL statement

**DQL (Data Query Language)** Data Query Language is
used to query the records (data) of the table in the database. Keywords: select, where, etc.

Note: The query does not modify the data in the database, it is just a form of displaying the data

Query all data in the table

1. Use * to indicate all columns: SELECT * FROM table name;

2. Write out the field name of each column to be queried: SELECT id, name, age FROM table name;

Query the data of the specified column: SELECT id, name, age, ... FROM table name;

Note: 2 is more efficient than 1, 1 retrieves all information except fields

Alias ​​query

1. Specifying aliases for columns and tables when querying requires the use of the AS keyword

2. The advantage of using aliases is to facilitate viewing and processing of the queried data

SELECT field name 1 AS alias, field name 2 AS alias... FROM table name;

Among them, AS can be omitted without writing

clear duplicate values

1. Query the specified column and the result does not have duplicate data

SELECT DISTINCT field name 1, field name 2 FROM table name;

2. The query results participate in the operation

1. Operation of a column of data and fixed value: SELECT column name 1 + fixed value FROM table name;

2. A column of data and other column data participate in the operation: SELECT column name 1 + column name 2 FROM table name;

Note: Participating in the operation must be a numeric type

Requirement: Math + English score

select math+english (as) total grade from student;

Requirements: Age +10

select name name, age+10 as age from student;

Guess you like

Origin blog.csdn.net/weixin_44411458/article/details/124320559