MySQL数据库常用基本操作

红太狼 2022-06-17 01:14 294阅读 0赞

1、显示数据库

  1. show databases;

2、选择数据库

  1. use 数据库名;

3、显示数据库中的表

  1. show tables;

4、显示数据表的结构

  1. describe 表名;

5、显示表中记录

  1. SELECT * FROM 表名

6、建库

  1. create databse 库名;

7、建表

复制代码

  1. create table 表名 (字段设定列表);
  2. mysql> create table name(
  3. -> id int auto_increment not null primary key ,
  4. -> uname char(8),
  5. -> gender char(2),
  6. -> birthday date );
  7. Query OK, 0 rows affected (0.03 sec)
  8. mysql> show tables;
  9. +------------------+
  10. | Tables_in_userdb |
  11. +------------------+
  12. | name |
  13. +------------------+
  14. 1 row in set (0.00 sec)
  15. mysql> describe name;
  16. +----------+---------+------+-----+---------+----------------+
  17. | Field | Type | Null | Key | Default | Extra |
  18. +----------+---------+------+-----+---------+----------------+
  19. | id | int(11) | NO | PRI | NULL | auto_increment |
  20. | uname | char(8) | YES | | NULL | |
  21. | gender | char(2) | YES | | NULL | |
  22. | birthday | date | YES | | NULL | |
  23. +----------+---------+------+-----+---------+----------------+
  24. 4 rows in set (0.00 sec)
  25. 注: auto_increment 自增
  26. primary key 主键

复制代码

8、增加记录

  1. insert into name(uname,gender,birthday) values('张三','男','1971-10-01');

9、修改记录

  1. update name set birthday='1971-01-10' where uname='张三';

10、删除记录

  1. delete from name where uname='张三';

11、删除表

  1. drop table 表名

12、删除库

  1. drop database 库名;

13、备份数据库

  1. mysqldump -u root -p --opt 数据库名>备份名; //进入到库目录

14、恢复

  1. mysql -u root -p 数据库名<备份名; //恢复时数据库必须存在,可以为空数据库

15、数据库授权

 格式:grant select on 数据库.* to 用户名@登录主机 identified by “密码”

例1、增加一个用户user001密码为123456,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL,然后键入以下命令:

  1. mysql> grant select,insert,update,delete on *.* to user001@"%" Identified by "123456";

例2、增加一个用户user002密码为123456,让此用户只可以在localhost上登录,也可以设置指定IP,并可以对数据库test进行查询、插入、修改、删除的操作 (localhost指本地主机,即MySQL数据库所在的那台主机)

  1. //这样用户即使用知道user\_2的密码,他也无法从网上直接访问数据库,只能通过MYSQL主机来操作test库。
  2. //首先用以root用户连入MySQL,然后键入以下命令:
  3. mysql>grant select,insert,update,delete on test.* to user002@localhost identified by "123456";

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读