MySQL 语法教程
目录
简介
MySQL 是一种关系型数据库管理系统,用于存储、管理和检索数据。它使用结构化查询语言(SQL)来执行数据库操作。以下是 MySQL 中常用的语法和操作。
数据库操作
创建数据库
CREATE DATABASE database_name;
删除数据库
DROP DATABASE database_name;
表操作
创建表
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);
示例:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT,
department VARCHAR(50)
);
删除表
DROP TABLE table_name;
查看表结构
DESCRIBE table_name;
数据操作
插入数据
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
示例:
INSERT INTO employees (name, age, department)
VALUES ('Alice', 30, 'HR');
查询数据
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column
LIMIT number;
示例:
SELECT name, age
FROM employees
WHERE age > 25
ORDER BY age DESC;
更新数据
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
示例:
UPDATE employees
SET age = 31
WHERE name = 'Alice';
删除数据
DELETE FROM table_name
WHERE condition;
示例:
DELETE FROM employees
WHERE name = 'Alice';
约束与索引
主键约束
CREATE TABLE table_name (
column1 datatype PRIMARY KEY,
...
);
示例:
CREATE TABLE departments (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
外键约束
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
FOREIGN KEY (column1) REFERENCES other_table(column)
);
示例:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT,
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(id)
);
唯一约束
CREATE TABLE table_name (
column1 datatype UNIQUE,
...
);
示例:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE,
password VARCHAR(100)
);
创建索引
CREATE INDEX index_name
ON table_name (column);
示例:
CREATE INDEX idx_department
ON employees (department);
删除索引
DROP INDEX index_name
ON table_name;
视图
创建视图
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
示例:
CREATE VIEW employee_view AS
SELECT name, age, department
FROM employees
WHERE age > 25;
删除视图
DROP VIEW view_name;
存储过程与函数
创建存储过程
DELIMITER //
CREATE PROCEDURE procedure_name (IN param1 datatype, OUT param2 datatype)
BEGIN
-- 过程逻辑
END //
DELIMITER ;
示例:
DELIMITER //
CREATE PROCEDURE GetEmployeeCount (OUT count INT)
BEGIN
SELECT COUNT(*) INTO count FROM employees;
END //
DELIMITER ;
创建函数
DELIMITER //
CREATE FUNCTION function_name (param datatype)
RETURNS datatype
BEGIN
-- 函数逻辑
RETURN value;
END //
DELIMITER ;
示例:
DELIMITER //
CREATE FUNCTION GetEmployeeAge (emp_name VARCHAR(100))
RETURNS INT
BEGIN
DECLARE emp_age INT;
SELECT age INTO emp_age FROM employees WHERE name = emp_name;
RETURN emp_age;
END //
DELIMITER ;
删除存储过程与函数
DROP PROCEDURE procedure_name;
DROP FUNCTION function_name;
触发器
创建触发器
CREATE TRIGGER trigger_name
BEFORE/AFTER INSERT/UPDATE/DELETE
ON table_name
FOR EACH ROW
BEGIN
-- 触发器逻辑
END;
示例:
CREATE TRIGGER before_employee_insert
BEFORE INSERT
ON employees
FOR EACH ROW
BEGIN
SET NEW.age = IFNULL(NEW.age, 30);
END;
删除触发器
DROP TRIGGER trigger_name;
事务处理
启动事务
START TRANSACTION;
提交事务
COMMIT;
回滚事务
ROLLBACK;
以上就是详细的 MySQL 语法教程,涵盖了从数据库、表操作到高级操作和事务处理的各个方面。希望对您有所帮助。