type of data
1. Integer
- Unsigned a certain number of non-negative
create table t3(
age tinyint unsigned
);
- Display width (ZEROFILL)
integral display width, when filled with enough digits 0
create table t4(
id int(10) zerofill primary key auto_increment,
name char(32)
);
insert into t4 values(12345, '5个');
insert into t4 values(1234567890, '10个');
insert into t4 values(123456789012, '12个');
select * from t4;
2. Float
Bits fixed point much longer use:
a float (M, D)
Double (M, D)
decimal (M, D)
M is the number of support length, D is the number of digits after the decimal point
create table t5 (
a float(10, 2),
b double(10, 2),
c decimal(10, 2)
);
3. string type
The difference between CHAR and VARCHAR types
Think:
strings, so you can specify the size of the float freely, it's time to operate than usual casual finger
given a can it?
A: No, the data type is not the better, larger data type will result in a bloated, storage space is
occupied by large, data retrieval will be slow
4. Enumeration (enum)
When one kind selected from a plurality of types of data used
at the time of front-end radio button, enumerated type can function
enumerated type advantages: 1. Optional limit value
2. space saving
3. High operating efficiency
create table t6(
name varchar(32),
sex enum('男','女','保密') default '保密'
);
-- 枚举类型的计数默认从1开始
insert into t6 set name='王宝强',sex=1;
The collection (set)
SET can have a maximum of 64 different members. Similar to the check box, you can choose how many how many.
create table t7 (
name varchar(32),
hobby set('吃','睡','玩','喝','抽')
);
insert into t7 values('张三','睡,抽,玩,吃,喝');
insert into t7 values('李四','睡,抽');
- Why not use set type?
In modern web development, multi-marquee value with thousands of stored value index with no more empty - How to solve problems that box?
The value of the check box of individually designed to a table
6. Time Type
- datetime
create table datetime_test (
create_at datetime
);
insert into datetime_test values('2019-4-2 16:54:00');
insert into datetime_test values('2019/4/2 16:54:00');
insert into datetime_test values(now());
-- 年份最大支持4个长度
insert into datetime_test values('10000/4/2 16:54:00'); --
错误
insert into datetime_test values('9999/4/2 16:54:00');
2.time
create table time_test (
create_at time
);
insert into time_test values('12:12:12');
insert into time_test values('100:12:12');
insert into time_test values('-100:12:12');
insert into time_test values('10 10:12:12');
-- 时间的范围是: [-838:59:59 - 838:59:59]
insert into time_test values('839:12:12'); -- 错误的
3.timestamp timestamp type
Timestamp in the display area and datetime type is the same, is not the same in the memory
range from 1970-1-10: 2038-1-19 11:14:07 to 0: 0
indicates 4 bytes time stamp using
the value size storage for bit length: 2 * (4 * 8--1)
create table timestamp_test (
create_time timestamp
);
insert into timestamp_test values(now());
insert into timestamp_test values('2038-1-19 11:14:07'); -
- 时间戳最大值
insert into timestamp_test values('2038-1-19 11:14:08'); -
- 错误
4.year
create table `year`(
create_at year
);
-- 从1900年开始 - 1900+255
insert into `year` values(now());
insert into `year` values('2155'); -- 年份最大值
insert into `year` values('2156'); -- 错误
7. Boolean
create table `bool`(
cond boolean
);
insert into `bool` set cond=True; -- 成功
insert into `bool` set cond=False; -- 成功
insert into `bool` set cond=1; -- 成功
insert into `bool` set cond=10; -- 成功
insert into `bool` set cond=-1; -- 成功
insert into `bool` set cond=0; -- 成功
insert into `bool` set cond=0.1; -- 成功
insert into `bool` set cond='True'; -- 失败
8. The column properties
Whether the inserted value can be empty
- null: is empty, the default is not to write
- not null: not empty, if inserted, touch field is null, the error
create table null_test (
id int primary key auto_increment,
username varchar(32) not null,
pwd varchar(16) null
);
insert into null_test values(null,null,null);
- default
create table default_test (
id int primary key auto_increment,
username varchar(32) default 'admin' not null,
pwd varchar(16) default 123456
);
insert into default_test (username) values ('admin');
- auto_increment
automatic growth column
default starting from a
normally used with the primary key
create table auto_inc (
id int primary key auto_increment,
name varchar(32)
);
insert into auto_inc (name) values ('aaa'), ('bbb'),
('ccc');
select * from auto_inc;
/* 输出:
+----+------+
| id | name |
+----+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
+----+------+
*/
- primary key
primary key uniquely identifies general
characteristics: can not be empty, can not be repeated, a table which can have only one primary key
-- 这里只有一个主键,这种主键叫做联合主键, 在项目中使用较少
create table double_pri_test (
id int,
sid int,
primary key(id,sid)
);
insert into double_pri_test values (1, 1);
insert into double_pri_test values (1, 2); -- 成功
insert into double_pri_test values (2, 1); -- 成功
insert into double_pri_test values (1, 1); -- 失败
- unique
unique key, to ensure that each data column which will not repeat the
mailbox can not be repeated, the phone number can not be repeated
create table test_uniq (
id int auto_increment primary key,
mobile char(11) unique
);
insert into test_uniq set mobile=13999999999;
- comment
field descriptions: the developer to see, for a general description of the respective fields
create table test_cmt (
ctime datetime comment '这个字段代表创建日期'
);
9. SQL comments
Single-line comments: - Hello
multiline comment: / * barabara * /
MySQL unique single-line comments: ha ha ha ha #