mysql (in)

mysql (in)

First, the database configuration

1、登录mysql
2.在mysql安装目录下:创建my.ini(my.cnf)
3.设置配置信息并保存
'''
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
[client]
default-character-set=utf8
'''
4.重启mysql服务
mysql> create database yjy;   #创建一个数据库

mysql> show create database yjy;  #查看数据库的详细信息

+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| yjy      | CREATE DATABASE `yjy` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
#数据库配置之后默认编码变为了utf8
# mysql 5.7 以后默认都是安全模式
# mysql 5.6 版本
sql_model=no_engine_substitution  # 非安全性,默认 可以写在my.ini中
sql_model=strict_trans_tables  # 安全性
1.查看当前数据库模式:
show variables like "%sql_mode%"; # %匹配0~n个任意字符 => 模糊查询

eg:show variables like "sql_mode";
+---------------+------------------------+
| Variable_name | Value                  |
+---------------+------------------------+
| sql_mode      | NO_ENGINE_SUBSTITUTION |
+---------------+------------------------+

2.临时设置为安全模式,服务重启后会被重置
mysql>: set global sql_mode="strict_trans_tables";  # 在root用户登录状态下
3.在设置后,quit断开数据库连接后(服务器不重启)就会进入安全模式

Second, modify database information

'''修改字符编码'''
mysql>:alter database 原数据库名 charset = 编码格式;
eg:alter database yjy charset = "utf8";

 #查看数据库的详细信息
show create database yjy;
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| yjy      | CREATE DATABASE `yjy` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
    
'''修改数据库名'''
mysql>:rename database 旧数据库名 to 新数据库名;  #尽量不要用
eg:rename database yjy to hhh;   

Special table (mysql.user) => User Management

'''
# 操作前提:登录root用户

1.重要字段
Host | User | Password

2.新建用户
create user 用户名@主机名 identified by '密码'; # 正确
create user zero@localhost identified by 'zero';

注:insert into mysql.user(Host,User,Password) values("主机名","用户名",password("密码")); # 错误

3.设置用户权限
grant 权限们 on 数据库名.表名 to 用户名@主机名 [with grant option];
grant create on db1.* to zero@localhost with grant option;
注:权限有select,delete,update,insert,drop..., all代表所有权限
注:数据库名,表名可以用*替换,代表所有
注:设置权限时如果没有当前用户,会自动创建用户,提倡使用
重点: grant all on db1.* to owen@localhost identified by 'owen'; # (创建用户)设置权限

4.撤销权限
revoke 权限名 on 数据库名.表名 from 用户名@主机名;
revoke delete on db1.* from owen@localhost;

5.修改密码
set password for 用户名@主机名 = password('新密码');
set password for owen@localhost = password('123');

6.删除用户
drop user 用户名@主机名;
'''

Modify three tables

'''修改表名'''
mysql>:alter table 旧表名 rename 新表名
eg:alter table t1 rename t2
'''修改字段'''
mysql>:alter table 表名 change 旧字段名 新字段名 char(18)
eg:alter table t2 change name hhhh char(18)
'''修改字段属性'''
mysql>:alter table 表名 modify 字段名 属性
eg:alter table t2 modify hhhh char(20)
#修改表名
mysql> alter table t1 rename t2;
mysql> show tables;
+---------------+
| Tables_in_yjy |
+---------------+
| t2            |
| user          |
+---------------+

#修改字段
mysql> alter table t2 change name hhhh char(18);
mysql> desc t2;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| hhhh  | char(18) | YES  |     | NULL    |       |
| age   | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+

#修改字段属性
mysql> alter table t2 modify hhhh char(20);
mysql> desc t2;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| hhhh  | char(20) | YES  |     | NULL    |       |
| age   | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+

change can either modify field names, and field properties can be modified, but can only be modified modify field properties

Fourth, create a complete syntax table

'''
create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],
字段名3 类型[(宽度) 约束条件]   
)engine= innodb charset = utf8;  #engine为引擎,驱动数据的方式,主要用于数据库优化
'''
#[]为可选参数
eg:create table t1(name char(4) not null);
#创建一个名为t1的表,数据插入时,name不能为空(null),且最长只能存放四个字符 

