美文网首页
常用MySQL命令

常用MySQL命令

作者: iGroove | 来源:发表于2017-04-25 20:27 被阅读0次
  • 基本操作curd

   C:create 创建
   U:update 修改
   R:read 读|查询
   D:delete 删除
  • sql分类

  # DDL:数据定义语言
       操作对象:数据库和表
       关键词:create alter drop
  # DML:数据操作语言
       操作对象:记录
  # DQL:数据查询语言(非官方)
  # DCL:数据控制语言
       操作对象:用户 事物 权限
规范 整型 浮点型 字符型
  • 建表操作

  • 查看表的创建信息
show create table tb_name;
  • 查看所有数据库
show databases;
  • 使用数据库
use db_name;
  • 查看当前正在使用的数据库
select database();
  • 查看表结构
show columns form tb_name;
  • 插入全部列表数据
insert tb_name values(v1,v2,v3···) ;
  • 插入指定列表数据
insert tb_name(列名1,列名5) values(v1,v5);
  • 所有记录查找
select * from tb_name ;
  • 外键约束作用 保持数据一致性,完整性,实现一对一或一对多关系

子表:指的是具有外键列的表 子表所参照的表称为父表

foreign key (pid) references provinces (id);

  • 修改数据表

  • 添加单列

 alter table tb_name add tb_name 列定义(int ) [first] | after col_name];
  • 删除单列
alter table tb_name drop col_name;
  • 添加多列
alter table tb_name add tb_name defined [first] | after col_name],
add tb_name defined [first] | after col_name];
  • 添加主键约束
alter table users2 add primary key (id);
alter table test add constraint PK_test_id primary key(id);
  • 删除主键约束
  alter tb_name drop primary 'key';
  • 添加唯一约束
alter table users2 add unique key (id);
  • 删除唯一约束
alter table tb_name drop  index dex_name;
  • 添加外键约束
alter table users2 add foreign key (id) references refer_tb_name (iid);
  • 删除外键约束
alter table tb_name drop foreign key fk_symbol;
  • 添加默认约束
alter table tb_name  alter col_name set default 'key';
  • 删除默认约束
alter table tb_name  alter col_name drop default; 
  • 修改列定义
alter table tb_name modify [column] col_name 
col_dedinition[first |after col_name]  ;
  • 修改列名称
alter table tb_name change [column] old_col_name
 new_col_name column_definition [first | after col_name];
  • 数据表更名
alter table tb_name rename [to | as] new _tb_name;
rename  table tb_name1 to new_tb_name [,tb_name2 to new_tb_name];
  • 删除基本表
drop table tb_name;

清空表

truncate tb_name;
Paste_Image.png
  • 操作数据表中的记录

  • 插入记录
insert tb_name [(col_name,···)] {values | value}({expr | default},···),···;
insert tb_name set col_name={expr | default},···;
insert tb_name [(tb_name)] select...;
  • update

  • 更新记录
update tb_reference set col_name1='key',col_name2='key' [where where_condition];
  • 删除记录
delete from tb_name [where where_condition];
  • 查找记录
select * from tb_name;
  • 查询去重
select distinct 字段名 from ta_name;
  • 取别名
  select prince+10  ‘新价格 ’ from tb_name;
  • 限制查找返回记录数量
select * from tb_name limit 2;
  • 连接查询

  • 内连接
格式1(推荐):select a.*,b.* from a [inner] join b on ab_condition
格式2(隐式):select a.*,b.* from a ,b where ab_condition
  • 外链接
左外连接:select a.*,b.* from a left [outer] join b on ab_condition
右外连接:select a.*,b.* from a right [outer] join b on ab_condition
  • 子查询
是将一个查询的结果作为一张临时的表
例子:select user.*,temp.* from user,
(select * from orders where price>300) as temp where user.id=temp.id;
  • 获取本地时间
select now();
//获取当前时间
select date_format(now(),'%Y年%m月%d日 %H点:%i分%s秒');

delimiter

  • 自定义函数
create function fun_name returns routine_body;
  • 查看字符集
show variables like '%char%';
查看字符集结果
  • 查看隔离级别
SHOW VARIABLES LIKE '%isolation'; 
  • 设置隔离级别
set session transaction isolation level read uncommitted;
  • 数据库备份
在命令行输入
mysqldump -uuser -ppwd db_name>备份到的位置
eg: mysqldump -uroot -p1234 store>d:\1.sql
  • 数据库还原
1.手动创建数据库
2.mysql -uroot -p1234 db_name<sql位置
eg:mysql -uroot -p1234 store1<d:1.sql
或者登陆到mysql
source sql位置
eg:source d:1.sql

相关文章

网友评论

      本文标题:常用MySQL命令

      本文链接:https://www.haomeiwen.com/subject/ebcmzttx.html