Day054--MySQL, 创建用户和授权, 数据类型, 枚举和集合, 约束,唯一, 主键,外键

创建用户和授权

1.创建用户:
# 指定ip:192.118.1.1的mjj用户登录
create user 'mjj'@'192.118.1.1' identified by '123';
# 指定ip:192.118.1.开头的mjj用户登录
create user 'mjj'@'192.118.1.%' identified by '123';
# 指定任何ip的mjj用户登录
create user 'mjj'@'%' identified by '123';

2.删除用户
drop user '用户名'@'IP地址';

3.修改用户
rename user '用户名'@'IP地址' to '新用户名'@'IP地址';

4.修改密码
set password for '用户名'@'IP地址'=Password('新密码');

进入mysql库, use mysql; 输入
select * from user\G;
查看所有用户

对当前的用户授权管理
查看权限
show grants for '用户'@'IP地址';

授权 mjj用户仅对db1.t1文件有查询、插入和更新的操作

grant select ,insert,update on db1.t1 to "mjj"@'%';

表示有所有的权限,除了grant这个命令,这个命令是root才有的。mjj用户对db1下的t1文件有任意操作

grant all privileges on db1.t1 to "mjj"@'%';
#mjj用户对db1数据库中的文件执行任何操作
grant all privileges on db1.* to "mjj"@'%';
#mjj用户对所有数据库中文件有任何操作
grant all privileges on . to "mjj"@'%';

远程连接:
mysql -uskx -P3306 -h 192.168.15.113 -p123

复制表

即复制表结构 又复制记录

create table t2 select * from db1.t1;

只复制表结构,不复制记录

create table t2 select * from db1.t1 where 1>3; #只要后面条件不成立,表示只复制表结构
create table t2 like db1.t1;

数据类型

整型 默认是有符号

数据类型 无符号(unsigned)和有符号 用0填充 zerofill

约束的作用: 保证数据的完整性和一致性

  • tinyint [-128~127] 小整数

  • int 整数

  • bigint 极大整数

create table t1(id int(4) unsigned,name char(20));


浮点型

  • float 单精度 随着小数位数的增多,不准确
  • double 双精度 随着小数位数的增多.不准确,比float要准确
  • decimal 小数 精准的小数

日期类型

year 年份 (1901~2155)

date 年月日

time 时分秒

datetime 年月日 时分秒 ***

now() sql语言中自带的内容函: 获取当前的时间(根据数据类型)

create table t10(born_year year,intClass datetime);

语法:
YEAR
YYYY(1901/2155)

DATE
YYYY-MM-DD(1000-01-01/9999-12-31)

TIME
HH:MM:SS('-838:59:59'/'838:59:59')

DATETIME

YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y)

TIMESTAMP

YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某时)

字符类型

  • char 定长,简单粗暴,浪费空间,存取速度快

  • varchar 变长,精准,节省空间,存取速度慢

  • text 长文本

    length():查看字节数
    char_length():查看字符数

枚举和集合

create table consumer(
id int,
name varchar(50),
sex enum('male','female','other') default 'male',
level enum('vip1','vip2','vip3','vip4'),#在指定范围内,多选一
fav set('play','music','read','study') #在指定范围内,多选多
);

注意:在sql中使用tinyint(1)来表示boolean类型

完整性约束

作用: 保证数据的完整性和一致性

not null 与 default

  • 如果单独设置not null 不能插入空值
  • 如果即设置了not null,又指定default,可以插入空值,会走default

unique 唯一

单列唯一

create table t4(
id int not null,
name char(20) unique
);

create table t4(
id int not null,
name char(20),
unique(name)
);
insert into t4(id,name) values(1,'alex');
insert into t4(id,name) values(1,'wusir');

多列唯一

  • 只要有一列相同,不能插入

    create table t5(
    id int,
    name char(20),
    unique(id),
    unique(name)
    );

联合唯一 ***

  • 多列相同时,不能插入

    create table t6(
    id int,
    name char(20),
    unique(id,name)
    );

