侧边栏壁纸
博主头像
王小木人

这是很长,很好的一生

  • 累计撰写 141 篇文章
  • 累计创建 43 个标签
  • 累计收到 7 条评论

目 录CONTENT

文章目录

mysql 命令

王小木人
2021-05-26 / 0 评论 / 0 点赞 / 1,217 阅读 / 4,584 字

存放数据库的好处
 1.安全
 2.搜索快,方便查找
 3.保证数据一致性,对数据有约束
数据库的类型
mysql
oracle
sqlserver
db2
access
启动数据库命令 net start mysql
关闭数据库命令 net stop mysql

连接数据库
本地连接
 mysql -u用户名 -p密码
远程连接
mysql -hip地址 -u用户名 -p密码

退出数据库连接

 1.exit 2,quit 3,/q

查看所有数据库 show databases
 information_schema 不可删
 mysql              不可删
 performance_schema 不可删

创建数据库  create database 数据库名称

删除数据库  drop database  数据库名称

选择数据库 use 数据库名称

创建数据表 create table 表名(
  列名 列类型,
  ...

查看该数据库的表 show tables 表名

删除数据库表 drop table 表名

查看表结构 desc 表名 或 explain 表
查看表的详细信息 show create table 表名
添加列 alter table 表名 add 列名 列类型

插入数据 insert into 表名(列名...) values (列值...)

         insert into 表名 values (值...) 

修改列 alter table 表名 change 旧列名 新列名 列的类型
       alter table 表名 modify 列名 列的类型        

修改表名 alter table 表名 rename 新表名
         rename table 旧表名 to 新表名
          
修改数据 update 表名 set 列名1=新值1 ,.. where 条件(为null时 is null或is not null)
 
删除数据 delete from 表名 where 条件

关闭事务自动提交(默认是打开的) set autocommit=0;
提交事务 commit
回滚     rollback;

条件查询
  查询特定的行 select * from 表名 where 条件
  查询列指定别名 select 列 as 列别名 from 表名 where 条件

聚合函数

求总数  count()
平均值 avg()
求和   sum()
最大值 max()
最小值 min()
合并列 concat(列1,s2...)
时间函数转换 查询比较时要转换后才能比较 str_to_date('时间字符串','%Y-%m-%d');

创建表自增列  create table 表名 (列名 int primary auto_increment)

分组查询 group by,having,order by

分组 group by 

条件筛选 having 

倒序排序 order by 列名  desc 
升序排序 order by  列名 asc
  select * from 表名 group by 列名 having 

模糊查询 like

 select * from 表名 where 列名 like ‘%值%’(‘’)

范围查询 select * from 表名 where 列名 between 起始大小 and 结束大小 

约束 

非空约束 not null

创建表时添加 create table 表名 (
                                列名 类型 not null,
                                ....
                               )

修改列非空约束 alter table 表名 change 旧列名 新列名 列类型 not null
              alter table 表名 modify 列名 列类型 not null

主键约束(唯一标示) pirmary key 
 
 创建表后添加 alter table 表名 add pirmary key(列名);
 创建表时添加 cteate table 表名 (
                          列名 int primary key,
                                 )

检查约束 check mysql不支持,提供枚举实现
         enum
创建表时添加 create table 表名(
    列名 enum(类型1,....),
 )
 
创建表后进行修改 alter table 表名 modify 列名  enum(类型,...)

唯一约束 unique

添加唯一约束 alter table 表名 add  unique(列名)

创建触发器 create trigger 触发器名字 出发时间(before或after) 触发事件(insert ,delete,select等) on 表名 for each row 程序体(sql语句或begin end)

外键约束
给一列增加外键 alter table 表名 add [外键名称] foreign key(外表列名) references 主表(列名);

创建表时添加外键约束 create table 表名 (列名 列类型, foreign key (外表列名) references 主表(列名))

删除键 alter table 表名 drop primary key|foreign key 外键名

查询主外键关系是否开启 select @@foreign_key_checks

关闭主外键关系 set @@foreign_key_checks=0

开启主外键关系 set @@foreign_key_checks=1

事务

三种状态 开启 提交 回滚

查询事务 select @@autocommit (1为自动提交,0为关闭自动提交)

关闭事务自动提交 set autocommit=0;

开启事务自动提交 set autocommit=1;

回滚 rollback

提交 commit

手动控制事务(autocommit为开启状态时) begin

                                    sql语句
                                      ...
                                      commit

修改引擎 alter table 表名 ENGINE=引擎名
mysql 数据库的存储数据引擎有几种,分别有几种
  
  两种
  InnoDB:支持主外键,事务
  MYISAM:不支持主外键,事务

删除主键 alter table 表名 drop primary key

删除外键 alter table 表名 drop foreign key 外键名

删除唯一约束 alter table 表名 drop index 列名

删除列 alter table 表名 drop column 列名

建表自增列 

创建表时设置    create table employee(
                     id int primary key auto_increment
                          )

截断表 truncate 表名

自增长设置  alter table 表名 auto_increment=值 

多表查询 select * from  表一,表二 where 表一.列=表二.列 

分页查询 select * from 表名 limit 索引下标,大小

触发器
 drop trigger if exists ageline;
delimiter $$ //修改结束字符
create  trigger ageline 
               before insert on student  for each row   
                
               begin   
                       if(new.age>120||new.age<0) then
                                          
                       delete from student where id=new.id;
                end if;
               end $$
delimiter ;

表连接  join

右连接 right join 
   select * from 表1 right join 表2 on 表1.列=表2.列 以右表为准,查询出右表全部数据 

左连接 left join
    select * from 表1 left join 表2 on 表1.列=表2.列 以左表为准,查询出左表全部数据 

内连接 inner join
    select * from 表1 inner join 表2 on 表1.列=表2.列 连接两张表,同时满足,等同where

子查询
  包含 in
  不包含 not in
exists 返回结果为真和假 ,为真时执行前面语句,为假时不执行
       select * from where exists (条件)

存储过程
 内置的聚合函数里面封装了sql脚本
 定义:用来封装sql 脚本的一种方式;预编译好的
 关键字 :procedure 
create procedure  过程名(形参 类型,...)
begin
end
创建后调用 call 过程名
查询存储过程 show procedure status like '过程名%'
查询存储过程详细 show create procedure 过程名
删除存储过程 drop procedure 过程名

数据库范式

 第一范式:列符合原子性,列不可拆分
 第二范式:表的原子性(一张表只做一件事), 每个列都与主键相关
 第三范式:每个列都与主键直接相关

查询表是否存在
select table_name from information_schema.TABLES where table_name='表明' and table_schema='数据库名';

0

评论区