MySQL数据库操作指令

一、数据库创建用户

1)简单创建

   CREATE  USER  ty@localhost;

2)带有密码

   CREATE  USER  ty@localhost  IDENTIFIED  BY ‘123456’

这里的ty@localhost是创建的用户名,123456是用户的密码。

 

注意:MySQL数据库命令不区分大小写。

 

二、创建用户之后,需要对用户赋予相应的权限,一般用GRANT指令

其格式为:GRANT 权限 ON 权限范围 to 用户名@登录主机 IDENTIFIED BY ‘密码’;

1)GRANT ALL ON *.* TO ‘ty’@’localhost’ ;

       让用户在本地登录,并赋予其所有数据库、表所有权限,“ALL”的意思是所有权限,ON子句中*.*是所有数据库、表,”localhost”表示在本地登录。

2GRANT SELECT,UPDATE,DELETE,INSERT ON *.* TO ‘ty’@’%’ IDENTIFIED BY ‘123456’

   让用户ty在所有地方都可以登录赋予其增删查改等权限,这里的’%’表示用户在所有地方任何主机都可以登录。

 

三、查看权限

    1)当前用户

  Show  grants;

2)其他用户(使用关键词for

      Show  grants  for  ty@localhost;

 

四、撤销权限

撤销已经赋予给Mysql用户的权限,用revoke指令,另外只需把关键字“to”换成“from”即可:

Grant  all  on  *.*  to  ty@localhost;

Revoke all  on  *.*  from  ty@localhost;

 

五、数据库操作指令

   1.1创建数据库

      Create  database  Tang;

   1.2创建数据表

     Create  table  userinfo(

         id  int(4)  not null  primary key  auto_increment,

         userName  char(20)  not null,

         password  char(20)  not null);

 

   2.1查看显示数据库

      Show  databases;

   2.2显示数据表

       使用desc语句,获取数据表结构

mysql> desc userinfo;

 

+-----------+-------------+----------+---------+----------------+--------+

| Field          | Type           | Null         | Key        | Default            | Extra    |

+-----------+-------------+----------+----------+---------------+--------+

| id              | int(11)        | NO          | PRI         | NULL              |             |

| userName  | varchar(20)  | NO          |              | NULL              |             |

| password   | varchar(20)  | NO         |               | NULL             |             |

+----- ------+-------------+---------+----------+---------------+---------+

3 rows in set (0.01 sec

 

 

   3.1删除数据库

      drop  database  Tang;

      drop  database  if  exists  Tang;

   3.2删除表格的字段

      Alter  table  userinfo  drop  new_sex;

   3.3 删除数据表

       drop  table  userinfo;

   3.4 删除表中具体的数据注意这里只用的命令也不同,是delete不是drop

       delete  from  userinfo  where  id=0;

      注意: 在不确定是否存在一个数据库或表时,如果要执行删除指令,最好加上一个条件判断“if  exists,这样可避免发生错误。

 

   4.1添加表的字段,这是在表的结构上做添加

       Alter  table  userinfo  add  sex  char(10)  not null;

   4.2往表中插入数据,这是对表的内容做添加

      Insert  into  userinfo  values(0,’ty’,’1234’),(1,’xy’,’5678’);

 

   5.1修改数据库的密码

      Set  password  for  ty@localhost  =  old_password(‘123456’);  

   5.2 修改原表的字段

   Alter  table  userinfo  change  sex  new_sex  bit  not null;

   5.3 修改表中的数据

       Updata  userinfo  set  userName=’th’  where  id=1;

   5.4修改表名

       使用rename指令

      RENAME  table  userinfo  to  user;

   

    6.use指令表示选择一个数据库

      Use  Tang;

 

 

    7.查询表中的数据(select

          查询所有数据

             Select  * from  userinfo;

          查询id0~2之间的行数据

             Select  * from  userinfo  order by  id  limit 0,2;

          查询前5条数据

             Select  * from  userinfo  limit 5;

          查询具体某一条数据

             Select  *from  userinfo  where  id=1;     

 

posted on 2016-01-16 16:44  tyingxy  阅读(186)  评论(0编辑  收藏  举报

导航