应用场景: 选课系统,一个学生可以选择多个课程,一个课程可以被多个学生选择,

student_id course_name

100 '生物'

101 '生物'

100 '化学

primary key

MySQL为什么需要一个主键?

https://blog.csdn.net/shellching/article/details/77367557

化学反应: not null + unique

Mysql版本一个表中只能有一个主键,可以有联合主键,但不能有多列主键

单列主键 不能为空 并且是唯一

primary key 索引(针对于大量数据) 查询速度要快

create table t7(
id int primary key,
name varchar(10) unique
);

create table t8(
id int not null unique,
name varchar(10) unique
);

复合主键

create table t9(
id int,  
name varchar(10),
primary key(id,name)
);

auto_increment 自增量,升序排列,初始值为1,步长为1

create table student(
id int primary key auto_increment,
name varchar(20) not null,
sex enum('male','female') default 'male',
ip varchar(20) unique
);

insert into student(name,sex,ip) values ('alex','female','127.0.0.5'),('wusir','male','173.45.32.1');

auto_increment_increment 设置自增值, 也就是步长

auto_increment_offset 设置偏移值,也就是起始量

查看可用的 开头auto_inc的词
mysql> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
rows in set (0.02 sec)
# 步长auto_increment_increment,默认为1
# 起始的偏移量auto_increment_offset, 默认是1

设置步长 为会话设置,只在本次连接中有效

set session auto_increment_increment=5;

全局设置步长 都有效。

set global auto_increment_increment=5;

设置起始偏移量

set global auto_increment_offset=3;

强调:If the value of auto_increment_offset is greater than that of auto_increment_increment, the value of auto_increment_offset is ignored.

翻译:如果auto_increment_offset的值大于auto_increment_increment的值,则auto_increment_offset的值会被忽略

设置完起始偏移量和步长之后,再次执行show variables like'auto_inc%';

发现跟之前一样,必须先exit,再登录才有效。

mysql> show variables like'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 5 |
| auto_increment_offset | 3 |
+--------------------------+-------+
rows in set (0.00 sec)

因为之前有一条记录id=1

mysql> select * from student;
+----+---------+------+
| id | name | sex |
+----+---------+------+
| 1 | xiaobai | male |
+----+---------+------+
row in set (0.00 sec)
# 下次插入的时候,从起始位置3开始,每次插入记录id+5
mysql> insert into student(name) values('ma1'),('ma2'),('ma3');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from student;
+----+---------+------+
| id | name | sex |
+----+---------+------+
| 1 | xiaobai | male |
| 3 | ma1 | male |
| 8 | ma2 | male |
| 13 | ma3 | male |
+----+---------+------+

auto_increment_increment和 auto_increment_offset

清空表区分delete和truncate的区别:

delete from t1; #如果有自增id,新增的数据,仍然是以删除前的最后一样作为起始。

truncate table t1;数据量大,删除速度比上一条快,且直接从零开始。

foreign key ***

外键

先创建主表

create table dep(
id int primary key auto_increment,
name char(10) unique,
dep_desc varchar(50) not null
);
# 校区表
create table school(
id int primary key auto_increment,
addr varchar not null
)

创建从表

create table emp(
eid int primary key auto_increment,
name char(10) not null,
age int not null,
dep_id int,
school_id int,
constraint fk_dep foreign key(dep_id) references dep(id)
on delete cascade
on update cascade,
constraint fk_school foreign key(school_id) references school(id)
on delete cascade
on update cascade
);

insert into dep(name,dep_desc) values('校长部','校长管理有限部门'),('公关部','公关管理有限部门'),('IT部门','IT技术有限部门'),('财务部','管钱很多部门');
insert into emp(name,age,dep_id)
values
('alex',18,1),
('wusir',30,2),
('吴老板',20,3),
('马老板',18,4),
('邱老板',20,2),
('女神',16,3);

posted @ 2018-11-22 17:09  SuraSun  阅读(191)  评论(0)    收藏  举报