• MySQL相关命令


    进入数据库:MySQL -u root -p
    查看数据库:show databases
    创建库:create database 库名称
    使用数据库:use 数据库名字
    查看正在使用的数据库:select database()
    查看表:show tables

    创建表:create table 表名称(各字段及其类型和属性)
    更改表的名字:alter table 表原名称 rename to 表新名称
    插入数据:insert into 表名称() values(数据)
    查看表的创建信息:show create table 表名称
    查看表的构造:desc 表名称
    查看表的全部信息:select * from表名称

    修改字段值:update 表名称 set 字段值=新值 where 字段值=旧值
    删除字段值:delete from 表名称 where 字段值=更新条件
    增加字段:alter table 表名称 add 字段 字段类型 字段属性
    删除字段:alter table 表名称 drop 字段
    修改字段:alter table 表名称 change 字段旧名 字段新名 字段类型 字段属性
    调整字段的顺序:alter table 表名称 modify 字段 字段类型 after 定位字段

    字符集全部命令:
    查看服务器支持的字符集:show character set
    查看字符集的校对规则:show collation like ‘utf8’
    查看当前库的字符集:show variables like ‘character%’
    查看当前库的校对规则:show variables like “collation%”
    创建表的时候直接分配字符集和表类型:create table 表名称(各字段及其类型和属性) engine=innodb defaultcharacter set gbk
    修改库的字符集:alter database 库名称 default character set 新的字符集
    修改表的字符集:alter table 表名称 convert to character set 新的字符集
    修改字段的字符集:alter table 表名称 modify 字段 字段属性 character set 新的字符集

    select全部命令:
    查看表的全部信息:select * from 表名称
    精确查询表的一个字段:select * from 表名称 where 定位信息
    精确查询表的多个字段:select * from 表名称 where 字段 in 定位信息
    精确显示和查询表的多个字段:select 列1,列2 from 表名称 where 字段 in 定位信息
    运算符和逻辑查询:slect * from 表名称 where (ID>1或者ID=1+1或者ID>2 and id<10或者ID>2 or ID<10)
    模糊查询:select * from 表名称 where 列 like 值(name like ‘%_’)
    聚合查询:select (count(*)或者sum(*)或者max(*)或者min(*)或者avg(*))from 表名称
    分组查询:select 展现的列 from 表名称 group by 参考列
    select name,count(name)from 表名称 group by name
    条件显示分组查询:select name count(name) from 表名称 group by name having count(name)>5
    顺序排列:select name,count(*) from 表名称 group by name order by name asc
    逆序排列:select name,count(*) from 表名称 group by name order by name desc
    左外连接:select 表1.列,表2.列 from 表名称 表1 left join 表2 on 表1.列=表2.列
    右外连接:select 表1.列,表2.列 from 表名称 表1 right join 表2 on 表1.列=表2.列


    添加外键约束
    create table student(id int(100)not null primary key auto_increment,name char(100) not null default '',age int not null default 0,birthday date not null default '2018-09-29',address varchar(100) not null default 'China',salary double not null default 0.00,body text not null,foreign key student(sid) references school(id) )

    终极创建表:
    create table student(id int(100)not null primary key auto_increment,name char(100) not null default '',age int not null default 0,birthday date not null default '2018-09-29',address varchar(100) not null default 'China',salary double not null default 0.00,body text not null,constarint w_key foreign key student(sid) references school(id) on delete cascade on update cascade)engine=innodb default characset=gbk;

    创建唯一索引:create table 表名称(列,列属性,unique)
    建表后添加唯一索引:create unique index 索引名 on 表名称(列)
    建表后添加约束:alter table 表名称 add constraint 索引名 unique(列)from
    创建普通索引:create index 索引名 on 表名称(列)
    查看索引:show index from 表名称
    删除索引:drop index 索引名 on 表名称
    外键约束:foreign key(外键名) references 表名称(主键)

    关闭自动提交:set autocommit=1(默认)将1改成0 如果想手动彻底提交,commit
    回滚:rollback 支持innodb表类型


    用户管理:
    进去mysql:use mysql
    查看用户:select user,host,password from user
    创建用户:create user 用户名@主机名(‘admin’@'localhost') identified by 密码(‘admin’)
    赋予权限:grant 权限(insert,update,delete,select) on (*.*或者库名称.*或者库名称. 表 名称)to 用户名@主机名(‘admin’@’localhost’)
    修改收回权限:revoke 权限(insert,update,delete,select) on (*.*或者库名称.*或者库名称. 表名称) from 用户名@主机名(‘admin’@’localhost’)
    设置密码:set password for 用户名@主机名(‘admin’@’localhost’)=PASSWORD(‘新密码’)
    修改密码:update 表名称 set password=PASSWORD('admin') where user='admin' && host='localhost'
    删除用户:delete from user where user=‘admin’&& host=‘localhost’
    drop user 用户名@主机名(‘admin’@’localhost’)

    视图:
    创建视图:create view 视图名(select_view) as 语句(select * from table)
    查询视图:show create view 视图名(select_view)
    创建具有外键约束的视图:create view see_view as select student.id,student.name,scores.python,scores.liuix from student join scores on student.id=scores.sid
    删除视图:drop view 视图名(select_view)
    使用视图:select * from 视图名(select_view)

    备份数据库:mysql dump -u root -p 数据库(python)>d:001.sql
    恢复数据库:mysql -u root -p 数据库(python)<d:/001.sql

  • 相关阅读:
    追随我心
    开心孕期创业经验和教训总结
    记和老友李吃饭
    如何理解hashCode的作用:
    周计划(2014.08.05~2014.08.10)
    个人职业提升内容
    个人职业发展分析和实现方法
    go 实现的排序算法
    xxx go内置函数
    6.并发
  • 原文地址:https://www.cnblogs.com/zhangweijie01/p/10169456.html
Copyright © 2020-2023  润新知