Fifth, the database engine tables

Data driven - primarily for data optimization

Can be optimized by modifying the database engine database

Note: The engine is specified construction of the table, the table provides for the use of, not the database

innobd Data can be stored, safe
myisam You can store data, find data more efficient than innobd
blackhole What saved nothing, nothing deposit does not go in for unnecessary information taken from the website of the climb, the database can be thrown into the blackhole
memory To save memory, i.e. close disappears, the table structure on the hard disk, but the number of all the table data stored in memory
mysql>: create table t1(id int)engine=innodb;
# 创建一个引擎为innodb的表t1,有id int类型字段    
mysql>:create table t2(x int,y int)engine=myisam;
# 创建一个引擎为myisam的表t2,有x,y两个int类型字段
mysql>: create table t3(id int)engine=blackhole;
# 创建一个引擎为blackhole的表t3,有id int类型字段
mysql>: create table t14(id int)engine=memory;
# 创建一个引擎为memory的表t4,有id int类型字段

Six, MYSQL supported data types

6.1 Integer

1, the data type

  • tinyint : -128 to 127. 1 byte (can be stored up to 4 digits)

  • smallint The : 2 bytes (up to six digits can be stored)

  • MEDIUMINT : bytes. 3

  • int : ~. 4 bytes -2147483648 2147483647 (can save up to 11 digits)

    Manually predetermined range will not affect the length of the int, int it is generally not a predetermined length

  • BIGINT: . 8 bytes (byte can be stored in a long data)

# 安全模式下,非安全模式下sql执行的警告语句,都会抛异常
eg>: create table t1(name char(2));  #创建表t1,设置属性字段name,数据类型为char(2)
eg>: insert into t1 values ("ab") # 正常
eg>: insert into t1 values ("yjy") # 错误 Data too long for column 'name' at row 1
    

mysql>: create table tb1(x tinyint, y smallint, z int(6));  #创建表tb1,设置属性字段x,数据类型为tinyint,设置属性字段y,数据类型为smallint,设置属性字段z,数据类型为int
mysql>: insert into tb1 values(128, 32768, 32768);  # 结果:127,32767,32768  #如果超出最大范围就会存入最大的值

2, constraint

  • unsigned : Unsigned (range that does not have a negative sign, can only be integers, such that the range of -128 to 127 tinyint, restraining him if he changes to a range of 0-255)
  • ZEROFILL : 0 padding (if the character length is 3, if the input 1, when the view data 001, if the input 10, when the view data 010, but is shown as such, does not change the value of the database)
# 整型约束
mysql>: create table tb2(x tinyint unsigned);  # 0~255
mysql>: insert into tb2 values(256), (-1);    # 255, 0
    
# 0填充约束
mysql>: create table tb3(x tinyint unsigned zerofill);
mysql>: insert into tb3 values(10);  # 010

6.2 Float

Three floating-point type, the width of the storage limit, an integer of 255 bits, 30 bits decimal

  • a float (255, 30) :. 4 bytes, the minimum accuracy, the most common
  • Double (255, 30) :. 8 bytes, high precision, multi placeholder
  • decimal (65, 30) : String deposit, full precision
'''
float(M, D):4字节,3.4E–38~3.4E+38
double(M, D):8字节,1.7E–308~1.7E+308
decimal(M, D):所在字节M,D大值基础上+2,其实就是M值+2就是decimal字段所占字节数
'''
# 建表:
mysql>: create table tb4 (age float(256, 30)); # Display width out of range for column 'age' (max = 255)
mysql>: create table tb5 (age float(255, 31)); # Too big scale 31 specified for column 'age'. Maximum is 30.
mysql>: create table tb5 (age float(65, 30)); # 在合理取值范围
    
mysql>: create table t12 (x float(255, 30));
mysql>: create table t13 (x double(255, 30));
mysql>: create table t14 (x decimal(65, 30));
# 1.111111164093017600000000000000 
mysql>: insert into t12 values(1.11111111111111111119);  
# 1.111111111111111200000000000000
mysql>: insert into t13 values(1.11111111111111111119);
# 1.111111111111111111190000000000
mysql>: insert into t14 values(1.11111111111111111119);

