从入门到自闭之Python--MySQL数据库的操作命令

命令:
  1. mysqld install; 配置数据库

  2. net start mysql;启动数据库

  3. mysql -uroot -p; 以root权限启动数据库,-p之后输入密码

  4. mysql -uroot -h"IP地址" -p; 设置远程连接

  5. set password = password('密码'); 设置密码

  6. mysqladmin -u用户名 -p旧密码 password 新密码

  7. show databases; 展示所有的数据库

  8. show create table 表名; 查看表结构(会查看到创建表的语句,包括约束条件,主键等)

  9. use 数据库名;切换数据库名

  10. show variables like '%chara%';查看当前编码

    1. 临时修改(在客户端执行):set xxxx = utf8;
    2. 永久解决:在my.ini 添加 set xxxx = utf8;
    3. 实时解决问题:create tables 表名() charset = utf8;
  11. 创建:

    1. select user(); 查看当前使用用户

    2. create database 数据库名;创建数据库

    3. create table 表名(字段名1 类型(条件)); 创建表,字段名不能一样

       create table demo(num int,username char(12),password char(32));
    4. insert into mysql.user(Host,User,Password) values("localhost","用户名",password("密码"));创建一个localhost账户用户,该账户只能在本地登录,不能字啊另一台机器上远程登录

    5. insert into mysql.user(Host,User,Password) values("%","用户名",password("密码"));创建一个在任意一台电脑上都可以登录的账户,也可以指定某台机器可以在远程登录。

  12. 删除具体操作:

    1. drop user 用户名@'%'; 删除账户
    2. drop user 用户名@'localhost';删除用户权限
    3. drop database 数据库名; 删除数据库
    4. drop table 表名;删除表
  13. 权限:

    1. flush privileges;刷新权限
    2. grant all privileges on 数据库名.* to 用户名@localhost identified by '密码';授权给某个用户这个数据库的所有权限
    3. 格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";
    4. grant select,update on 数据库名.* to 用户名@localhost identified by '密码';
    5. grant select,delete,update,create,drop on . to 用户名@"%" identified by "密码";授权用户拥有所有数据库的某些权限
    6. @"%" 表示对所有非本地主机授权,不包括localhost。(localhost地址设为127.0.0.1
    7. 对localhost授权:加上一句grant all privileges on 数据库名.* to 用户名@localhost identified by '密码';即可
    8. show grants for 'root'@'localhost';查看数据库中具体某个用户的权限
    9. GRANT ALL ON . TO 用户名@127.0.0.1 WITH GRANT OPTION;修改用户权限
  14. 查看数据:

    1. select database() ;查看当前所在库
    2. select * from 表名; 查看表中所有数据
    3. SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; 查看数据库中的所有用户
  15. desc 表名; / describe 表名;查看表结构

  16. insert into 表名 values(数据);表里添加数据

  17. update 表名 set password='alex3714' where num=1;更新数据

  18. delete from 表名 where num=1;删除表中数据、

权限:
  1. usage:使用权限
  2. select:查看数据
  3. update:更新
  4. insert:写入
  5. delete:清除数据
  6. all:所有权限
  7. on后面跟数据库中的某个表
  8. ( * )代表所有的表(数据库.*)
基础操作:
  1. 数据库的操作
    1. create database 数据库名 ; 创建一个数据库名,带有具体意义的英文名字
    2. show databases; 查看有多少个数据库
    3. use 数据库名; 切换数据库
    4. select database(); 查看当前所在的库
  2. 表的操作
    1. create table 英文表名(num int , username char (12),password char(32));
    2. show tables;查看当前有多少表
    3. desc 表名; 查看表结构
    4. describe 表名; 查看表结构
    5. alter table 表名,修改表名
  3. 数据的操作
    1. insert into 表名 values(1,'alex','123'); 必须一一对应
    2. select * from 表名; 查看表中所有数据
    3. update 表名 set 数据名='xxxxx' where num = 1; 修改数据
    4. delete from 表名 where num = 1; 删除数据

猜你喜欢

转载自www.cnblogs.com/heyulong1214/p/12069842.html