SQL (Structured Query Language) is a structured query language used to manage and operate relational databases. It is one of the most widely used languages in database management systems.
-- Query data
SELECT * FROM table name WHERE condition;
--Insert data
INSERT INTO table name (column 1, column 2) VALUES (value 1, value 2);
--Update data
UPDATE table name SET column 1 = value 1 WHERE condition;
-- Delete data
DELETE FROM table name WHERE condition;
--Create table
CREATE TABLE table name (
Column name 1 data type,
Column name 2 data type
);
INT: Integer type.VARCHAR(n):Variable length character string, up toncharacters.DATE:Date type.FLOAT: Floating point type.BOOLEAN: Boolean value (true or false).MySQL is a popular open source relational database management system (RDBMS) that uses SQL as the query language and is suitable for small to medium-sized to large applications.
mysql -u root -p
CREATE DATABASE example_db;
USE example_db;
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), age INT);
INSERT INTO users (name, age) VALUES ('Alice', 30);
SELECT * FROM users;
SQLite is an embedded database that does not require a separate server for management and is suitable for lightweight applications.
sqlite3 example.db
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);
INSERT INTO users (name, age) VALUES ('Alice', 30);
SELECT * FROM users;
PostgreSQL (often referred to as Postgres) is a powerful open source object-relational database system (ORDBMS). It is based on more than 30 years of active development and has a strong reputation for reliability, functional robustness, and performance, and is known as "the world's most advanced open source database."
PostgreSQL adopts a client-server architecture and assigns independent handlers to each connection, which ensures that the crash of a single connection will not affect the stability of the entire database server.
The power of Postgres comes largely from its rich ecosystem extension:
| advantage | shortcoming |
|---|---|
| High data integrity and security | When writing large amounts of data at a high frequency, the VACUUM mechanism may cause performance fluctuations. |
| Support complex data analysis and business logic | Memory consumption is generally higher compared to MySQL |
| The open source community is extremely active and there is no vendor lock-in | There are many parameters to set, and beginners need a long learning curve to configure and optimize performance. |
T-SQL, whose full name is Transact-SQL, is an extended version of the SQL standard developed by Microsoft and Sybase. It is the core communication language for Microsoft SQL Server and Azure SQL-related services. Compared with standard SQL, T-SQL adds programming capabilities, allowing it to not only query data, but also handle complex logical operations.
| Function type | Standard SQL (ANSI) | T-SQL (Microsoft) |
|---|---|---|
| String concatenation | Use double vertical bars || | Use the plus sign + |
| Limit the number of columns returned | Use FETCH FIRST | Use the TOP keyword |
| Data type conversion | CAST | CAST and CONVERT (supports formatting) |
| program logic | Mainly basic inquiries | Complete procedural programming language capabilities |
The following code shows how to combine variables, logical judgments and data queries in T-SQL:
-- Declare and set variables
DECLARE @Threshold INT = 100;
DECLARE @CurrentStock INT;
-- Get the inventory of a specific product
SELECT @CurrentStock = StockQuantity
FROM Products
WHERE ProductID = 5;
-- logical judgment
IF @CurrentStock < @Threshold
BEGIN
PRINT 'Warning: Inventory is below preset threshold. ';
-- Execute replenishment logic...
END
ELSE
BEGIN
SELECT * FROM Products WHERE ProductID = 5;
END
T-SQL is widely used in the following fields:
T-SQL extends standard SQL to provide full programmatic capabilities. Through variables, logic control and error handling, developers can write complex business logic at the database level.
In T-SQL, all custom variables must start with the @ symbol. Use DECLARE for declaration and SET or SELECT for assignment.
-- Declare variables
DECLARE @EmployeeCount INT;
DECLARE @DepartmentName NVARCHAR(50);
-- assignment
SET @DepartmentName = 'IT Department';
--Assign values from query results
SELECT @EmployeeCount = COUNT(*)
FROM Employees
WHERE Department = @DepartmentName;
-- Output results
PRINT @EmployeeCount;
IF...ELSE statements allow different blocks of code to be executed based on conditions. If a block contains multiple statements, they must be wrapped with BEGIN...END.
DECLARE @StockLevel INT;
SET @StockLevel = 10;
IF @StockLevel < 5
BEGIN
PRINT 'Inventory is seriously low, please restock immediately. ';
END
ELSE IF @StockLevel < 20
BEGIN
PRINT 'Inventory is low, it is recommended to restock. ';
END
ELSE
BEGIN
PRINT 'In stock. ';
END
The TRY...CATCH mechanism provided by T-SQL is similar to modern programming languages (such as C# or Java) and can capture exceptions during execution to prevent abnormal program interruption.
BEGIN TRY
-- Code to try to execute
INSERT INTO Sales (OrderID, ProductID, Quantity)
VALUES (1001, 'P01', -5); -- Assume this will trigger a constraint error
END TRY
BEGIN CATCH
-- Handling when errors occur
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_MESSAGE() AS ErrorMessage,
ERROR_SEVERITY() AS Severity;
--Rollback transaction
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH
nameField values are unique.nameThe length of the field is moderate to avoid being too long and affecting performance.Such as the profile type data table:
CREATE TABLE config_types (
name VARCHAR(50) PRIMARY KEY,
description TEXT
);
INSERT INTO config_types (name, description) VALUES ('general', 'General settings');
SELECT * FROM config_types WHERE name = 'general';
This table will store the common attributes of all "animals".
| Field name | data type | illustrate |
|---|---|---|
| id | INT | animal's unique identifier |
| species | VARCHAR(50) | types of animals |
| age | INT | animal age |
This table will inherit the id of the animal table and store the unique attributes of "cat".
| Field name | data type | illustrate |
|---|---|---|
| id | INT | Corresponds to the id of the animal table |
| breed | VARCHAR(50) | cat breed |
| favorite_food | VARCHAR(50) | Cat's favorite food |
CREATE TABLE animal (
id INT PRIMARY KEY AUTO_INCREMENT,
species VARCHAR(50) NOT NULL,
age INT NOT NULL
);
CREATE TABLE cat (
id INT PRIMARY KEY,
breed VARCHAR(50),
favorite_food VARCHAR(50),
FOREIGN KEY (id) REFERENCES animal(id)
);
INSERT INTO animal (species, age) VALUES ('Cat', 3);
INSERT INTO cat (id, breed, favorite_food) VALUES (1, 'Siamese', 'Fish');
| Animal ID | type | age |
|---|---|---|
| 1 | Cat | 3 |
| Animal ID | variety | love food |
|---|---|---|
| 1 | Siamese | Fish |
In this example,animalThe table stores the common attributes of all animals, andcatThe table stores the cat's unique attributes.catin the tableidis a referenceanimaltabularid, indicating that this is an inheritance relationship.
SELECT * FROM animal;
This query will return complete information about all cats, including common attributes inherited from the animal table.
SELECT animal.id, animal.species, animal.age, cat.breed, cat.favorite_food
FROM animal
JOIN cat ON animal.id = cat.id;
SELECT * FROM animal WHERE id = 1;
SELECT animal.id, animal.species, animal.age, cat.breed, cat.favorite_food
FROM animal
JOIN cat ON animal.id = cat.id
WHERE cat.breed = 'Siamese';
In these query examples we useJOINWillanimaltable andcatThe tables are combined to obtain complete information about the cat. This approach ensures that query results include both inherited and unique properties.
FOREIGN KEY (foreign key) is used to establish the association between two data tables to ensure the reference integrity of the data. For example, a field value in one data table must reference a primary key or unique value in another data table.
CREATE TABLE subtable (
Field Name Data Type,
FOREIGN KEY (foreign key field) REFERENCES parent data table (primary key field)
);
Establish a one-to-many relationship, such as orders and customers:
-- Create parent data table (customers)
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(50)
);
--Create sub-tables (orders) and set foreign keys
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
PRIMARY KEYorUNIQUE。ON DELETEBehavior.can pass throughON DELETEandON UPDATESpecify foreign key behavior:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CASCADE: Synchronously delete or update related data in the sub-data table.SET NULL: Set the foreign key field in the subdata table toNULL。RESTRICT: Prevent deletion or update, default behavior.NO ACTION:andRESTRICTSimilar, defer constraint checking.In MySQL, you can useCOMMENTto add notes to the fields.
CREATE TABLE users (
id INT PRIMARY KEY COMMENT 'User unique identification code',
name VARCHAR(50) COMMENT 'User name',
age INT COMMENT 'User's age'
);
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(50),
ageINT
) COMMENT = 'User information table';
The comments of the field can be queried through the following syntax:
SHOW FULL COLUMNS FROM users;
ALTER TABLE users MODIFY COLUMN name VARCHAR(50) COMMENT 'Modified remarks';
COMMENT, but SQLite does not support it.COMMENT ONinstruction.COMMENT ON COLUMN users.name IS 'user name';
In SQL queries, when the field name conflicts with the reserved word (Keyword) of the database system, a syntax error will occur. Dealing with this problem requires comprehensive consideration from both prevention and solution aspects.
This is the most recommended approach and aims to eliminate the problem at its source.
intervalChange totime_interval,WillrankChange touser_rank。When the use of reserved words cannot be avoided (for example: when working with an existing database structure), the field name must be wrapped in appropriate quotation marks to clearly inform the SQL engine that this is an identifier and not an SQL command.
| database system | quotation marks | example |
|---|---|---|
| MySQL / MariaDB | backtick (`) |
SELECT `interval`, `time` FROM table; |
| PostgreSQL / Oracle | double quotes (") |
SELECT "interval", "time" FROM table; |
| SQL Server | square brackets ([]) |
SELECT [interval], [time] FROM table; |
In programming languages such as Python, if you need to dynamically generate SQL statements, you should implement a function to automatically execute quotation marks to ensure that the generated statements are safe and correct.
\b) to match field names exactly and avoid incorrectly replacing string values or long names.If you use an ORM (such as SQLAlchemy or Django ORM), the framework automatically handles reserved words and quotation mark differences between different databases, abstracting the underlying SQL output, which greatly simplifies development work and improves the stability of the code.
Calculates the time difference in seconds between two DATETIME fields.
SELECT * FROM table_name
WHERE TIMESTAMPDIFF(SECOND, datetime_column1, datetime_column2) < 5;
Ensure that the time difference is an absolute value to avoid order effects.
SELECT * FROM table_name
WHERE ABS(TIMESTAMPDIFF(SECOND, datetime_column1, datetime_column2)) < 5;
If you just want to compare whether they are on the same day, you can use DATEDIFF.
SELECT * FROM table_name
WHERE DATEDIFF(datetime_column1, datetime_column2) = 0;
Applicable to databases that support timestamp operations, such as MySQL.
SELECT * FROM table_name
WHERE ABS(UNIX_TIMESTAMP(datetime_column1) - UNIX_TIMESTAMP(datetime_column2)) < 5;
The value you provided1763251200000Is a standard **millisecond timestamp** (Unix Epoch Time in milliseconds).
MySQL built-in functions (such asFROM_UNIXTIME) The default processing is **second level timestamp**. Therefore, you need to convert millisecond timestamps to seconds before use.
Divide your millisecond value by $1000$ to get the second-level timestamp required by the MySQL function:
1763251200000 / 1000 = 1763251200
useFROM_UNIXTIME()Function converts second-level timestamp to standard MySQLDATETIMEFormat:
SELECT FROM_UNIXTIME(1763251200000 / 1000);
If your version of MySQL supports milliseconds and you want to preserve millisecond precision in the results, you can use the second parameter:
SELECT FROM_UNIXTIME(1763251200000 / 1000, '%Y-%m-%d %H:%i:%s.%f');
in:
%Y-%m-%d %H:%i:%sIs the standard date and time format.%fCaptures Microseconds, so you need to pass in the remaining milliseconds as a fraction.If your intention is to store the millisecond value in an integer (e.g.INTorBIGINT) field as the second-level timestamp, then only a simple division operation is required:
SELECT 1763251200000 / 1000;
Or, if your data is stored in table fields (for example, the field is namedtimestamp_ms):
SELECT timestamp_ms / 1000 AS unix_timestamp_s FROM your_table;
When you insert this value into something that hasDATETIMEorTIMESTAMPtype field, you need to enterVALUESPartially perform the conversion:
INSERT INTO your_table (datetime_column)
VALUES (FROM_UNIXTIME(1763251200000 / 1000));
FROM_UNIXTIME()The function converts the UTC timestamp to the time zone configured for your MySQL server. If your timestamp is based on UTC and you want the result to be UTC as well, you need to make sure your server or connection time zone is set correctly, or useCONVERT_TZFunction performs explicit time zone handling.
In MySQL, useFORMAT()to format floating point numbers.
SELECT FORMAT(123.4567, 2); -- Result: '123.46'
ROUND()Used for rounding to a fixed number of decimal places.
SELECT ROUND(123.4567, 2); -- Result: 123.46
Convert FLOAT to DECIMAL to maintain a fixed number of decimal places.
SELECT CAST(123.4567 AS DECIMAL(10,2)); -- Result: 123.46
SELECT CONVERT(123.4567, DECIMAL(10,2)); -- Result: 123.46
SELECT id, FORMAT(price, 2) AS formatted_price FROM products;
You can directly set the number of decimal points when creating a data table.
CREATE TABLE products (
id INT PRIMARY KEY,
price DECIMAL(10,2) -- two decimal places
);
Get the maximum value of the field.
SELECT MAX(price) AS max_price FROM products;
Get the minimum value of the field.
SELECT MIN(price) AS min_price FROM products;
Calculate the average of the fields.
SELECT AVG(price) AS avg_price FROM products;
SELECT
MAX(price) AS max_price,
MIN(price) AS min_price,
AVG(price) AS avg_price
FROM products;
SELECT category,
MAX(price) AS max_price,
MIN(price) AS min_price,
AVG(price) AS avg_price
FROM products
GROUP BY category;
Find the maximum value of another query result.
SELECT MAX(price) FROM (SELECT price FROM products WHERE category = 'electronics') AS subquery;
Get the maximum value after sorting.
SELECT price FROM products WHERE category = 'electronics' ORDER BY price DESC LIMIT 1;
WITH filtered_products AS (
SELECT price FROM products WHERE category = 'electronics'
)
SELECT MAX(price) FROM filtered_products;
Geometric Mean calculation formula:
GM = (x1 * x2 * ... * xn)^(1/n)
In SQL, the geometric mean can be calculated using logarithmic operations.
SELECT EXP(AVG(LOG(price))) AS geometric_mean FROM products WHERE price > 0;
usePOWER()Compute the nth root:
SELECT POWER(EXP(SUM(LOG(price))), 1 / COUNT(price)) AS geometric_mean
FROM products WHERE price > 0;
LOG()will cause errors.SELECT EXP(AVG(LOG(price))) AS geometric_mean FROM products WHERE price > 0;
In MySQL/MariaDB, you can useSTDDEV()to calculate the standard deviation.
SELECT STDDEV(salary) AS salary_stddev FROM employees;
SQL provides two methods of calculating standard deviation:
STDDEV_POP()- Calculate the population standard deviation (Population Standard Deviation)STDDEV_SAMP()- Calculate sample standard deviation (Sample Standard Deviation)SELECT
STDDEV_POP(salary) AS population_stddev,
STDDEV_SAMP(salary) AS sample_stddev
FROM employees;
If the SQL version does not supportSTDDEV(), you can use the following formula:
SELECT SQRT(
SUM(POW(salary - (SELECT AVG(salary) FROM employees), 2)) / COUNT(salary)
) AS salary_stddev
FROM employees;
STDDEV()The standard deviation can be calculated directly.STDDEV_POP()Calculate the parent standard deviation,STDDEV_SAMP()Calculate the sample standard deviation.SQLWHEREclause is used to extract from the tableExtract records that meet specified conditions(column/row). It is one of the most basic and important parts of the Data Manipulation Language (DML), used to accurately filter the required data.
WHEREThe clause is evaluated for each row (Row) as the database reads the table. Only when the data in a certain row satisfiesWHEREThe row will be included in the result set only if the following conditions are met.SELECT, UPDATE, DELETEUse it with SQL commands.WHEREclause usually followsFROMAfter clause:
SELECT column_name(s)
FROM table_name
WHERE condition;
or when used to modify or delete information:
UPDATE table_name
SET column1 = value1
WHERE condition;
WHEREOperators are used in clauses to establish conditions. The most common ones are:
| Operator type | operator | describe |
|---|---|---|
| comparison operator | = |
equal |
| comparison operator | >, <, >=, <= |
Greater than, less than, greater than or equal to, less than or equal to |
| comparison operator | <>or!= |
not equal to |
| Logical operators | AND |
Meet multiple conditions at the same time |
| Logical operators | OR |
satisfy any of the conditions |
| Logical operators | NOT |
Condition is not met |
| special operators | BETWEEN |
Within a certain range (including boundaries) |
| special operators | LIKE |
Fuzzy matching string pattern (matching%or_) |
| special operators | IN |
The value is any item in the list |
| special operators | IS NULL / IS NOT NULL |
Whether the field value is NULL |
Suppose there is a file namedEmployeestable, includingEmployeeID, LastName(surname),Salary(salary) andDepartment(department).
Find all employees with department 'Sales':
SELECT EmployeeID, LastName
FROM Employees
WHERE Department = 'Sales';
Find employees whose salary is greater than or equal to 50,000 and whose department is not 'HR':
SELECT *
FROM Employees
WHERE Salary >= 50000 AND Department != 'HR';
Find employees with salaries between 60,000 and 80,000 (inclusive):
SELECT *
FROM Employees
WHERE Salary BETWEEN 60000 AND 80000;
Find employees in the 'Marketing' or 'Finance' department:
SELECT *
FROM Employees
WHERE Department IN ('Marketing', 'Finance');
This is equivalent to usingWHERE Department = 'Marketing' OR Department = 'Finance'。
SQLGROUP BYclause is used toSELECTIn a statement, rows with the same value (or combination of values for multiple fields) are grouped into summary columns. It is often used with SQLsummary functionUsed together to calculate a summary value for each group.
GROUP BYThe core function is to merge multiple single rows with the same value in the table into a single group (Group).COUNT()、SUM()、AVG()、MAX()orMIN()Combined with other summary functions, you can calculate a statistical summary for each group rather than a summary of the entire table.GROUP BYclause must be inWHEREclause, but afterHAVINGandORDER BYbefore clause.
SELECT column_name(s), aggregate_function(column_to_summarize)
FROM table_name
WHERE condition_on_rows -- (optional) filter single rows before grouping
GROUP BY column_name(s) -- specifies the column used for grouping
HAVING condition_on_groups -- (optional) filter groups after grouping
ORDER BY column_name(s);
Important rules:anything that appears inSELECTin the list, butNoColumns surrounded by summary functions aremustincluded inGROUP BYin clause.
Suppose there is a file namedProductstable, includingCategory(product category) andPrice(price).
SELECT Category, AVG(Price) AS AveragePrice, COUNT(ProductID) AS TotalProducts
FROM Products
GROUP BY Category;
GROUP BY Category: directive database will all have the sameCategoryRow grouping of values.AVG(Price): Calculate the average of all product prices in each grouping (each category).COUNT(ProductID): Calculate the total quantity of products in each grouping (each category).You can group based on multiple fields. Rows will be grouped together only if all specified fields have the same value.
SELECT Category, Supplier, SUM(SalesAmount) AS TotalSales
FROM SalesRecords
GROUP BY Category, Supplier
ORDER BY Category, Supplier;
This will produce a result where each row represents the total sales for a unique Category + Supplier combination.
| function | Function |
|---|---|
COUNT() |
Count the number of rows in a group. |
SUM() |
Calculates the sum of numeric fields in a group. |
AVG() |
Calculates the average of numeric fields in a group. |
MAX() |
Find the maximum value of a field in a group. |
MIN() |
Find the minimum value of a field in a group. |
In SQL,HAVINGclause is used to filter group (Group) results, it is usually used withGROUP BYclauses are used together.
AlthoughWHEREclause is used to filter the rows of a single column (Rows), but is useful when group-based summary values are required (e.g.COUNT(), SUM(), AVG()When filtering groups by waiting for the result of the summary function), you must useHAVINGclause.
| feature | WHERE clause | HAVING clause |
|---|---|---|
| execution timing | Filter the original rows before the data is grouped (GROUP BY). | After the data is grouped (GROUP BY), filter the summarized groups. |
| Available conditions | Summary functions (such as COUNT, SUM, AVG) cannot be used directly. | Aggregation functions must be used to set filter conditions. |
| Application objects | A single column value. | Group results. |
SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE condition_on_rows -- (optional) filter single rows
GROUP BY column_name(s)
HAVING condition_on_groups -- (required) filter groups
ORDER BY column_name(s);
Suppose there is a file namedOrderstable, includingCustomerID(Customer ID) andTotalAmount(total amount of order). we want to find out allAverage order amount exceeds 500of customers.
SELECT CustomerID, AVG(TotalAmount) AS AverageOrder
FROM Orders
GROUP BY CustomerID
HAVING AVG(TotalAmount) > 500;
GROUP BY CustomerID: Group all orders by customer ID.AVG(TotalAmount): Calculate the average order amount per customer.HAVING AVG(TotalAmount) > 500: This is the filter condition. It tells SQL to display only those average order amounts (i.e.AVG(TotalAmount)results) for groups (customers) greater than 500**.Suppose we want to find all customers whose total order count is **3** and whose average order amount is less than 1000**.
SELECT CustomerID, COUNT(OrderID) AS TotalOrders, AVG(TotalAmount) AS AverageOrder
FROM Orders
GROUP BY CustomerID
HAVING COUNT(OrderID) >= 3 AND AVG(TotalAmount) < 1000;
HAVING COUNT(OrderID) >= 3: Filter out customer groups with less than 3 orders in total.AND AVG(TotalAmount) < 1000: Also filter out customer groups whose average order amount is greater than or equal to 1,000.existSELECTIn query, if definedexpr1 AS field1, can it be inexpr2used infield1?
The execution order of SQL determines that aliases cannot be in the sameSELECTInternally quoted again:
SELECT price * 1.1 AS new_price, new_price + 10 AS final_price FROM products; -- Error
Unknown column 'new_price' in 'field list'
can be calculated first in the subquerynew_price, and then referenced in the outer query:
SELECT new_price, new_price + 10 AS final_price
FROM (SELECT price * 1.1 AS new_price FROM products) AS subquery;
Available if SQL supports Common Table Expressions (CTE)WITHTo simplify:
WITH cte AS (
SELECT price * 1.1 AS new_price FROM products
)
SELECT new_price, new_price + 10 AS final_price FROM cte;
If it is just a simple operation, you can repeat the calculation directly (but it is not recommended because the readability is poor):
SELECT price * 1.1 AS new_price, price * 1.1 + 10 AS final_price FROM products;
SELECTReference the alias within the query.WITH) to solve.JOIN is used to merge related data from multiple data tables and establish a relationship based on a certain field (usually a foreign key).
Only the data that meets the conditions in the two data tables are returned.
SELECT orders.order_id, customers.name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;
Returns all the data in the left table. If there is no corresponding data in the right table, it will be displayed.NULL。
SELECT customers.name, orders.order_id
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;
Returns all the data in the right table. If there is no corresponding data in the left table, it will be displayed.NULL。
SELECT customers.name, orders.order_id
FROM orders
RIGHT JOIN customers ON orders.customer_id = customers.customer_id;
Returns all the data in the left and right tables, and displays it if there is no match.NULL。
MySQL does not support FULL JOIN, availableLEFT JOINandRIGHT JOINCombination simulation.
SELECT customers.name, orders.order_id
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id
UNION
SELECT customers.name, orders.order_id
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
Returns all possible combinations (Cartesian product) of two tables.
SELECT customers.name, products.product_name
FROM customers
CROSS JOIN products;
Used for associations within the same table, such as employee superior relationships.
SELECT A.name AS employee, B.name AS manager
FROM employees A
JOIN employees B ON A.manager_id = B.employee_id;
-- abc.sql SOURCE other_file.sql; SOURCE another_file.sql;
-- abc.sql \i other_file.sql \i another_file.sql
#!/bin/bash mysql -u user -p database_name < abc.sql mysql -u user -p database_name < other_file.sql
cat abc.sql other_file.sql another_file.sql > combined.sql mysql -u user -p database_name < combined.sql
SET @param1 = 'value1'; SOURCE other_file.sql;2. Reference variables in `other_file.sql`:
SELECT * FROM table WHERE column = @param1;
\set param1 'value1' \i other_file.sql2. Use variables in `other_file.sql`:
SELECT * FROM table WHERE column = :'param1';
sed "s/{param1}/value1/g" abc.sql | mysql -u user -p database_name
2. Use placeholder `{param1}` in the SQL file, replaced by the command line tool.psql -d database_name -v param1=value1 -f abc.sql2. Use `:'param1'` to represent variables in the SQL file.
Stored Procedure (stored routine or stored procedure) is a set of SQL statements that are pre-compiled and stored in the database and can be executed through calls to improve efficiency and reduce code duplication.
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;
CALL GetAllProducts();
Search for products in a specific category:
DELIMITER //
CREATE PROCEDURE GetProductsByCategory(IN category_name VARCHAR(50))
BEGIN
SELECT * FROM products WHERE category = category_name;
END //
DELIMITER ;
call:
CALL GetProductsByCategory('electronics');
Calculate the total number of items in a category:
DELIMITER //
CREATE PROCEDURE GetProductCountByCategory(IN category_name VARCHAR(50), OUT total_count INT)
BEGIN
SELECT COUNT(*) INTO total_count FROM products WHERE category = category_name;
END //
DELIMITER ;
call:
CALL GetProductCountByCategory('electronics', @count);
SELECT @count;
DELIMITER //
CREATE PROCEDURE CalculateTotalRevenue()
BEGIN
DECLARE total DECIMAL(10,2);
SELECT SUM(price) INTO total FROM sales;
SELECT total AS total_revenue;
END //
DELIMITER ;
call:
CALL CalculateTotalRevenue();
DELIMITER //
CREATE PROCEDURE CheckStock(IN product_id INT, OUT stock_status VARCHAR(20))
BEGIN
DECLARE stock INT;
SELECT quantity INTO stock FROM inventory WHERE id = product_id;
IF stock > 10 THEN
SET stock_status = 'In Stock';
ELSEIF stock > 0 THEN
SET stock_status = 'Low Stock';
ELSE
SET stock_status = 'Out of Stock';
END IF;
END //
DELIMITER ;
call:
CALL CheckStock(1, @status);
SELECT @status;
DELIMITER //
CREATE PROCEDURE CountDown(IN start_num INT)
BEGIN
DECLARE i INT;
SET i = start_num;
loop_label: LOOP
IF i <= 0 THEN
LEAVE loop_label;
END IF;
SELECT i;
SET i = i - 1;
END LOOP;
END //
DELIMITER ;
call:
CALL CountDown(5);
DROP PROCEDURE IF EXISTS GetAllProducts;
DELIMITERAvoid SQL syntax conflicts.In MySQL and MariaDB, stored procedure parameters cannot be directly set to default values (unlike SQL Server or PostgreSQL). However, it is possible to useIFConditional statements to simulate preset values.
Suppose we want to queryuserstable, when parametersuser_idWhen not provided, the default query ID is 1:
DELIMITER //
CREATE PROCEDURE GetUserById(IN user_id INT)
BEGIN
IF user_id IS NULL THEN
SET user_id = 1; --Default value
END IF;
SELECT * FROM users WHERE id = user_id;
END //
DELIMITER;
CALL GetUserById(NULL); -- will query id = 1
CALL GetUserById(5); -- Query id = 5
COALESCE()Will return the specified default value when the parameter is NULL:
DELIMITER //
CREATE PROCEDURE GetUserById(IN user_id INT)
BEGIN
SELECT * FROM users WHERE id = COALESCE(user_id, 1);
END //
DELIMITER ;
CALL GetUserById(NULL); -- Default is 1
CALL GetUserById(10); -- Query id = 10
If you want the parameters to be optional, you can create multiple Stored Procedures. For example:
DELIMITER //
CREATE PROCEDURE GetAllUsers()
BEGIN
SELECT * FROM users;
END //
CREATE PROCEDURE GetUserById(IN user_id INT)
BEGIN
SELECT * FROM users WHERE id = user_id;
END //
DELIMITER ;
CALL GetAllUsers(); -- no parameters, query all
CALL GetUserById(3); -- Query id = 3
DEFAULTvalue.IForCOALESCE()Analog defaults.Stored Procedure of MySQL/MariaDB is not supportedRETURNReturn query results, but can be usedOUTParameters pass back values.
DELIMITER //
CREATE PROCEDURE GetUserCount(OUT user_count INT)
BEGIN
SELECT COUNT(*) INTO user_count FROM users;
END //
DELIMITER ;
CALL GetUserCount(@total);
SELECT @total; -- Display the number of users
If you want to return a query result, directlySELECTThat’s it:
DELIMITER //
CREATE PROCEDURE GetUserById(IN user_id INT)
BEGIN
SELECT * FROM users WHERE id = user_id;
END //
DELIMITER ;
CALL GetUserById(5); -- Query the user with ID 5
Although MySQL supportsRETURN, but can only return a single value, usually used to control the process:
DELIMITER //
CREATE PROCEDURE GetMaxSalary()
BEGIN
DECLARE max_salary DECIMAL(10,2);
SELECT MAX(salary) INTO max_salary FROM employees;
RETURN max_salary; -- But this will not return the value directly in MySQL
END //
DELIMITER;
CALLto get the RETURN value, so it is recommended to use the OUT parameter:DELIMITER //
CREATE PROCEDURE GetMaxSalary(OUT max_salary DECIMAL(10,2))
BEGIN
SELECT MAX(salary) INTO max_salary FROM employees;
END //
DELIMITER ;
CALL GetMaxSalary(@max);
SELECT @max; -- Display the maximum salary
Use multipleOUTThe parameters return different calculation results:
DELIMITER //
CREATE PROCEDURE GetUserStats(OUT total_users INT, OUT avg_age DECIMAL(5,2))
BEGIN
SELECT COUNT(*) INTO total_users FROM users;
SELECT AVG(age) INTO avg_age FROM users;
END //
DELIMITER ;
CALL GetUserStats(@total, @avg);
SELECT @total, @avg; -- Display the total number and average age of users
OUTparameter.SELECT。OUTparameter.RETURNReturning query results is only applicable to process control.MySQL Stored Procedure can be accessed throughOUTParameters return values, which can then be used outside of CALLSELECTGet this value.
DELIMITER //
CREATE PROCEDURE GetTotalUsers(OUT total_users INT)
BEGIN
SELECT COUNT(*) INTO total_users FROM users;
END //
DELIMITER ;
CALL GetTotalUsers(@total);
SELECT @total AS UserCount; -- Use return value outside of CALL
If Stored Procedure usesSELECTReturning results cannot be directly stored in variables, but they can be usedINSERT INTO ... SELECT。
DELIMITER //
CREATE PROCEDURE GetMaxSalary()
BEGIN
SELECT MAX(salary) AS max_salary FROM employees;
END //
DELIMITER ;
CREATE TEMPORARY TABLE temp_result (max_salary DECIMAL(10,2));
INSERT INTO temp_result EXECUTE GetMaxSalary();
SELECT max_salary FROM temp_result; -- used outside CALL
If the results generated by Stored Procedure need to be accessed in variables, you can usePREPAREandEXECUTE。
DELIMITER //
CREATE PROCEDURE GetUserById(IN user_id INT)
BEGIN
SELECT name FROM users WHERE id = user_id;
END //
DELIMITER ;
SET @sql = 'CALL GetUserById(5)';
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
OUTParameter returns a single value in leaflet, passed@variableaccess.INSERT INTO ... CALLStore in the staging table.PREPAREandEXECUTE。In MySQL / MariaDB, Stored Function (function) cannot be returnedSELECTResult set, otherwise an error will occur:
ERROR 1415 (0A000): Not allowed to return a result set from a function
Functions cannot return result sets, but Stored Procedures can.
DELIMITER //
CREATE FUNCTION GetUsers()
RETURNS TABLE
BEGIN
RETURN (SELECT * FROM users); -- This is not allowed
END //
DELIMITER;
DELIMITER //
CREATE PROCEDURE GetUsers()
BEGIN
SELECT * FROM users;
END //
DELIMITER ;
CALL GetUsers();
If you only need to return a single value (such as a count or maximum value), you can useRETURN。
DELIMITER //
CREATE FUNCTION GetUserCount()
RETURNS INT DETERMINISTIC
BEGIN
DECLARE total INT;
SELECT COUNT(*) INTO total FROM users;
RETURN total;
END //
DELIMITER ;
SELECT GetUserCount();
If you really need to return multiple rows of results within the function, you can have the function insert data into the Temporary Table and then query it externally.
DELIMITER //
CREATE FUNCTION PopulateTempUsers()
RETURNS INT DETERMINISTIC
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS temp_users AS (SELECT * FROM users);
RETURN 1;
END //
DELIMITER ;
SELECT PopulateTempUsers();
SELECT * FROM temp_users;
RETURN。HeidiSQL is a free open source SQL client that supports MySQL, MariaDB, PostgreSQL and MS SQL Server. It provides a GUI to manage databases, execute SQL queries, import/export data, etc.
1. Go to the official website to download HeidiSQL:https://www.heidisql.com/download.php2. Execute the installer and follow the instructions to complete the installation
3. Open HeidiSQL and set up a new connection
1. Start HeidiSQL
2. Click "Add" to create a new connection
3. Settings:
- Hostname/IP: 127.0.0.1 or remote server IP
- Username: root or other user
- Password: the corresponding password
- Port: 3306 (MySQL/MariaDB)
4. Click "Open" to connect to the database
Enter the SQL statement in the HeidiSQL query window:
SELECT * FROM users WHERE status = 'active';
Click the "Execute" button to view the results.
1. Right-click the database → select "Export SQL"
2. Select the data table to be exported
3. Set the export format (.sql, .csv, .json)
4. Click "Export"
1. Open HeidiSQL and select the target database
2. Click "Tools" → "Execute SQL File"
3. Select the .sql file and execute
1. Enter "Tools" → "Manage User Permissions"
2. Select the users to manage
3. Set database permissions (SELECT, INSERT, UPDATE, DELETE, etc.)
4. Click "Save"
1. Start HeidiSQL.
2. Connect to the MySQL or MariaDB server.
3. Select the target database in the Database list on the left.
1. Right-click the database name on the left and select "Create New" → "Save Program".
2. HeidiSQL will open a new SQL editing window and provide a default stored procedure template.
The following is a simple example that returns all data in the users table:
DELIMITER //
CREATE PROCEDURE GetAllUsers()
BEGIN
SELECT * FROM users;
END //
DELIMITER ;
1. Click the "Go" button (green lightning bolt).
2. If the execution is successful, the program can be found in the "Save Program" column on the left.
CALL GetAllUsers();
Pass in parameters to filter data, such as querying based on user ID:
DELIMITER //
CREATE PROCEDURE GetUserById(IN user_id INT)
BEGIN
SELECT * FROM users WHERE id = user_id;
END //
DELIMITER ;
Call a stored procedure with parameters:
CALL GetUserById(1);
DROP PROCEDURE IF EXISTS GetAllUsers;
DELIMITERMake sure the syntax is correct.CALLTest stored procedures.In today's software development environment, database applications have evolved from simple data storage to comprehensive solutions integrating AI, edge computing and automated operation and maintenance. The following are the current mainstream development models:
With the popularity of large language models (LLM), retrieval-augmented generation (RAG) has become a development standard. This type of solution focuses on vectorization and retrieval of unstructured data.
In order to simplify operation and maintenance and reduce start-up costs, developers tend to choose database services with automatic scaling capabilities, which is especially popular in web applications.
In the TypeScript ecosystem, synchronizing database definitions with front-end types is key to ensuring system stability.
In order to cope with global access and reduce latency, it has become a trend to deploy data close to users.
| Plan Category | Represent technology | Core advantages |
|---|---|---|
| AI first | pgvector, Milvus | Support semantic search and knowledge base construction |
| Serverless type | Supabase, Vercel Postgres | Zero-burden operation and maintenance, billing based on volume |
| Efficient development type | Drizzle ORM, Prisma | Extremely high type safety and development speed |
| Decentralized architecture | CockroachDB, TiDB | Cross-region deployment, automatic fault tolerance |
In modern database application development, the front-end is no longer just about presenting data, but also emphasizes type synchronization with the back-end database, state management, and server-side rendering performance. The following are the current mainstream choices:
This is currently the solution with the highest market share and richest community support, and is particularly suitable for complex database management systems.
Known for its simple learning curve and highly integrated tool chain, it is deeply loved by the backend management systems of medium and large enterprises.
In order to improve development efficiency and reduce data transmission errors, modern front-ends highly rely on the following tools:
For database management interfaces used within enterprises, developers often choose faster integration solutions.
| Scheme name | Applicable scenarios | Core advantages |
|---|---|---|
| Next.js + Tailwind | SaaS products, modern web applications | SEO friendly, ultimate performance optimization |
| Vue + Element Plus | Enterprise internal backend and management system | Rich components and extremely fast development |
| TanStack Query | Applications with high-frequency data updates | Powerful cache management and automatic synchronization |
| Retool | Emergency internal maintenance tools | Drag and drop, almost no need to write CSS |
Modern database web management tools allow teams to access data directly through a browser without the need to install desktop software. The following are divided into four categories according to functional positioning:
This type of tool supports multiple database connections (such as MySQL, PostgreSQL, SQL Server) and is suitable for developers who manage diverse environments.
A UI deeply optimized for a specific database by the original manufacturer or the community.
Convert the database into an intuitive interface similar to Excel, suitable for non-technical personnel or to quickly build an internal backend.
If the data is hosted on a cloud service provider, the native web console usually has the highest level of integration.
CloudBeaver is an open source web-based database management solution developed by the DBeaver team. It uses Java back-end and React front-end architecture to allow users to securely manage various databases through the browser, which is very suitable for scenarios that require remote access or team collaboration.
CloudBeaver provides open source Community Edition (Community) and commercial Enterprise Edition (Enterprise). The main difference lies in advanced feature support:
| Features | Community | Enterprise (Enterprise) |
|---|---|---|
| SQL database support | Supports most mainstream SQL | Contains NoSQL (MongoDB, Redis) |
| Cloud service integration | basic wiring | Native support for AWS, GCP, Azure resource browsing |
| Authentication | Account password | Supports SSO, SAML, LDAP, Kerberos |
| Advanced tools | Basic query | AI assistant (SQL generation), visual query builder |
The most recommended installation method for CloudBeaver is to use Docker, as it already packages all necessary Java environments and drivers. There are three main deployment paths:
This is the simplest method, just execute one line of instructions to start the service. By default, it will listen on port 8978.
docker run --name cloudbeaver -d -p 8978:8978 dbeaver/cloudbeaver:latest
After execution, open browser inputhttp://localhost:8978You can enter the setting wizard.
Data persistence can be easily managed through Compose files to ensure that settings will not be lost after the container is restarted.
version: '3'
services:
cloudbeaver:
image: dbeaver/cloudbeaver:latest
container_name: cloudbeaver
restart: unless-stopped
ports:
- "8978:8978"
volumes:
- ./cloudbeaver-data:/opt/cloudbeaver/workspace
Save the above content asdocker-compose.yml, and then executedocker-compose up -d。
If Docker cannot be used in the environment, you can download the compiled binary file and install it manually:
cloudbeaver-ce-latest-linux-x86_64.tar.gz)。tar -xvf cloudbeaver-ce-latest-linux-x86_64.tar.gz。./run-server.sh(Linux/macOS) orrun-server.bat (Windows)。When entering the web interface for the first time, the system will guide you through the following configuration:
email: [email protected]