[MySQL Beginner Series 3]: Add, delete and modify operations of MySQL

In MySQL, the operation on data is divided into four typical operations of "addition, deletion, modification and query". We have already briefly talked about the installation of mysql and other introductions. This blog will explain in detail the "add, delete and modify" operations in MySQL.

[MySQL Beginner Series 1]: Teach you how to get started with MySQL

[MySQL Beginner Series 2]: Teach you how to get started with MySQL - database and data table operations

1. SQL statement rules

Each development language has its own grammatical rules. When writing SQL statements, the following writing rules must be followed:

1.1 The SQL statement should end with a semicolon;

In a relational database, SQL statements are executed one by one, one SQL statement represents an operation of the database, and a semicolon indicates the end of the sentence.
Except that some databases do not require a semicolon as a statement terminator.

1.2 Words in SQL statements need to be separated by English spaces

Statements that are not delimited will result in an error and cannot be executed normally.

1.3 SQL statements are case insensitive

However, in actual development, keywords are usually uppercase, and database names, table names, and column names are lowercase.
When actually writing SQL statements, mixed uppercase and lowercase letters will make the code less readable, so a uniform uppercase and lowercase style should be used. .

1.4 SQL statement comments

Comments in SQL statements consist of a hyphen – or #, and a comment block enclosed between /* and */. Comments can improve the readability and maintainability of your code.

1.5 Keywords in SQL Statements

The keywords in the SQL statement represent a certain type of operation, such as SELECT, INSERT, UPDATE, DELETE, and these keywords are only used to perform this type of operation.

2. How to add new data records

Add new data record syntax:

INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...);

Among them, the INSERT INTO statement is the key statement for inserting new data records into the MySQL database, table_name indicates the name of the data table to be inserted into the data, column1, column2, column3, etc. are the names of the columns in the table, and value1, value2, value3, etc. The inserted data value.

For example, we want to insert a new record into the table named "students", the record contains three fields "ID", "Name" and "Age", the corresponding INSERT INTO statement is as follows:

INSERT INTO students (ID, Name, Age) VALUES (1, ‘张三’, 20);

This SQL statement means to insert a new record in the "students" table, the ID of this record is 1, the Name is "Zhang San", and the Age is 20.

For another example, to insert multiple pieces of data:

插入一些数据到 "books" 数据表
INSERT INTO books(title, author, price) VALUES('C++ Programming', 'John Smith', 29.99);
INSERT INTO books(title, author, price) VALUES('Java Programming', 'Jane Doe', 39.99);
INSERT INTO books(title, author, price) VALUES('Python Programming', 'Bob Johnson', 24.99);

In addition, the INSERT INTO statement can also insert a set of data through a subquery, for example:


INSERT INTO students (ID, Name, Age) SELECT ID, Name, Age FROM other_students WHERE Age > 20;

The meaning of the above SQL statement is: select the student information whose age is greater than 20 from the table named "other_students", and then insert it into the "students" table.

3. How to delete existing data records

The syntax format is:

 DELETE FROM table_name WHERE condition;

Among them, DELETE FROM means to delete data records from the specified table, table_name means the name of the specified table, and the WHERE statement is optional, which means the specified condition.

For example, if we want to delete a record with an age of 20 from a table named "students", the corresponding SQL statement would be this:

DELETE FROM students WHERE Age = 20;

If no WHERE clause is specified, all records in the table will be deleted:

DELETE FROM students;

Special attention: When executing the DELETE statement, make sure that the data you delete is really needed to be deleted, because the MySQL DELETE statement has no rollback function (that is, it is irreversible after deletion).

4. How to modify existing data records

The syntax format is:

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

Among them, UPDATE means to update the data records in the specified table, table_name means the name of the specified table, the SET statement is used to set the field to be updated and its corresponding new value, and the WHERE statement is used to specify the update condition.

For example, if we want to update the name and age of the student whose "ID" is 1 in the table named "students", the corresponding SQL statement is as follows:


UPDATE students SET Name = ‘李四’, Age = 22 WHERE ID = 1;

The meaning of this SQL statement is: update the "Name" of the row with "ID" as 1 in the "students" table to "Li Si" and "Age" to 22.

5. How to query data records

Querying data records is one of the most basic and common operations in MySQL.
The following is a sample SQL statement to query data records:

SELECT * FROM table_name;

Among them, "table_name" is the name of the table to be queried, and "*" means to query all fields in the table. This statement queries all data records in the table.

For example, to query all the data in the books table:

SELECT * FROM books;

Usually, we filter data based on some criteria. At this time, you can use the WHERE clause to specify the query conditions. For example:

SELECT * FROM table_name WHERE column_name = 'value';

The above statement will query the records in the table where the "column_name" field is equal to "value".

In addition to the WHERE clause, you can also use the ORDER BY clause to sort by a field, and use the LIMIT clause to limit the number of queries. For example:

SELECT * FROM table_name WHERE column_name = 'value' ORDER BY column_name2 DESC LIMIT 10;

The above statement will query the records whose "column_name" field is equal to "value" in the table, sort them in descending order according to the "column_name2" field, and only return the first 10 records.

6. Learn efficient and practical features such as constraints, automatic growth, and foreign keys in the SQL language

In the use of MySQL database, there are many other grammars in SQL statements, including constraints, auto-increment, triggers, stored procedures, views, Indexes (indexes), cursors (cursors), etc.,
MySQL provides some powerful features to ensure data integrity and consistency, the following are some of the efficient and practical features:

  1. Constraint

Constraints in MySQL mainly include primary key constraints, unique constraints, non-null constraints, and foreign key constraints. By defining constraints, we can effectively guarantee the integrity and consistency of data.

For example, we can use the following statement to define a table called "users" and add a primary key constraint on the "id" field:

CREATE TABLE users (
  id INT(11) NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
);

In the above example, after the "id" field is set as a primary key constraint, the same value cannot be inserted repeatedly, thus ensuring the uniqueness of the data.

  1. automatic growth

The AUTO_INCREMENT attribute is provided in MySQL, which can make a field automatically increase when new data is inserted, which facilitates data processing.

For example, in the "users" table defined above, we can set the "id" field to auto-increment:

CREATE TABLE users (
  id INT(11) NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
);

This way, when inserting new data, you don't need to manually specify a value for the "id" field, and the database will automatically generate an incrementally unique value for it.

  1. Foreign Keys

The foreign key in MySQL is a very useful constraint feature, which establishes an association between multiple tables to ensure its integrity and consistency. By defining the foreign key relationship, you can easily realize the automation performance such as cascading operations between tables, and automate operations such as insertion, deletion, and update.

For example, we can create an order table called "orders" while defining the "users" table, and define a foreign key pointing to the "id" field in the "users" table:

CREATE TABLE orders (
  id INT(11) NOT NULL AUTO_INCREMENT,
  user_id INT(11),
  product_name VARCHAR(255) NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (user_id) REFERENCES users(id)
);

In the above example, the "user_id" field in the "orders" table has a foreign key relationship with the "id" field in the "users" table. In this way, when inserting data into the "orders" table, only the "id" value that already exists in the "users" table can be inserted, which ensures the interrelationship between the data.

The above is a simple addition, deletion, modification and query operation. As an entry-level tutorial, it is still very simple.

Please note that the examples only cover a small part of MySQL's functionality, please refer to more MySQL documentation and tutorials for more information and practical applications.

Guess you like

Origin blog.csdn.net/fumeidonga/article/details/131144620