可以用:\s 来查看数据库基本信息,里面含有用户、字符编码等信息。
Server characterset: latin1 Db characterset: latin1 Client characterset: gbk Conn. characterset: gbk
由于5.6版本编码不统一 ,会造成乱码,我们需要统一修改成utf8编码格式。
方法: 1.找到windows下的MySQL默认的配置文件:my-default.ini,然后拷贝上述文件并且从命名为my.ini 2.添加字符编码相关的配置(百度查看即可) 结果: Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 ps: 1.如果配置文件涉及到了mysqld相关的配置修改,那么需要重启服务端才可以生效 2.在配置文件中的mysql下提前写好用户名和密码,之后直接可以输入mysql就登陆(在mysql下写user = 'root' password = '密码')
存储引擎我么可以简单的理解为是针对相同的数据采用不同的存取策略
我们可以用:show engines; 来查看所有的存储引擎
mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
MyISAM是5.5及之前版本默认的存储引擎,它存取数据的速度快,但是功能较少,安全性较低
InnoDB 是5.5之后版本默认的存储引擎,存取数据的熟读没有myisam快,但是支持事物、行锁、外键等诸多功能
MEMORY是基于内存存储引擎,存取数据极快,但是断电之后立刻丢失
BLACKHOLE是黑洞的意思,即在任何写进去的数据都会立刻丢失,类似于垃圾站
建立存储引擎不同的4个表 (mysql中大小写是不敏感的,可忽略大小写) create table t1(id int) engine= innodb; create table t2(id int) engine= myisam; create table t3(id int) engine= memory; create table t4(id int) engine= blackhole; ps:在windows的cmd终端中,鼠标右键就是粘贴
mysql> select * from t4; Empty set (0.00 sec) C:\WINDOWS\system32>net stop mysql C:\WINDOWS\system32>net stop mysql C:\WINDOWS\system32>mysql -uroot -p Enter password: *** mysql> use d3; Database changed mysql> select * from t3; Empty set (0.00 sec)
查看创建的文件,发现:
1.在 InnoDB 存取引擎中: .frm 表结构 .ibd 表数据、表索引(加快数据查询) 2.在 MyISAM 存取引擎中: .frm 表结构 .MYD 表数据 .MYI 表索引(加快数据查询) 3. 在 MEMORY 存取引擎中: .frm 表结构 4.在 BLACKHOLE 存取引擎中: .frm 表结构
创建表的完整语法: create table 表名( 字段名1 字段类型(数字) 约束条件, 字段名2 字段类型(数字) 约束条件, 字段名3 字段类型(数字) 约束条件, .... 字段名n 字段类型(数字) 约束条件 )engine = 存储引擎; 1.字段名和字段类型是必须的(至少写一个) 2.数字跟约束条件是可选的(可有可无) 3.约束条件可以写多个 空格隔开即可 4.最后一个字段的结尾千万不能加逗号
常见的整型有tinyint(1bytes 2^8)、smallint(2bytes 2^16)、int(4bytes 2^32)、bigint(8bytes 2^64)
mysql> create table t5(id tinyint); Query OK, 0 rows affected (0.07 sec) mysql> insert into t5 values(-129),(128); Query OK, 2 rows affected, 2 warnings (0.00 sec) Records: 2 Duplicates: 0 Warnings: 2 mysql> select * from t5; +------+ | id | +------+ | -128 | | 127 | +------+ 2 rows in set (0.00 sec) ps: 1.当插入的数据超出范围时,数据失真,没有实际意义 2.所有的整型类型默认都会带有负号
添加约束条件:unsigned 意思是不需要负号
mysql> create table t6(id tinyint unsigned); Query OK, 0 rows affected (0.04 sec) mysql> insert into t6 values(-129),(128),(-1),(256); Query OK, 4 rows affected, 3 warnings (0.01 sec) Records: 4 Duplicates: 0 Warnings: 3 mysql> select * from t6; +------+ | id | +------+ | 0 | | 128 | | 0 | | 255 | +------+
插入的数据值超出了数据类型的范围,不应该让其插入并自动修改,没有意义,数据库应该直接报错(这个特性其实时有的,只是被我们改了(配置文件)),下面有两种方式:
方式一:命令临时修改
set session sql_mode='strict_trans_tables' 当前客户端操作界面有效;
set global sql_mode='STRICT_TRANS_TABLES' 服务端不重启永久有效
mysql> set session sql_mode='strict_trans_tables'; Query OK, 0 rows affected (0.00 sec) mysql> create table t7(id tinyint unsigned); Query OK, 0 rows affected (0.03 sec) mysql> insert into t7 values(-129),(128),(-1),(256); ERROR 1264 (22003): Out of range value for column 'id' at row 1
方式二:在配置文件永久修改:[mysqld] sql_mode='STRICT_TRANS_TABLES'
浮点型有float、double、decimal三种类型,三者都可以存储浮点型数据,但是精确度不一致(float<double<decimal)
float(n,m) 第一个数n表示总长度总共多少位,第二个数m表示小数占 多少位;double(n,m)、decimal(n,m)意思一样
create table t10(id float(255,30)); create table t11(id double(255,30)); create table t12(id decimal(65,30)); insert into t10 values(11.11111111111111111111111111111111111); insert into t11 values(11.11111111111111111111111111111111111); insert into t12 values(11.11111111111111111111111111111111111);
精确度:float<double<decimal;一般情况下,float足够使用了,如果想追求非常完美的精确度,可以使用字符串来代替
mysql> select * from t10; +-----------------------------------+ | id | +-----------------------------------+ | 11.111110687255860000000000000000 | +-----------------------------------+ 1 row in set (0.00 sec) mysql> select * from t11; +-----------------------------------+ | id | +-----------------------------------+ | 11.111111111111110000000000000000 | +-----------------------------------+ 1 row in set (0.00 sec) mysql> select * from t12; +-----------------------------------+ | id | +-----------------------------------+ | 11.111111111111111111111111111111 | +-----------------------------------+ 1 row in set (0.00 sec)
字符类型有两种类型:char、varchar
1.char(n):定长,最大只能存储n个字符,如果超过范围就会直接报错;如果不超出范围,则用空格填充至四个字符 2.varchar(n):变长,最大只能存储n个字符,如果超过范围则直接报错,如果不超出范围,则有几位就存几位
mysql> create table t8(id int,name char(4)); Query OK, 0 rows affected (0.04 sec) mysql> create table t9(id int,name varchar(4)); Query OK, 0 rows affected (0.04 sec)
超出范围两者都会报错
mysql> insert into t8 values(1,'jason') -> ; ERROR 1406 (22001): Data too long for column 'name' at row 1 mysql> insert into t9 values(1,'jason'); ERROR 1406 (22001): Data too long for column 'name' at row 1
利用 char_length() 来统计字段数据长度。
mysql> insert into t8 values(1,'aa'),(2,'bbb'); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> insert into t9 values(1,'aa'),(2,'bbb'); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select char_length(name) from t8; +-------------------+ | char_length(name) | +-------------------+ | 2 | | 3 | +-------------------+ 2 rows in set (0.00 sec) mysql> select char_length(name) from t9; +-------------------+ | char_length(name) | +-------------------+ | 2 | | 3 | +-------------------+ 2 rows in set (0.00 sec)
默认情况下char在存储的时候针对没有满足固定卫视的字符会自动填充空格,然后再读取的时候会自动将填充的空格移除,如果想取消该机制 ,需要添加set global sql_mode='strict_trans_tables,pad_char_to_full_length';(该目录是替换,不是新增,所以之前的配置也要写上)
mysql> exit Bye C:\WINDOWS\system32>mysql -uroot -p Enter password: *** mysql> use d3 Database changed mysql> select char_length(name) from t8; +-------------------+ | char_length(name) | +-------------------+ | 4 | | 4 | +-------------------+ 2 rows in set (0.00 sec) mysql> select * from t8; +------+------+ | id | name | +------+------+ | 1 | aa | | 2 | bbb | +------+------+ 2 rows in set (0.00 sec) mysql> select char_length(name) from t9; +-------------------+ | char_length(name) | +-------------------+ | 2 | | 3 | +-------------------+ 2 rows in set (0.00 sec) mysql> select * from t9; +------+------+ | id | name | +------+------+ | 1 | aa | | 2 | bbb | +------+------+ 2 rows in set (0.00 sec)
1.char 优点:整存整取,速度快 缺点:浪费存储空间 eg:char(4) a bb ccccddd eeee 2.varchar 优点:节省存储空间 缺点:存取数据的速度慢于char eg:varchar(4) 1bytes+a1bytes+bb1bytes+cccc1bytes+ddd1bytes+eeee 存取数据都需要操作报头(耗时) ps:两者使用频率都很高,现在默认很多时候是varchar
数字大部分情况下都是用来限制字段的存储长度,但是整型除外。
整型不是用来限制存储的长度,而是展示长度。以后涉及到整型字段的定义,类型后面不需要加括号写数字,除非有业务需求必须固定位数
zerofill 表示用零填充
mysql> create table t13(id int(3)); Query OK, 0 rows affected (0.03 sec) mysql> insert into t13 values(1),(11),(111),(1111); Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from t13; +------+ | id | +------+ | 1 | | 11 | | 111 | | 1111 | +------+ 4 rows in set (0.00 sec) mysql> create table t14(id int(3) zerofill); Query OK, 0 rows affected (0.04 sec) mysql> insert into t14 values(1),(11),(111),(1111); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from t14; +------+ | id | +------+ | 001 | | 011 | | 111 | | 1111 | +------+ 4 rows in set (0.00 sec)
枚举型是多选一的情况使用,集合是多选多或多选一的情况使用
enum(选项一,选项二...)
mysql> create table t15(id int,name varchar(32),gender enum('male','female','other')); Query OK, 0 rows affected (0.04 sec) mysql> insert into t15 values(1,'nana','female'); Query OK, 1 row affected (0.01 sec) mysql> insert into t15 values(2,'aa','男'); ERROR 1265 (01000): Data truncated for column 'gender' at row 1 mysql> select * from t15; +------+------+--------+ | id | name | gender | +------+------+--------+ | 1 | nana | female | +------+------+--------+ 1 row in set (0.00 sec)
mysql> create table t16(id int,name varchar(32),hobby set('read','sleeep','eat')); Query OK, 0 rows affected (0.03 sec) mysql> insert into t16 values(1,'nana','read'); Query OK, 1 row affected (0.01 sec) mysql> insert into t16 values(2,'aa','read,sleeep,eat'); Query OK, 1 row affected (0.00 sec) mysql> insert into t16 values(1,'dd','读书'); ERROR 1265 (01000): Data truncated for column 'hobby' at row 1 mysql> select * from t16; +------+------+-----------------+ | id | name | hobby | +------+------+-----------------+ | 1 | nana | read | | 2 | aa | read,sleeep,eat | +------+------+-----------------+ 2 rows in set (0.00 sec)
日期类型有date(年月日)、datetime(年月日 时分秒)、time(时分秒)、year(年份)
mysql> create table t17(id int,name varchar(32),birth date,register_time datetime,study_time time,now_year year); Query OK, 0 rows affected (0.04 sec) mysql> desc t17; +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(32) | YES | | NULL | | | birth | date | YES | | NULL | | | register_time | datetime | YES | | NULL | | | study_time | time | YES | | NULL | | | now_year | year(4) | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+ 6 rows in set (0.02 sec) mysql> insert into t17 values(1,'nana','1998-12-29','2022-11-11 11:11:11','11:11:11','2022'); Query OK, 1 row affected (0.01 sec) mysql> select * from t17; +------+------+------------+---------------------+------------+----------+ | id | name | birth | register_time | study_time | now_year | +------+------+------------+---------------------+------------+----------+ | 1 | nana | 1998-12-29 | 2022-11-11 11:11:11 | 11:11:11 | 2022 | +------+------+------------+---------------------+------------+----------+ 1 row in set (0.00 sec)
mysql> create table t18(id int unsigned); Query OK, 0 rows affected (0.04 sec) mysql> insert into t18 values(-1),(1); ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> create table t19(id int(4) zerofill); Query OK, 0 rows affected (0.04 sec) mysql> insert into t19 values(1); Query OK, 1 row affected (0.01 sec) mysql> select * from t19; +------+ | id | +------+ | 0001 | +------+ 1 row in set (0.00 sec)
mysql> create table t20(id int,name varchar(32) not null); Query OK, 0 rows affected (0.03 sec) mysql> insert into t20 values(1,'nana'); Query OK, 1 row affected (0.01 sec) msql> insert into t20 values(2); ERROR 1136 (21S01): Column count doesn't match value count at row 1 mysql> insert into t20(id) values(30); ERROR 1364 (HY000): Field 'name' doesn't have a default value
mysql> create table t21(id int,name varchar(32),gender varchar(32) default '男'); Query OK, 0 rows affected (0.04 sec) mysql> insert into t21 values(1,'aa'); ERROR 1136 (21S01): Column count doesn't match value count at row 1 mysql> insert into t21(id,name) values(1,'aa'); Query OK, 1 row affected (0.00 sec) mysql> insert into t21 values(2,'bb','女'); Query OK, 1 row affected (0.01 sec) mysql> select * from t21; +------+------+--------+ | id | name | gender | +------+------+--------+ | 1 | aa | 男 | | 2 | bb | 女 | +------+------+--------+ 2 rows in set (0.00 sec)
使用可以单列唯一,也可以联合唯一
mysql> create table t22(id int unique,name varchar(32)); Query OK, 0 rows affected (0.04 sec) mysql> insert into t22 values(1,'nana'); Query OK, 1 row affected (0.01 sec) mysql> insert into t22 values(1,'aa'); ERROR 1062 (23000): Duplicate entry '1' for key 'id' mysql> insert into t22 values(2,'nana'); Query OK, 1 row affected (0.01 sec) mysql> select * from t22; +------+------+ | id | name | +------+------+ | 1 | nana | | 2 | nana | +------+------+ 2 rows in set (0.00 sec)
mysql> create table t23(host varchar(32),port int,unique(host,port)); Query OK, 0 rows affected (0.02 sec) mysql> insert into t23 values('127.0.0.1',8080); Query OK, 1 row affected (0.01 sec) mysql> insert into t23 values('127.0.0.1',8088); Query OK, 1 row affected (0.00 sec) mysql> insert into t23 values('127.0.1.0',8080); Query OK, 1 row affected (0.01 sec) mysql> insert into t23 values('127.0.0.1',8080); ERROR 1062 (23000): Duplicate entry '127.0.0.1-8080' for key 'host' mysql> select * from t23; +-----------+------+ | host | port | +-----------+------+ | 127.0.0.1 | 8080 | | 127.0.0.1 | 8088 | | 127.0.1.0 | 8080 | +-----------+------+ 3 rows in set (0.00 sec