CREATE DATABASE my_database CHARACTER SET utf8 COLLATE utf8_general_ci;
SHOW DATABASES;
USE my_database;
DROP DATABASE IF EXISTS my_database;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
DESCRIBE users;
ALTER TABLE users ADD COLUMN last_login DATETIME;
RENAME TABLE users TO members;
DROP TABLE users;
INSERT INTO users (username, email) VALUES ('JohnDoe', 'john@example.com');
SELECT * FROM users;
或者更复杂的查询:
SELECT username, COUNT(*) as total_entries FROM users GROUP BY username;
UPDATE users SET email = 'new@example.com' WHERE username = 'JohnDoe';
DELETE FROM users WHERE id = 1;
SELECT * FROM users WHERE age > 18 AND gender = 'M';
SELECT * FROM users ORDER BY username DESC;
SELECT COUNT(*) FROM users;
SELECT AVG(salary) FROM employees;
SELECT department, COUNT(*) FROM employees GROUP BY department;
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE ON database_name.* TO 'newuser'@'%';
REVOKE ALL PRIVILEGES ON database_name.* FROM 'newuser'@'%';
SET PASSWORD FOR 'newuser'@'%' = PASSWORD('new_password');
START TRANSACTION;
-- 执行一系列操作
COMMIT;
# 备份整个数据库
mysqldump -u 用户名 -p 数据库名 > backup.sql
# 恢复数据库
mysql -u 用户名 -p 数据库名 < backup.sql
CREATE VIEW user_emails AS
SELECT username, email FROM users;
SELECT * FROM user_emails;
CREATE OR REPLACE VIEW user_emails AS
SELECT username, email, created_at FROM users;
DROP VIEW IF EXISTS user_emails;
CREATE INDEX idx_username ON users(username);
SHOW INDEX FROM users;
DROP INDEX idx_username ON users;
SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE country = 'USA');
SELECT customers.name, orders.order_date
FROM customers
INNER JOIN orders ON customers.id = orders.customer_id;
INSERT INTO users (username, email) VALUES
('User1', 'user1@example.com'),
('User2', 'user2@example.com'),
('User3', 'user3@example.com');
DELETE FROM users WHERE age < 18;
TRUNCATE TABLE users;
SHOW TABLES;
mysqldump -u 用户名 -p 数据库名 表名 > table_backup.sql
mysql -u 用户名 -p 数据库名 < table_backup.sql
当然,接下来再介绍几个MySQL中的其他重要命令:
UPDATE users
SET email = 'newemail@example.com', age = 30
WHERE username = 'JohnDoe';
SELECT * FROM users
LIMIT 10 OFFSET 20; -- 获取第21-30条记录
CREATE TABLE new_table LIKE old_table;
SHOW TABLES LIKE 'users';
或者使用SQL表达式:
SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'users' AND table_schema = DATABASE();
DELETE t1 FROM users t1, users t2
WHERE t1.id < t2.id AND t1.username = t2.username;
或在较新版本的MySQL中使用DISTINCT
关键字结合GROUP BY
和HAVING
删除重复项:
CREATE TABLE deduplicated_users AS
SELECT MIN(id) as id, username, email
FROM users
GROUP BY username, email
HAVING COUNT(*) = 1;
-- 确认无误后,替换原表
DROP TABLE users;
RENAME TABLE deduplicated_users TO users;
ALTER DATABASE database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
SHOW STATUS;
SHOW SESSION VARIABLES;
当然,接下来再介绍几个MySQL中其他高级功能的命令示例:
CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
SET NEW.email = LOWER(NEW.email);
END;
此触发器在插入新用户前自动将email字段转换为小写。
DROP TRIGGER IF EXISTS before_insert_users;
DELIMITER //
CREATE PROCEDURE get_user_info(IN user_id INT)
BEGIN
SELECT * FROM users WHERE id = user_id;
END //
DELIMITER ;
调用存储过程:
CALL get_user_info(1);
SHOW EVENTS;
CREATE EVENT update_last_login
ON SCHEDULE EVERY 1 DAY STARTS '2022-01-01 01:00:00'
DO
UPDATE users SET last_login = CURRENT_TIMESTAMP;
-- 若要立即启用该事件(默认创建时禁用)
ALTER EVENT update_last_login ENABLE;
DROP EVENT IF EXISTS update_last_login;
CREATE TABLE sales (
id INT NOT NULL,
sale_date DATE,
amount DECIMAL(10,2),
PRIMARY KEY (id)
)
PARTITION BY RANGE (YEAR(sale_date)) (
PARTITION p0 VALUES LESS THAN (2010),
PARTITION p1 VALUES LESS THAN (2020),
PARTITION p2 VALUES LESS THAN MAXVALUE
);
以上是在MySQL中使用分区表对数据进行逻辑分隔的例子。
ALTER TABLE table_name ENGINE=InnoDB;
更改表的存储引擎,例如从MyISAM改为InnoDB。
以上只是MySQL部分高级功能的简单展示,实际使用时请根据具体需求和场景选择合适的功能,并结合官方文档深入学习。
python推荐学习汇总连接:
50个开发必备的Python经典脚本(1-10)
50个开发必备的Python经典脚本(41-50)
————————————————
?最后我们放松一下眼睛