浮头导航网

专注编程技术分享的开发者社区

MySQL中的命令及运用_mysql命令使用

MySQL中的命令丰富多样,下面从数据库操作、表操作、数据操作、查询操作等多个方面为你详细介绍:


数据库操作命令


1. 创建数据库


CREATE DATABASE database_name;


例如,创建一个名为 test_db 的数据库:


CREATE DATABASE test_db;


2. 删除数据库


DROP DATABASE database_name;


若要删除 test_db 数据库:


DROP DATABASE test_db;


3. 使用数据库


USE database_name;


使用 test_db 数据库:


USE test_db;


4. 查看所有数据库


SHOW DATABASES;


表操作命令


1. 创建表


CREATE TABLE table_name (

column1 datatype constraint,

column2 datatype constraint,

...

);


创建一个名为 students 的表,包含 id 、 name 和 age 字段:


CREATE TABLE students (

id INT PRIMARY KEY AUTO_INCREMENT,

name VARCHAR(50),

age INT

);


2. 删除表


DROP TABLE table_name;


删除 students 表:


DROP TABLE students;


3. 修改表结构


● 添加列


ALTER TABLE table_name ADD column_name datatype;


在 students 表中添加 gender 列:


ALTER TABLE students ADD gender VARCHAR(10);


● 修改列类型


ALTER TABLE table_name MODIFY column_name new_datatype;


将 students 表中 age 列的数据类型修改为 SMALLINT :


ALTER TABLE students MODIFY age SMALLINT;


● 删除列


ALTER TABLE table_name DROP COLUMN column_name;


删除 students 表中的 gender 列:


ALTER TABLE students DROP COLUMN gender;


● 重命名表


ALTER TABLE old_table_name RENAME TO new_table_name;


将 students 表重命名为 pupils :


ALTER TABLE students RENAME TO pupils;


4. 查看表结构


DESCRIBE table_name;


查看 students 表的结构:


DESCRIBE students;


5. 查看所有表


SHOW TABLES;


数据操作命令


1. 插入数据


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


向 students 表中插入一条记录:


INSERT INTO students (name, age) VALUES ('John', 20);


2. 更新数据


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


将 students 表中 name 为 John 的记录的 age 更新为 21:


UPDATE students SET age = 21 WHERE name = 'John';


3. 删除数据


DELETE FROM table_name WHERE condition;


删除 students 表中 age 大于 25 的记录:


DELETE FROM students WHERE age > 25;


查询操作命令


1. 基本查询


SELECT column1, column2, ... FROM table_name;


查询 students 表中的 name 和 age 列:


SELECT name, age FROM students;


2. 条件查询


SELECT * FROM table_name WHERE condition;


查询 students 表中 age 大于 18 的所有记录:


SELECT * FROM students WHERE age > 18;


3. 排序查询


SELECT * FROM table_name ORDER BY column_name ASC|DESC;


按 age 降序查询 students 表中的记录:


SELECT * FROM students ORDER BY age DESC;


4. 分组查询


SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;


按 age 分组,统计每组的记录数:


SELECT age, COUNT(*) FROM students GROUP BY age;


5. 多表查询


SELECT * FROM table1 JOIN table2 ON table1.column = table2.column;


假设还有一个 courses 表和 enrollments 表,查询学生姓名和所选课程名称:


SELECT students.name, courses.course_name

FROM students

JOIN enrollments ON students.id = enrollments.student_id

JOIN courses ON enrollments.course_id = courses.id;


其他常用命令


1. 事务操作


● 开启事务


START TRANSACTION;


● 提交事务


COMMIT;


● 回滚事务


ROLLBACK;


2. 索引操作


● 创建索引


CREATE INDEX index_name ON table_name (column_name);


在 students 表的 name 列上创建索引:


CREATE INDEX idx_name ON students (name);


● 删除索引


DROP INDEX index_name ON table_name;


删除 idx_name 索引:


DROP INDEX idx_name ON students;


3. 用户和权限管理


● 创建用户


CREATE USER 'username'@'hostname' IDENTIFIED BY 'password';


创建一个名为 user1 ,密码为 password123 的用户:


CREATE USER 'user1'@'localhost' IDENTIFIED BY 'password123';


● 授予权限


GRANT privilege_type ON database_name.table_name TO 'username'@'hostname';


授予 user1 用户对 test_db 数据库中 students 表的 SELECT 和 INSERT 权限:


GRANT SELECT, INSERT ON test_db.students TO 'user1'@'localhost';


● 撤销权限


REVOKE privilege_type ON database_name.table_name FROM 'username'@'hostname';


撤销 user1 用户对 test_db 数据库中 students 表的 INSERT 权限:


REVOKE INSERT ON test_db.students FROM 'user1'@'localhost';


● 刷新权限


FLUSH PRIVILEGES;

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言