# 重点:长度与小数位分析
# 报错,总长度M必须大于等于小数位D
mysql>: create table t14 (x decimal(2, 3));

# 能存储 -0.999 ~ 0.999,超长度的小数位会才有四舍五入,0.9994可以存,就是0.999,0.9995不可以存
mysql>: create table t14 (x decimal(3, 3));  # 整数位 3 - 3,所以最大为0

# 能存储 -9.999 ~ 9.999,超长度的小数位会才有四舍五入,9.9994可以存,就是9.999,9.9995不可以存
mysql>: create table t14 (x decimal(4, 3));  # 整数位 4 - 3,所以最大为9

# 能存储 -99.999 ~ 99.999,超长度的小数位会才有四舍五入,99.9994可以存,就是99.999,99.9995不可以存
mysql>: create table t14 (x decimal(5, 3));  # 整数位 5 - 3,所以最大为99

6.3 Character

  • char : fixed-length
  • varchar: variable length
'''
#限制存储宽度
-char(4):规定字符串的长度为4就是4,存长度为1的字符串也是4,存长度为4的字符串也是4
-varchar(4):数据长度决定字符长度,为可变长度存储数据,存长度为1的字符串他就为1
'''
mysql>: create table ts1 (s1 char(4), s2 varchar(4));
mysql>: insert into ts1 values('adcde', 'xyzabc');  # 'adcd', 'xyza'

6.4 Time Type

  • year:yyyy(1901-2155)
  • date:yyyy-MM-dd (1000-01-01~~9999-12-31 )
  • datetime:yyyy-MM-dd HH:mm:ss(1000-01-01 00:00:00~~9999-12-31 23:59:59)
  • timestamp: yyyy-MM-dd HH: mm: SS (1970-01-01 00: 00: 00 ~ ~ 2038-01-19 ??) (this time only to 2038-01-19)
mysql>: create table td1 (my_year year, my_date date, my_time time);
mysql>: insert into td1 values(1666, '8888-8-8', '8:8:8');  # 时间需要在取值访问内

    
mysql>: create table td2 (my_datetime datetime, my_timestamp timestamp);   
mysql>: insert into td2 values('2040-1-1 1:1:1', '2040-1-1 1:1:1');  # 时间需要在取值访问内   
mysql>: insert into td2(my_datetime) values('2040-1-1 1:1:1'); # timestamp不赋值会才有系统当前时间
+---------------------+---------------------+
| my_datetime         | my_timestamp        |
+---------------------+---------------------+
| 2040-01-01 01:01:01 | 2019-09-23 22:01:50 |
+---------------------+---------------------+ 


# datetime:8字节,可以为null
# timestamp:4字节,有默认值CURRENT_TIMESTAMP

6.5 enumeration and collection

  • enum: radio (only choose one, such as gender)
  • set: multiple choice (you can choose more than, for example, hobby)
# enum、set默认值为NULL
mysql>: create table tc1 (name varchar(20), sex enum('男', '女'), hobbies set('男', '女', '哈哈'));
mysql>: insert into tc1 values('wwb', '哈哈哈', '未知');  
#输入的值是错误的,所以错误的值就是空的
+------+------+---------+
| name | sex  | hobbies |
+------+------+---------+
| wwb  |      |         |
+------+------+---------+


# enum、set手动设置默认值 '男' 与 '哇塞'
mysql>: create table tc12 (name varchar(20), sex enum('男', '女') default '男', hobbies set('男', '女', '哈哈'));
    
mysql>: insert into tc12 values('wwb', '哇塞哇塞', '男'); 
# 对sex字段赋值错误,系统默认用空字符串填充(非安全模式),安全模式抛异常
+------+------+---------+
| name | sex  | hobbies |
+------+------+---------+
| wwb  |      | 男      |
+------+------+---------+

mysql>: insert into tc12(name) values('wwb');    
# 如果对除了sex字段外的其他字段进行赋值,这个字段会才有默认值
+------+------+---------+
| name | sex  | hobbies |
+------+------+---------+
| wwb  |      | 男      |
| wwb  | 男   | NULL    |
+------+------+---------+

