Table of contents
SQL usage encyclopedia
Introduction to SQL
SQL, the full name of Structured Query Language, is a standard language for managing relational databases. SQL language includes data definition language (DDL), data manipulation language (DML), data control language (DCL), etc. This article will introduce in detail the common operations of SQL, including table creation, query, modification, deletion and other operations.
CREATE TABLE
Introduction to CREATE TABLE usage
CREATE TABLE is part of SQL's data definition language (DDL) and is used to create new database tables. Its basic syntax is as follows:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Create user table using CREATE TABLE
For example, we can use CREATE TABLE to create a table named Users, containing three fields: id, name, and email:
CREATE TABLE Users (
id INT,
name VARCHAR(100),
email VARCHAR(100)
);
SELECT and FROM
Introduction to SELECT and FROM usage
SELECT and FROM are part of SQL's Data Manipulation Language (DML) and are used to query data from database tables. SELECT is used to specify the columns to be queried, and FROM is used to specify the table to be queried. Its basic syntax is as follows:
SELECT column1, column2, ...
FROM table_name;
Query user tables using SELECT and FROM
For example, we can use SELECT and FROM to query the id and name of all users from the Users table:
SELECT id, name
FROM Users;
ALTER TABLE
Introduction to ALTER TABLE usage
ALTER TABLE is part of SQL's data definition language (DDL) and is used to modify the structure of database tables. Its basic syntax is as follows:
ALTER TABLE table_name
ADD column_name datatype;
Use ALTER TABLE to modify the field names of user tables
For example, we can use ALTER TABLE to rename the name field of the Users table to username:
ALTER TABLE Users
RENAME COLUMN name TO username;
Use ALTER TABLE to modify the field type of the user table
For example, we can use ALTER TABLE to change the type of the id field of the Users table to VARCHAR:
ALTER TABLE Users
ALTER COLUMN id TYPE VARCHAR;
Use ALTER TABLE to modify field comments of user tables
For example, we can use ALTER TABLE to add comments to the email field of the Users table:
ALTER TABLE Users
COMMENT ON COLUMN email IS '用户的电子邮件地址';
WHERE
Introduction to WHERE usage
WHERE is part of SQL's data manipulation language (DML) and is used to set conditions when querying. Its basic syntax is as follows:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Query user table using AND of WHERE
For example, we can use the AND of WHERE to query the user with id 1 and name 'John' from the Users table:
SELECT *
FROM Users
WHERE id = 1 AND name = 'John';
Query user table using OR of WHERE
For example, we can use the OR of WHERE to query users with id 1 or name 'John' from the Users table:
SELECT *
FROM Users
WHERE id = 1 OR name = 'John';
Query user table using IN of WHERE
For example, we can use the IN of WHERE to query users with IDs 1, 2, and 3 from the Users table:
SELECT *
FROM Users
WHERE id IN (1, 2, 3);
Query the user table using BETWEEN of WHERE
For example, we can use BETWEEN of WHERE to query users with IDs between 1 and 3 from the Users table:
SELECT *
FROM Users
WHERE id BETWEEN 1 AND 3;
Query user table using LIKE of WHERE
For example, we can use LIKE of WHERE to query users whose names start with 'J' from the Users table:
SELECT *
FROM Users
WHERE name LIKE 'J%';
UPDATE
Introduction to UPDATE usage
UPDATE is part of SQL's data manipulation language (DML) and is used to modify data in database tables. Its basic syntax is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Use UPDATE to modify user table records
For example, we can use UPDATE to change the name of the user with id 1 in the Users table to 'Jack':
UPDATE Users
SET name = 'Jack'
WHERE id = 1;
DELETE
Introduction to DELETE usage
DELETE is part of SQL's data manipulation language (DML) and is used to delete data in database tables. Its basic syntax is as follows:
DELETE FROM table_name WHERE condition;
Use DELETE to delete records from the user table
For example, we can use DELETE to delete the user with id 1 in the Users table:
DELETE FROM Users
WHERE id = 1;
ORDER BY
Introduction to ORDER BY usage
ORDER BY is part of SQL's data manipulation language (DML) and is used to sort query results. Its basic syntax is as follows:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
Query user table using ORDER BY
For example, we can use ORDER BY to query all users from the Users table and sort by id in descending order:
SELECT *
FROM Users
ORDER BY id DESC;
GROUP BY
Introduction to GROUP BY usage
GROUP BY is part of SQL's data manipulation language (DML) and is used to group query results. Its basic syntax is as follows:
SELECT column1, column2, ...
FROM table_name
GROUP BY column1, column2, ...;
Query user table using GROUP BY
For example, we can use GROUP BY to query the number of users for each name from the Users table:
SELECT name, COUNT(*)
FROM Users
GROUP BY name;
HAVING
Introduction to HAVING usage
HAVING is part of the data manipulation language (DML) of SQL and is used to filter the results of GROUP BY. Its basic syntax is as follows:
SELECT column1, column2, ...
FROM table_name
GROUP BY column1, column2, ...
HAVING condition;
Use HAVING to query the user table
For example, we can use HAVING to query the names of users whose number is greater than 1 from the Users table:
SELECT name, COUNT(*)
FROM Users
GROUP BY name
HAVING COUNT(*) > 1;
Summarize
The above is a detailed introduction to common SQL operations, including table creation, query, modification, deletion and other operations. I hope it helps. If you have any questions, please leave a message for discussion.