mysql汇总

重拾MySQL

1. 下载zip5.7版本安装 docker容器安装已在博客中写过不再赘述
2. 主要是windows下配置安装
3. 解压后path环境变量目录到\bin
4. 在bin同级目录新建my.ini文件

[client]
port=3306
default-character-set=utf8]
[mysqld]
#设置为自己的mysql的安装目录
basedir=D:\pnMysql\mysql-5.7.19-win64\
#设置mysql data数据目录,系统自动创建解压后不存在
datadir=D:\pnMysql\mysql-5.7.19-win64\data\
port=3306
character set sevesr=utf8
#跳过安全检查
skip-grant-tables

5. 使用管理员身份打开cmd切换进入到D:\pnMysql\mysql-5.7.19-win64\bin执行

cd /D D:\pnMysql\mysql-5.7.19-win64\bin
mysqld -install

6. 初始化数据库 生成data

mysqld --initialize-insecure --uer=mysql

7. 启动mysql服务

net start mysql
#关闭服务
net stop mysql

8. 进入mysql空密码

mysql -u root -p

9. 修改root用户密码为pn

use mysql;
update user set authentication_string=password('pn')where user='root'and Host='localhost';

10. 刷新权限

flush privileges;

零散

# 商店收获系统表
#数据库 shop_db 商品表 goods(商品号 goods_id,商品名goods_name,单价unitprice 商品类别 category #供应商 provider);
#客户表 customer (客户名 #customer_id,姓名name,住址address,电邮email,性别sex,身份证card_id);
#购买订单 purchase (购买单号 order_id 客户号 customer_id 商品号 goods_id购买数量 nums)
#姓名不能为空 电邮不能重复 单价在 1.0-9999.99之间check
create database shop_db;
	create table goods(goods_id int primary key, goods_name varchar(32) not null default '',unitprice DECIMAL(10,2) 
		CHECK(unitprice>=1.0 AND unitprice<=9999.99),category INT NOT NULL default 0,

		provider varchar(64) NOT NULL DEFAULT '');
	create table  customer(customer_id int primariky,`name` varchar(22),adress varchar(128),
		email varchar(32) unique, sex ENUM('男','女') NOT NULL ,card_id char(18));
	create table purchase(order_id int primary key,customer_id char(8),goods_id int,nums int NOT NULL DEFAULT 0,
		foreign key (customer_id) references customer(customer_id),
		foreign key (goods_id) references goods (goods_id));
;
 primary key auto_increment. null
 create table t24(id INT PRIMARY KEY auto_increment,)
 	alter table t24 auto_increment=100   
 	#自增长要和主键或者unique一起使用
 	#创建索引,emp表 empeno列 创建empno_index索引
 	create index empno_index on emp(empeno)  
 	#1.主键索引(primary) 唯一索引(unique) 普通索引(index) 全文索引(fulltext)
 	#2.全文索引适用于myisam 开发中使用 全文搜索 solr 和elasticsearch
 	show indexes from xxx;
 	create  unique index id_index on emp(id);
 	alter table t25 add index id_index(id);
 	alter table add primary key(id);
 	drop index id_index on t25
 	#删除主键索引
 	alter table t26 drop primary key;
 	show index from t26
 	show keys from t25; 
 	desc t25;

 	#创建索引 不会出现在where子句中的字段不应该创建索引
create table order(
          id int primary key,customer_name varchar(36) not null,
          `name` varchar(36) nums int not null
          );
create table order2(
          id int unique not null,customer_name varchar(36) not null,
          `name` varchar(36) nums int not null
          );
create table menu (
         id int primary key,
         'name' varchar(36),
          customer varchar(36),
          name_id int unique,
          price double(64));

create table menu2 (
         id int ,
         'name' varchar(36),
          customer varchar(36),
          name_id int ,
          price double(64));
alter table menu2 add primary key(id);
alter table menu2 add unique(name_id);
#msql事务:事务用于保证数据的一致性,它由一组相关的dml 语句组成,要么全部成功要么全部失败
#基本事物操作
#1. start transaction 开始一个事务 2. savepoint 保存点名--设置保存点。3. rollback to 保存点名--回退事务 rollback 全部退回  commit 提交事务
create table t27(id int,
                `name` varchar(32));
                start transaction
                savepoint a 
                insert into t27 values(100,'kk');
                    savepoint b 
                    insert into t27 values(200.'ff')
                        rollback to b      
                        rollback to a 
                        commit
# 事务要求mysql innodb存储引擎
set autocommit=false#开启事务
#事务的隔离级别 脏读 不可重复读 幻读
#脏读 当一个事务读取另一个事务尚未提交的改变时,产生脏读
#不可重复读 同一查询在同一事务中多次进行,由于其他提交事务所做的修改或删除 每次返回不同的结果集,此时发生不可重复读
#幻读 同一查询在同一事务中多次进行 由于其他提交事务所做的插入操作,每次返回不同的结果集产生幻读
select @@tx_isolation#查看当前事务隔离级别
select @@global.tx_isolation#查看系统隔离级别
select session transaction  isolation level read uncommitted#设置当前隔离级别
select global transaction isolation level read uncommitted#设置系统隔离级别
#全局修改 在配置文件 my.ini下增加 transaction-isolation=
#隔离级别 4种
#读未提交 Read uncommitted 不加锁 脏读 不可重读   幻读
#读已提交 Read committed   不加锁      不可重复读 幻读
#可重复读 Repeatable read  不加锁
#可串行化 Serializable      加锁

#mysql 表类型和存储引擎
#mysql 支持6种类型 分为事务安全型和非事务安全型
#查看所有引擎
show engines
alter table xx engine=innodb;
#视图 虚拟表 内容由查询定义 视图表修改 基表也会修改
#视图的基本使用 视图中还可以创建视图
#1 create view 视图名 as select 语句
#2 alter view 视图名 as select 语句
#3 show  create view 视图名
# 4 drop view #视图名1,视图名2
desc shitubiao
#test
create view sanbiao
    as 
    select empeno,ename,dname,grade
    from emp,dept,salgrade
    where emp.deptno=dept.deptno AND 
    (sal between losal and hisal)
#mysql use表
create USER 'PN'@'localhost' identified by '123456'
    select password('123456')
    drop user 'PN'@'localhost'
    set password=password('abc')
    #修改其他用户密码
    set password for 'yonghuming'@'localhost'=password('123456')
    #权限赋予
    grant select,update on kuming.duixingming to 'PN'@'localhost' #[identify by '123456']
        grant all on *.* to 'PN'@'localhost' #root权限
            revoke select on ku.duixingming from 'PN'@'123456'#回收权限
            flush privileges #权限生效



























  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值