# 注:对set类型的字段进行赋值,用一个字符串,字符串内部用,将多个选项隔开,且不能添加空格等其他额外字符
mysql>: insert into tc2 values('wwb', '男', '男,女,哈哈'); 

Seven constraints

"""
primary key:主键,唯一标识,表都会拥有,不设置为默认找第一个 不空,唯一 字段,未标识则创建隐藏字段
foreign key:外键
unique key:唯一性数据, 该条字段的值需要保证唯一,不能重复

auto_increment:自增,只能加给key字段辅助修饰

not null:不为空
default:默认值

unsigned:无符号
zerofill:0填充
"""
'''创建一个td1表,x为整型不为空,y为整型默认为0,z为整型,默认为100'''
mysql>: create table td1 (x int not null, y int default 0, z int default 100);
# 不能为空即必须赋值
# y、z在没有赋值的情况下,才有默认值,设置值后,采用默认值

mysql>: create table td2 (x int auto_increment);  
# 报错,auto_increment必须设置主键字段
mysql>: create table td2 (x char(4) auto_increment);  
# 报错,auto_increment必须设置给 int字段
mysql>: create table td2 (x int unique auto_increment, y int unique auto_increment);
# 报错,auto_increment字段最多出现 1次
# 正确,主键和唯一键分析
mysql>: create table td21 (x int unique auto_increment, y int unique);
# x为主键:没有设置primary key时,第一个 唯一自增键,会自动提升为主键    
   
mysql>: create table td22 (x int unique, y int unique auto_increment);
# y为主键:没有设置primary key时,第一个 唯一自增键,会自动提升为主键    
    
mysql>: create table td23 (x int primary key, y int unique auto_increment);
# x为主键:设置了主键就是设置的,主键没设置自增,那自增是可以设置在唯一键上的    
    
mysql>: create table td24 (x int primary key auto_increment, y int unique);
# x为主键:设置了主键就是设置的,主键设置了自增,自增字段只能有一个,所以唯一键不能再设置自增了        

mysql>: create table td25 (x int unique, y int unique);
# 默认主键:没有设置主键,也没有 唯一自增键,那系统会默认添加一个 隐式主键(不可见)

'''
# 唯一键:确保一个字段,数据不能重复
# 主键:是一条记录的唯一标识(可以理解为数据的编号)
'''
注:
1.键是用来讲的io提供存取效率
2.联合唯一
create table web (
    ip char(16),
    port int,
    unique(ip,port)
);
3.联合主键
create table web (
    ip char(16),
    port int,
    primary key(ip,port)
);

# eg:1
# 单列唯一
create table t20 (
    id int unique
);
# 联合唯一
create table web (
    ip char(16),
    port int,
    unique(ip,port)
);
# 如果联合两个字段,两个字段全相同才相同,否则为不同
insert into web values ('10.10.10.10', 3306), ('10.10.10.10', 3306);

# 注: 
# 1.表默认都有主键, 且只能拥有一个主键字段(单列主键 | 联合主键)
# 2.没有设置主键的表, 数据库系统会自上而下将第一个规定为unique not null字段自动提升为primary key主键
# 3.如果整个表都没有unique not null字段且没有primary key字段, 系统会默认创建一个隐藏字段作为主键
# 4.通常必须手动指定表的主键, 一般用id字段, 且id字段一般类型为int, 因为int类型可以auto_increment

# eg:2
create table t21(id int auto_increment); # 自增约束必须添加给key的字段
# eg:3
create table t21(id int primary key auto_increment); # 自增要结合key,不赋值插入,数据会自动自增, 且自增的结果一直被记录保留
# eg:4
# 联合主键
create table t22(
    ip char(16),
    port int,
    primary key(ip,port)
);
# 如果联合两个字段,两个字段全相同才相同,否则为不同
insert into web values ('10.10.10.10', 3306), ('10.10.10.10', 3306);

Guess you like

Origin www.cnblogs.com/yanjiayi098-001/p/11575373.html