1.在终端输入\s,查看数据库的基本信息(当前用户,版本,编码,端口号)
2.默认的配置文件是my-default.ini
拷贝上述的文件,然后将文件程序命名为my.ini
添加字符编码相关配置:
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
配置文件中涉及到了MySQL相关的配置的修改,那么需要重启服务器才可以生效
ps:我们可以利用配置文件偷懒,将用户名和密码写入配置文件中 ,后续登录的时候就不需要进行输入密码了
我们可以理解存储引擎就是针对相同数据采取不同的存取策略
show engines;
ps:MySQL中默认忽略大小写
1.MyISAM
MySQL5.5之前默认的存储引擎
存取速度快,但是功能少,安全性较低
2.InnoDB
MySQL5.5之后的默认存储引擎
支持事物,行锁,外键等操作,速度没有MyISAM快,但是安全性更高
3.Memory
基于内存的存储引擎,仅应用于临时表的数据存取,但是断电立刻丢失
4.BlackHole
任何写入的数据会立刻消失,类似于垃圾站
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql> use db1;
Database changed
mysql> create table t1(id int) engine=innodb;
Query OK, 0 rows affected (0.02 sec)
mysql> create table t2(id int) engine=myisam;
Query OK, 0 rows affected (0.04 sec)
mysql> create table t3(id int) engine=memory;
Query OK, 0 rows affected (0.01 sec)
mysql> create table t4(id int) engine = blackhole;
Query OK, 0 rows affected (0.04 sec)
mysql> insert into t1 values(1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t2 values(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t3 values(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t4 values(1);
Query OK, 1 row affected (0.00 sec)
1.Innodb
.frm --->> 表结构
.ibd --->> 表数据(表索引)
2.myisam三个文件
.frm ---->> 表结构
.MYD ---->> 表数据
.MYI ---->> 表索引
3.memory一个文件
.frm ---->> 表结构
4.blackhole一个文件
.frm ----->> 表结构
create table 表名(
字段名 字段类型(数字) 约束条件,
字段名 字段类型(数字) 约束条件,
字段名 字段类型(数字) 约束条件
);
1.字段名和字段类型是必须的
2.数字和约束条件是可选的(可有可无)
3.约束条件可以写多个,也可以一个,多个用逗号隔开就好了
4.最后一行不能加逗号
tinyint 1bytes
smallint 2bytes
int 4bytes
bigint 8bytes
1.验证整型是否自带负号
mysql> create table t5(id tinyint);
Query OK, 0 rows affected (0.04 sec)
mysql> insert into t5 values(-129),(256);
Query OK, 2 rows affected, 2 warnings (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 2
mysql> desc t5;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | tinyint(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.01 sec)
2.自定义移除符号
# unsigned 约束条件,意思是说不需要负号
mysql> create table t6(id tinyint unsigned);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t6 values(-129),(256);
Query OK, 2 rows affected, 2 warnings (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 2
mysql> select * from t6;
+------+
| id |
+------+
| 0 |
| 255 |
+------+
2 rows in set (0.00 sec)
插入的数据超出了数据类型的范围,不应该让其插入并自动修改,没有意义,数据库应该直接报错
我们可以修改my.ini配置文件就可以完成这样的设想
[mysqld]
sql_mode='strict_trans_tables'
mysql> use db1;
Database changed
mysql> insert into t6 values(-8);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
float,double,decimal三者都可以存储浮点型数据,但是各自的精确度不一致
mysql> create table t7(id float(60,20));
Query OK, 0 rows affected (0.02 sec)
mysql> create table t8(id double(60,20));
Query OK, 0 rows affected (0.02 sec)
mysql> create table t9(id decimal(60,20));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t7 values(1.11111111111111111111);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t8 values(1.11111111111111111111);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t9 values(1.11111111111111111111);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t7;
+------------------------+
| id |
+------------------------+
| 1.11111116409301760000 |
+------------------------+
1 row in set (0.00 sec)
mysql> select * from t8;
+------------------------+
| id |
+------------------------+
| 1.11111111111111120000 |
+------------------------+
1 row in set (0.00 sec)
mysql> select * from t9;
+------------------------+
| id |
+------------------------+
| 1.11111111111111111111 |
+------------------------+
1 row in set (0.00 sec)
# 精确度:float < double < decimal
# 一般情况下,float足够使用了,如果想要追求精确,可以用字符串代替
char:定长
char(4) 最多存储四个字符,超出就报错,不够四个,空格来填
varchar:变长
varchar(4) 最多存储四个字符,超出就报错,不够有几位存几位
mysql> insert into t10 values(1,'jason1');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t11 values(1,'jason1');
ERROR 1406 (22001): Data too long for column 'name' at row 1
# 位数超出就报错
char与varchar
char;整存整取,速度快,浪费存储空间
varchar:节省存储空间,存取数据的速度慢于char
默认情况下char在存储的时候针对没有满足固定位数的字符会自动填充空格,然后在读取的时候又会自动将填充的空格移除,如果想取消该机制,需要
sql_mode
set global sql_mode='strict_trans_tables,pad_char_to_full_length';
mysql> create table t16(id int,name char(4));
Query OK, 0 rows affected (0.02 sec)
mysql> create table t17(id int,name varchar(4));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t16 values(2,'J');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t17 values(1,'tom');
Query OK, 1 row affected (0.01 sec)
mysql> select char_length(name) from t17;
+-------------------+
| char_length(name) |
+-------------------+
| 3 |
+-------------------+
1 row in set (0.00 sec)
数字在很多地方都是用来表示限制存储数据的长度
但是在整型中数字却不是用来限制存储长度,而是控制展示的长度
以后写整型无需添加数字
create table t18(id int(3));
insert into t18 values(1),(123),(123456);
create table t19(id int(3) zerofill);
insert into t19 values(1),(123),(123456);
mysql> select * from t19;
+--------+
| id |
+--------+
| 001 |
| 123 |
| 123456 |
+--------+
3 rows in set (0.00 sec)
枚举(多选一):
mysql> create table t12(
-> id int,
-> name varchar(32),
-> gender enum('male','female')
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t12 values(1,'jason','male');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t12;
+------+-------+--------+
| id | name | gender |
+------+-------+--------+
| 1 | jason | male |
+------+-------+--------+
1 row in set (0.00 sec)
集合(多选多):
mysql> create table t13(
-> id int,
-> name varchar(13),
-> hobbies set('dance','sing','drew')
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t13 values(1,'jia','dance,sing,drew');
Query OK, 1 row affected (0.04 sec)
mysql> select * from t13;
+------+------+-----------------+
| id | name | hobbies |
+------+------+-----------------+
| 1 | jia | dance,sing,drew |
+------+------+-----------------+
1 row in set (0.00 sec)
datetime ---->> 年月日时分秒
date ---->> 年月日
time ---->> 时分秒
year ---->> 年
mysql> create table t15(
-> id int,
-> name varchar(32),
-> register_time datetime,
-> birthday date,
-> study_time time,
-> work_time year
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t15 values(1,'jia','2000-11-11 11:11:11','1998-01-21','11:11:11','2000');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t15;
+------+------+---------------------+------------+------------+-----------+
| id | name | register_time | birthday | study_time | work_time |
+------+------+---------------------+------------+------------+-----------+
| 1 | jia | 2000-11-11 11:11:11 | 1998-01-21 | 11:11:11 | 2000 |
+------+------+---------------------+------------+------------+-----------+
1 row in set (0.00 sec)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章