注:示例均在为具有所有操作权限的root用户操作
1、用户操作相关类
1)创建用户
命令:create user 'username'@'host' identified by 'password';
说明:username - 你将创建的用户名,
host - 指定该用户在哪个主机上可以登陆,此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录,如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录;也可以指定某台机器可以远程登录;
password - 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器。
举例:
mysql> create user 'test'@'localhost' identified by 'test';
Query OK, 0 rows affected (0.00 sec)
2)查看已创建用户
命令:select host,user from mysql.user;
举例:
mysql> use mysql;
mysql> select host,user from mysql.user;
+-----------+-----------+
| host | user |
+-----------+-----------+
| % | root |
| localhost | tangyuan1 |
| localhost | test |
+-----------+-----------+
3 rows in set (0.00 sec)
3)修改用户密码
命令:set password for 'username'@'host' = password('newpassword')
说明:
username - 你修改的用户名,
host - 用户名设置的登录主机
newpassword - 新设置的密码
举例:将1)中创建的test用户密码修改为test1
mysql> set password for 'test'@'localhost' = password('test1');
Query OK, 0 rows affected, 1 warning (0.00 sec)
4)授权用户权限
命令:grant privileges on databasename.tablename to 'username'@'host'
说明:
privileges - 用户的操作权限,如select,insert,update 等,如果要授予所的权限则使用all
databasename - 数据库名
tablename-表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示, 如*.*
举例:给1)中创建的test用户服务所有数据库和表的全部操作权限
mysql> grant all on *.* to 'test'@'localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges; #注:更新权限后必须执行flush privileges更新权限
Query OK, 0 rows affected (0.00 sec)
5)查看用户权限
命令:show grants for 'username'@'host'
说明:
username - 待查询的用户名
host - 用户设置的登录主机,不写为%
举例:查询3)修改密码后的1)用户test的操作权限
mysql> show grants for test@'localhost';
+---------------------------------------------------+
| Grants for test@localhost |
+---------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost' |
+---------------------------------------------------+
1 row in set (0.00 sec)
6)撤销用户权限
命令:revoke privilege on databasename.tablename from 'username'@'host'
举例:撤销5)中设置的test用户权限
mysql> revoke all on *.* from 'test'@'localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show grants for test@'localhost';
+------------------------------------------+
| Grants for test@localhost |
+------------------------------------------+
| GRANT USAGE ON *.* TO 'test'@'localhost' |
+------------------------------------------+
1 row in set (0.00 sec)
7)删除用户
命令:drop user 'username'@'host'
举例:删除1)中创建的test用户
mysql> drop user 'test'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user from mysql.user; #查看用户列表已经没有test用户
+-----------+-----------+
| host | user |
+-----------+-----------+
| % | root |
| localhost | tangyuan1 |
+-----------+-----------+
2 rows in set (0.00 sec)
mysql> shwo grants for test@'localhost'; #查看test用户权限失败
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'shwo grants for test@'localhost'' at line 1
2、数据库操作相关类
1)创建数据库
命令:create database databasename
说明:
databasename - 待创建的数据库名
举例:创建一个名为ttt的数据库
mysql> create database ttt;
Query OK, 1 row affected (0.00 sec)
2)查看已创建数据库
命令:show databases
举例:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| one250 |
| performance_schema |
| sys |
| ttt |
+--------------------+
6 rows in set (0.00 sec)
3)连接数据库
命令:use databasename
举例:连接2)中创建的ttt数据库
mysql> use ttt;
Database changed
4)打印当前连接数据库
命令:select database()
举例:
mysql> select database();
+------------+
| database() |
+------------+
| ttt |
+------------+
1 row in set (0.00 sec)
5)删除数据库
命令:drop database databasename
说明:
database name- 待删除的数据库名
举例:删除1)中创建的ttt数据库
mysql> drop database ttt;
Query OK, 0 rows affected (0.01 sec)
mysql> show databases; #查看数据库列表,ttt数据库已经被删除
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| one250 |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
3、数据表操作相关类
1)创建数据表
命令:create table tablename ( <attribute name 1> <type 1> [,..<attribute name n> <type n>])
说明:
tablename - 待创建的数据表名
attribute name - 属性名
type - 类型
举例:为ttt数据库创建一个名为tb_temp1的数据表
mysql> use ttt; #使用ttt数据库
Database changed
mysql> create table tb_temp1 (id int(4),name varchar(25),salary float);
Query OK, 0 rows affected (0.16 sec)
2)查看创建数据表
命令:show tables
举例:
mysql> show tables;
+---------------+
| Tables_in_ttt |
+---------------+
| tb_temp1 |
+---------------+
1 row in set (0.00 sec)
3)数据表插入数据
命令:insert intotablename (attribute name 1,attribute name 2,...) value (value 1, value 2,...)
说明:
tablename - 待插入数据的数据表名
attribute name - 属性名
value - 插入对应属性的值
举例:
A、对表中的所有属性名插入值
对1)中创建的tb_temp1数据表,插入id=1,name="test",salary=10000的值
mysql> insert into tb_temp1 (id,name,salary) value (1,'test',10000);
Query OK, 1 row affected (0.02 sec)
B、对表中指定属性名插入值
对1)中创建的tb_temp1数据表,插入name="test1",salary=11000的值
mysql> insert into tb_temp1 (name,salary) value ('test1',11000);
Query OK, 1 row affected (0.02 sec)
C、对表同时插入多条数据
命令:insert intotablename (attribute name 1,attribute name 2,...)value (value 10,value 20,...), (value11, value21)
说明:
tablename - 待插入数据的数据表名
attribute name - 属性名
value - 插入对应属性的值
举例:对1)中创建的tb_temp1同时插入id=3,name='test2',salary=12000和id=4,name='test3',salary=13000的数据
mysql> insert into tb_temp1 (id,name,salary) value (3,'test2',12000), (4,'test3',13000);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
4)查看数据表中的数据
命令: select <attribute name 1,attribute name 2,...> from < tablename> where <expression >
说明:
attribute name - 属性名
tablename - 待查数据表名
expression - 表达式
查询数据表的所有数据命令为:select * from tablename
举例1:查询1)中创建的tb_temp1数据表中的所有数据
mysql> select * from tb_temp1;
+------+-------+--------+
| id | name | salary |
+------+-------+--------+
| 1 | test | 10000 |
| NULL | test1 | 11000 |
| 3 | test2 | 12000 |
| 4 | test3 | 13000 |
+------+-------+--------+
4 rows in set (0.00 sec)
举例2:查询1)中创建的tb_temp1数据表id=1的所有数据
mysql> select * from tb_temp1 where id=1;
+------+------+--------+
| id | name | salary |
+------+------+--------+
| 1 | test | 10000 |
+------+------+--------+
1 row in set (0.00 sec)
5)更新数据表中的数据
命令:update tablename set attribute name 1=value 1,attribute name 2=value 2, ... where conditions
说明:
tablename - 待更新的数据表名
attribute name - 属性名
value - 属性值
conditions - 满足更新的条件
举例:更新1)中tb_temp1数据表满足id=1的name属性值为id1
mysql> update tb_temp1 set name='id1' where id=1;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tb_temp1 where id=1;
+------+------+--------+
| id | name | salary |
+------+------+--------+
| 1 | id1 | 10000 |
+------+------+--------+
1 row in set (0.00 sec)
6)增加数据表中的属性名
命令:alter tabletablenameaddattribute name type others
说明:
others - 其他
举例:向1)中的tb_temp1数据表增加名为sex的属性,默认为'male'
mysql> alter table tb_temp1 add sex varchar(25) default 'male';
Query OK, 0 rows affected (0.29 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from tb_temp1;
+------+-------+--------+------+
| id | name | salary | sex |
+------+-------+--------+------+
| 1 | id1 | 10000 | male |
| NULL | test1 | 11000 | male |
| 3 | test2 | 12000 | male |
| 4 | test3 | 13000 | male |
+------+-------+--------+------+
3 rows in set (0.00 sec)
7)清空数据表
命令:truncate table tablename
举例:清空1)中的数据表tb_temp1
mysql> truncate table tb_temp1;
Query OK, 0 rows affected (0.12 sec)
mysql> select * from tb_temp1;
Empty set (0.00 sec)
8)删除数据表中的数据
命令:delete fromtablename where conditions
说明:
tablename - 数据表名
conditions - 满足删除的条件
举例:删除1)tb_temp1中,id=4的数据
mysql> delete from tb_temp1 where id=4;
Query OK, 1 row affected (0.02 sec)
mysql> select * from tb_temp1;
+------+-------+--------+
| id | name | salary |
+------+-------+--------+
| 1 | id1 | 10000 |
| NULL | test1 | 11000 |
| 3 | test2 | 12000 |
+------+-------+--------+
3 rows in set (0.00 sec)
网友评论