一.存储引擎
Innodb
目前5.1之后MySQL版本默认的存储引擎
支持事务(support transactions),行锁(row-level locking),外键(foreign key)
由于上面的支持 数据更安全

建表的时候innodb会产生两个文件
一个是表结构文件
一个是存储数据文件
MyIsam
5.1版本之前的MySQL的默认存储引擎
查询速度较于Innodb要快
建表的时候会产生三个文件
一个是表结构文件
一个是索引文件
索引你先把它理解成是书的目录,能够帮助你更快的查询数据
一个是存储数据文件
memory
将数据存放于内存中
建表的时候都仅仅只有一个表结构文件
blackhole
任何写入的数据都会消失
建表的时候都仅仅只有一个表结构文件

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 |

+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+


二、创建表的完整语法
create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],
字段名3 类型[(宽度) 约束条件]
);
注意:
1.字段名和字段类型是必须的 中括号内的参数都是可选参数
2.同一张表中字段名不能重复
3.最后一个字段后面不能加逗号
create table t6(
id int,
name char,
);

宽度:
使用数据库的准则:能尽量让它少干活就尽量少干活

对存储数据的限制
char(1) 只能存一个字符
如果超了 mysql会自动帮你截取
1.插入的时候 mysql自动截取
2.会直接报错(mysql严格模式)


alter table t5 modify name char not null;
not null该字段不能插空

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;

insert into t1 values(1);
insert into t2 values(2);
insert into t3 values(3);
insert into t4 values(4);

select * from t1; #
select * from t2;
select * from t3; #关闭服务端数据就消失
select * from t4; #写什么,什么就消失

##建表
mysql> create table t5(id int,name char(2),password int,play char(6));
Query OK, 0 rows affected (0.31 sec)

mysql> desc t5;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(2) | YES | | NULL | |
| password | int(11) | YES | | NULL | |
| play | char(6) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+

##在t5表中插入数据
mysql> insert into t5 values(4,'hang',12344213789456,'粉红色的健康很舒服');
Query OK, 1 row affected, 3 warnings (0.07 sec)

mysql> select * from t5;
+------+------+------------+--------------------+
| id | name | password | play |
+------+------+------------+--------------------+
| 1 | zg | 2147483647 | ballll |
| 1 | ha | 2147483647 | balsdf |
| 3 | zg | 2147483647 | ballll |
| 4 | ha | 2147483647 | 粉红色的健康 |
+------+------+------------+--------------------+
在id=4时,我们可以看到char括号内的数字限制了字符的个数,不管字母或汉字或其他字符一个就算成一个字符,在我自定表字段的时候设置name
char(2),实际名字‘hang’是4个字符,被截取我们设定的字符个数;password

#字段可以插空
mysql> desc t5;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(2) | YES | | NULL | |
| password | int(11) | YES | | NULL | |
| play | char(6) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
mysql> insert into t5 value(5,'an',2344789456,'NULL');
Query OK, 1 row affected, 1 warning (0.05 sec)

mysql> select * from t5;
+------+------+------------+--------------------+
| id | name | password | play |
+------+------+------------+--------------------+
| 1 | zg | 2147483647 | ballll |
| 1 | ha | 2147483647 | balsdf |
| 3 | zg | 2147483647 | ballll |
| 4 | ha | 2147483647 | 粉红色的健康 |
| 5 | an | 2147483647 | NULL |
+------+------+------------+--------------------+

#更改表中的某个字段不能为空,设置完以后,
alter table t5 modify name char not null;
not null该字段不能插空如果再插入空,该字段会被插空的地方会被清空。

mysql> alter table t5 modify play char not null;
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> update t5 set play=null where id=1;
Query OK, 2 rows affected, 2 warnings (0.06 sec)
Rows matched: 2 Changed: 2 Warnings: 2

mysql> select * from t5;
+------+------+------------+------+
| id | name | password | play |
+------+------+------------+------+
| 1 | z | 2147483647 | |
| 1 | h | 2147483647 | |
| 3 | z | 2147483647 | b |
| 4 | h | 2147483647 | 粉 |
| 5 | a | 2147483647 | N |
| 6 | z | 12345 | |
+------+------+------------+------+
6 rows in set (0.00 sec)



类型和中括号内的约束
类型约束的是数据的存储类型
而约束是基于类型之上的额外限制

#注意:
1. 在同一张表中,字段名不能相同
2. 宽度和约束条件可选,字段名和类型是必须的
3. 最后一个字段后不能加逗号!

# 补充:
# 1.宽度指的是对存储数据的限制
create table userinfo(name char);
insert into userinfo values('jason');
"""
1.没有安全模式的数据库版本,能够存放数据但是只会存进去一个j
2.最新数据库版本直接报错提示无法存储:Data too long for column 'name' at row 1
"""

# 2.约束条件初识>>> null 与 not null
#设置not null后,char默认只能只能接受1个字符,即,char(1),多余的字符会被清楚掉。
create table t1(id int,name char not null);
insert into t1 values(1,'j'); # 正常存储
insert into t1 values(2,null); # 报错

# 总结 类型与约束条件区别
# 类型:限制字段必须以什么样的数据类型存储
# 约束条件:约束条件是在类型之外添加一种额外的限制

 

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信