On the database 1

Database Configuration

By purpose profiles unified configuration: the unified management server (mysqld), client (client)
configured mysqld (server) is encoded as utf8, then re-create the database, the default encoding are used utf8

Configuration procedure
1) in the mysql installation root directory, create a configuration file: my.ini
configuration file called my.cnf under mac

2) set up a profile and save it

[mysqld]  # 服务器配置
port=3306  # 可以修改数据库默认端口(如果数据库端口被其他软件占用)
character-set-server=utf8  # 编码格式
collation-server=utf8_general_ci  # 排序方式(默认跟编码格式走)

[client]  # mysql自己的客户端叫[mysql],配置[client]即配置了[mysql],也配置了其他存在方式的客户端,比如Navicat可视化客户端
default-character-set=utf8  # 编码格式

3) Restart the database service

Modify database information

Modify Character Encoding

mysql>: alter database 数据库名 charset=编码格式;

User Action

You have the operation right database for a specific user allocation database

mysql>: grant 权限们 on 数据库.表 to 用户名@'主机名' identified by '密码';

1) all: All permissions
2) oldboy *:. Oldboy at all database tables
3) oldboy @ 'localhost': The machine user can log in by Oldboy
4) identified by 'Oldboy123': password Oldboy123

eg>: grant all on oldboy.* to oldboy@'localhost' identified by 'Oldboy123';

1) select, delete, update, insert, drop, alter: the specified permission
. 2) oldboy *: All the following table Oldboy database
3) oldboy @ '%': Any machine can sign by Oldboy user
4) identified by 'Oldboy123' : password Oldboy123

eg>: grant select,delete,update,insert,drop,alter on oldboy.* to oldboy@'%' identified by 'Oldboy123';

#撤销权限

mysql>: revoke 权限1,权限2,... on 数据库名.表名 from 用户名@'主机名';
#禁掉本地oldboy用户对oldboy数据库的所有表的drop权限
eg:> revoke drop on oldboy.* from oldboy@'localhost';

delete users

drop user 用户名@'主机名';

Modify table

Modify the table name

mysql>: alter table 旧表 rename 新表;

Modify field name

mysql>: alter table 表名 change 旧字段 新字段 类型(长度);

Modify field properties

mysql>: alter table 表名 modify 字段 新类型(长度);

Create a complete syntax table

Constraint length and in some cases may be omitted

mysql>: create table 表名 (
    属性名1 类型(长度) 约束,
    ...
    属性名n 类型(长度) 约束
) engine=引擎 default charset=utf8;

Engine database table: driven data - database optimization

Premise: Engine is built form is prescribed, provided to the table to use, not the database

Display all engines

mysql> show engines; 

innodb (default): Supports transactions, row-level locking, foreign keys

mysql>: create table t11(id int)engine=innodb;

myisam: query efficiency superior to innodb, when not required to support transactions, row-level locking, foreign keys, you can optimize your database by setting myisam

mysql>: create table t12(id int)engine=myisam;

blackhole: black hole, put into your account data will be gone (understandable data does not exist)

mysql>: create table t13(id int)engine=blackhole;

memory: the table structure, the table data are all stored on the hard disk is stored in memory

mysql>: create table t14(id int)engine=memory;

Database schema

mysql is the default security mode after 5.7

mysql 5.6 version

sql_model=no_engine_substitution  # 非安全性,默认
sql_model=strict_trans_tables  # 安全性

View current database schema:

show variables like "%sql_mode%"; # %匹配0~n个任意字符 => 模糊查询

Temporary set to secure mode, will be reset after the service is restarted

mysql>: set global sql_mode="strict_trans_tables";  # 在root用户登录状态下

After setting, quit disconnect from the database after (without restarting the server) goes into safe mode

Safe Mode, warning sql statement executed non-secure mode, will throw an exception

eg>: create table t1(name char(2));
eg>: insert into t1 values ("ab") # 正常
eg>: insert into t1 values ("owen") # 错误 Data too long for column 'name' at row 1

mysql data types supported

Integer

Types of

tinyint: 1 byte, in the range -128 to 127, the default length. 4
smallint The: 2 bytes, the range from -32768 to 32767, a default length. 6
MEDIUMINT:. 3 byte
int: 4 ~ byte 2147483647 -2147483648
BIGINT: 8 bytes

constraint

unsigned: Unsigned
zerofill: 0 filled

To build the table

mysql>: create table tb1(x tinyint, y smallint, z int(6));

# 插入数据
mysql>: insert into tb1 values(128, 32768, 32768);  # 结果:127,32767,32768

Conclusion: The size of the integer determined by the occupied bytes (the range), the length can be customized, but does not affect the occupied bytes (range)
length of all integer variables are generally omitted write

结论>: create table tb1(x tinyint, y smallint, z int);

Integer Constraints

mysql>: create table tb2(x tinyint unsigned);  # 0~255
mysql>: insert into tb2 values(256), (-1);    # 255, 0

0 padding constraints

mysql>: create table tb3(x tinyint unsigned zerofill);
mysql>: insert into tb3 values(10);  # 010

Float

Test floating-point type in Safe Mode

Type
float (M, D): 4 bytes, 3.4E-38 is ~ 3.4E + 38 is
Double (M, D):. 8 bytes, 1.7E-308 1.7E + ~ 308
decimal (M, D): where the word section M, +2, in fact, is the decimal value of M + 2 bytes occupied field on the basis of a large value D
''
width
limit memory width
(M, D) => M is the number of bits, D is a decimal, M greater than or equal to D
a float (255, 30): the lowest accuracy, the most commonly used
double (255, 30): high precision, occupying multiple
decimal (65, 30): string deposit, full-precision
'' '

