共计 5419 个字符,预计需要花费 14 分钟才能阅读完成。
这篇文章给大家分享的是有关 MySQL 数据库基础操作命令有哪些的内容。丸趣 TV 小编觉得挺实用的,因此分享给大家做个参考,一起跟随丸趣 TV 小编过来看看吧。
1、使用帮助信息
登陆数据库就不介绍了哦,比如说想做创建的数据库的操作,不知道命令,就可以查看帮助信息
mysql help create; Many help items for your request exist. To make a more specific request, please type help item , where item is one of the following topics: CREATE DATABASE # 最简单的创建命令 CREATE EVENT CREATE FUNCTION CREATE FUNCTION UDF CREATE INDEX CREATE LOGFILE GROUP CREATE PROCEDURE CREATE SERVER CREATE TABLE CREATE TABLESPACE CREATE TRIGGER CREATE USER CREATE VIEW SHOW SHOW CREATE DATABASE SHOW CREATE EVENT SHOW CREATE FUNCTION SHOW CREATE PROCEDURE SHOW CREATE TABLE SPATIAL
2、创建、删除、查看数据库
mysql create database test_data; # 创建默认字符集的数据库(默认是拉丁字符集) Query OK, 1 row affected (0.02 sec) mysql show databases like test% +------------------+ | Database (test%) | +------------------+ | test_data | +------------------+ 1 rows in set (0.00 sec)
创建 gbk 字符集的数据库
mysql create database test_gbk DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; Query OK, 1 row affected (0.04 sec) mysql show create database test_gbk; # 查看创建数据库的语句 +----------+----------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------+ | test_gbk | CREATE DATABASE `test_gbk` /*!40100 DEFAULT CHARACTER SET gbk */ | +----------+----------------------------------------------+ 1 row in set (0.00 sec)
删除数据库
mysql drop database test_data; Query OK, 0 rows affected (0.07 sec) mysql show databases; +----------------------------+ | Database | +----------------------------+ | information_schema | | test_gbk | +----------------------------+
3、连接数据库
mysql use test_gbk; # 相当于 cd 命令,切换到数据库进行操作 Database changed mysql select database(); # 查看当前连接的数据库,相当于 pwd +------------+ | database() | +------------+ | test_gbk | +------------+ 1 row in set (0.00 sec) mysql select user(); # 查看当前连接数据库的用户, 相当于 whoami +--------------------+ | user() | +-------------------+ | root@localhost | +--------------------+ 1 row in set (0.00 sec)
4、创建用户、授权、收回权限
当数据库创建完成后,就需要创建用户,以供需要连接数据库的人员使用与操作数据库,不可能人人使用 root 登陆,所以权限设置也是很重要的
mysql grant all on test_gbk.* to testuser @ localhost identified by 123456 # 创建用户并 all 权限给在 test_gbk 库所有表,密码 lsquo;123456 rsquo; Query OK, 0 rows affected (0.00 sec) mysql flush privileges; # 刷新权限,使权限生效 Query OK, 0 rows affected (0.00 sec) mysql show grants for testuser @ localhost # 查看用户有哪些权限 +-----------------------------------------------------------------------------------------------------------------+ | Grants for testuser@localhost | +-----------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO testuser @ localhost IDENTIFIED BY PASSWORD *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | GRANT ALL PRIVILEGES ON `test_gbk`.* TO testuser @ localhost | +-----------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
收回权限
mysql revoke insert,update,select,delete on test_gbk.* from testuser @ localhost # 将以上权限收回 Query OK, 0 rows affected (0.00 sec) mysql show grants for testuser @ localhost +----------------------------------------------------------------------------------------------------+ | Grants for testuser@localhost | +----------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO testuser @ localhost IDENTIFIED BY PASSWORD *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | GRANT CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `test_gbk`.* TO testuser @ localhost | +------------------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
注:原来不知道 all 权限到底是哪些权限,采用这种方法之后,应该就清楚了
SELECT,INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER
5、创建、删除表
mysql create table test(id int(4)not null,name char(20)not null); # 建表,并且建立两个字段 Query OK, 0 rows affected (0.06 sec) mysql show tables;# 查看表 +--------------------+ | Tables_in_test_gbk | +--------------------+ | test | +--------------------+ 1 row in set (0.00 sec) mysql desc test; # 查看表结构 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(4) | NO | | NULL | | | name | char(20) | NO | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql create table test1(id int(4)not null,name char(20)not null); Query OK, 0 rows affected (0.01 sec) mysql show tables; +------------------------+ | Tables_in_test_gbk | +------------------------+ | test | | test1 | +--------------------+ 2 rows in set (0.00 sec)
删除表
mysql drop tables test; Query OK, 0 rows affected (0.00 sec) mysql show tables; +--------------------+ | Tables_in_test_gbk | +--------------------+ | test1 | +--------------------+ 1 row in set (0.00 sec)
查看建表
mysql show create table test1\G *************************** 1. row *************************** Table: test1 Create Table: CREATE TABLE `test1` ( `id` int(4) NOT NULL, `name` char(20) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=gbk 1 row in set (0.00 sec)
感谢各位的阅读!关于“MySQL 数据库基础操作命令有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!