Built table:

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

String: Database optimization - char efficiency is higher than varchar

Type
char: length, always using the stored data length set
varchar: variable length, provided within the length of the variable length data storage

Width
limit memory width
char (4): keep "a" "ab" "abc " "abcd" length of 4 are used, "abcde" only (given safe mode) before storing four
varchar (4): deposit " a "" ab "" abc " " abcd " stored length 1,2,3,4 respectively," abcde "only (given safe mode) before storage 4

char to length to store, if the data length changes, usually more space, but the data access operation at a fixed predetermined length, high efficiency
, varchar storing data, first calculates the length of the data to be stored, the data stored in the dynamic variable length, so generally more space-saving, but the calculation is the need for time-consuming, so inefficient

varchar calculated data length information is needed to open up space to store, in the data header (before the start of data), it is also necessary to store additional consumption of 1 to 2 bytes
so if the data is a fixed length, or a small range of fluctuation, char compared would not occupy more space, and high efficiency
'' '

To build the table

mysql>: create table ts1 (s1 char(4), s2 varchar(4));
mysql>: insert into ts1 values('adcde', 'xyzabc');  # 'adcd', 'xyza'

time

Types of

year:yyyy(1901/2155)
date:yyyy-MM-dd(1000-01-01/9999-12-31)
time:HH:mm:ss
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 ??)

To build the table

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不复制会才有系统当前时间

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

Enumeration and collection

Enumerate the set of: providing an option for a field - enumeration only radio (1), a set of multiple choice (0-n th)

Built table
enum, set the default value is NULL

mysql>: create table tc1 (name varchar(20), sex enum('男', '女', '哇塞'), hobbies set('男', '女', '哇塞'));
mysql>: insert into tc1 values('ruakei', '哇塞哇塞', '未知');  

enum, set manually set the default value of 'M' and 'wow'

mysql>: create table tc2 (name varchar(20), sex enum('男', '女', '哇塞') default '男', hobbies set('男', '女', '哇塞') default '哇塞');
mysql>: insert into tc2 values('ruakei', '哇塞哇塞', '未知');  
mysql>: insert into tc2(name) values('ruakei');

For sex, hobbies two field assignment error, the system defaults by filling an empty string (non-secure mode), safe mode throw an exception
if a sex other fields outside of hobbies two field assignment, which will have two fields Defaults

Note: to set the type of field assignment, with a string inside the string with, separating multiple options, and can not add any additional character spaces, etc.

mysql>: insert into tc2 values('ruakei_1', '女', '男,女,哇塞');  

constraint

primary key: primary key, unique identifier, will have a table, not set as the default look for first is not empty, the only field that does not identify you create a hidden field
foreign key: foreign key
unique: unique data, the need to ensure that the value of the article field The only, can not be repeated

auto_increment: increment, int type field can only be applied to the key, as a secondary modification, only one table is provided a self-energizing field

not null: not empty - for some fields, such as the user name registration, birth sex of the person, etc., in these fields demand, not only set to Null, it is necessary to assign
default: Default - to have default values unexpected field assignment, there is a default field values are assigned default values

unsigned: Unsigned - from the stored digital zero
zerofill: 0 filled - integer stored data length is less than the length of the range will be left filled with zeros in a digital

default limit not null and
not empty, no default value x, must be assigned
the y, z in the case where no value, only the default settings, the default values

mysql>: create table td1 (x int not null, y int default 0, z int default 100);



# 报错,auto_increment必须设置给 键字段
mysql>: create table td2 (x int auto_increment);
# 报错,auto_increment必须设置给 int字段
mysql>: create table td2 (x char(4) auto_increment);
# 报错,auto_increment字段最多出现 1次
mysql>: create table td2 (x int unique auto_increment, y int unique auto_increment);

# 正确,主键和唯一键分析
# x为主键:没有设置primary key时,第一个 唯一自增键,会自动提升为主键
mysql>: create table td21 (x int unique auto_increment, y int unique);
# y为主键:没有设置primary key时,第一个 唯一自增键,会自动提升为主键
mysql>: create table td22 (x int unique, y int unique auto_increment);
# x为主键:设置了主键就是设置的,主键没设置自增,那自增是可以设置在唯一键上的
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);
# 默认主键:没有设置主键,也没有 唯一自增键,那系统会默认添加一个 隐式主键(不可见)
mysql>: create table td25 (x int unique, y int unique);

# 唯一键:确保一个字段,数据不能重复
# 主键:是一条记录的唯一标识(可以理解为数据的编号)



# 联合唯一
# ip在port不同时,可以相同,ip不同时port也可以相同,均合法
# ip和port都相同时,就是重复数据,不合法
mysql>: create table tu1 (ip char(16), port int, unique(ip, port));

# 也可以设置成 联合主键,道理同 联合唯一
mysql>: create table tu2 (ip char(16), port int, primary key(ip, port));
# sql可以多行书写
mysql>: 
create table t22(
    ip char(16),
    port int,
    primary key(ip,port)
);


# 通常自增字段的 自增索引 会被永久记录,想清空表并清空自增索引:
mysql>: truncate 数据库名.表名

Guess you like

Origin www.cnblogs.com/MrYang161/p/11575231.html