# *****64_整型*****
"""
-- 創(chuàng)建一個名為my_int的整型表
create table my_int(
int_1 tinyint,
int_2 smallint,
int_3 int,
int_4 bigint
)charset utf8;
-- 插入數據
-有效數據
insert into my_int
values(100,100,100,100);
-無效數據: 類型限定
insert into my_int
values('a','b','199','f');
-錯誤: 超出范圍
insert into my_int
values(255,10000,100000,1000000);
-- 給表增加一個無符號類型
alter table my_int add int_5
tinyint unsigned; -- 無符號類型
-- 插入數據
insert into my_int
values(127,10000,100000,1000000,
255);
-- 指定顯示寬度
alter table my_int add int_6
tinyint(1) unsigned;
-- 插入數據
insert into my_int
values(127,0,0,0,255,255);
-- 顯示寬度為2, 0填充
alter table my_int add int_7
tinyint(2) zerofill;
-- 插入數據
insert into my_int
values(1,1,1,1,1,1,1);
insert into my_int
values(100,100,100,100,100,100,100);
"""
# *****65_浮點型*****
"""
-- 浮點數表
create table my_float(
f1 float,
-- !!!最大十位!!!, 有二位是小數
f2 float(10,2),
-- 10位在精度范圍之外
f3 float(6,2)
-- 6位在精度范圍之內
);
-- 插入數據
insert into my_float
values (1000.10,1000.10,1000.10);
insert into my_float
values(1234567890,12345678.90,1234.56);
-- e:10的x次方
insert into my_float
values(3e38,3.01e7,1234.56);
-- 后兩位是最大值
insert into my_float
values(999999999,99999999.99,9999.99);
-- 超出長度插入數據
insert into my_fioat
values(123456,1234.12345678,123.9876543);
-- 小數部分可以超出長度
insert into my_fioat
values(123456,1234.12,12345.56);
-- 最后一個整數部分超出
"""
# *****66_定點型*****
"""
-- 創(chuàng)建定點數表
create table my_decimal(
f1 float(10,2),
d1 decimal(10,2)
);
-- 插入數據
insert into my_decimal
values(12345678.90,12345678.90);
-- 有效數據
insert into my_decimal
values(1234.123456,1234.123456);
-- 小數部分可以超出
-- 查看警告
show warnings;
-- 插入數據
insert into my_decimal
values(99999999.99,99999999.99);
-- 沒有問題
insert into my_decimal
values(99999999.99,99999999.999);
-- 進位超出范圍
"""
# *****67_日期*****
"""
-- 創(chuàng)建時間日期表
create table my_date(
d1 datetime,
d2 date,
d3 time,
d4 timestamp,
d5 year
);
-- 插入數據
insert into my_date
values('2019-11-21 13:38:36',
'2019-11-21','13:38:36',
'2019-11-21 13:38:36',2015);
-- 時間使用負數
insert into my_date
values('2019-11-21 13:38:36',
'2019-11-21','-13:38:36',
'2019-11-21 13:38:36',2015);
insert into my_date
values('2019-11-21 13:38:36',
'2019-11-21','-213:38:36',
'2019-11-21 13:38:36',2015);
insert into my_date
values('2019-11-21 13:38:36',
'2019-11-21','-2 13:38:36',
'2019-11-21 13:38:36',2015);
-- -2表示過去2天, 也就是48小時
-- year可以使用2位或者四位
insert into my_date
values('2019-11-21 13:38:36',
'2019-11-21','13:38:36',
'2019-11-21 13:38:36',69);
insert into my_date
values('2019-11-21 13:38:36',
'2019-11-21','13:38:36',
'2019-11-21 13:38:36',70);
-- timestamp: 修改記錄
update my_date set d1=
'2019-11-21 19:37:17' where d5=
1970;
"""
# *****68_字符串*****
"""
-- 創(chuàng)建枚舉表: 枚舉實際上存儲的是數值()
create table my_enum(
gender enum('男','女','保密')
);
-- 插入數據
insert into my_enum
values('男'),('保密');
-- 有效數據
insert into my_enum
values('male');
-- 錯誤數據: 沒有該元素
-- 將字段結果取出來進行+0運算
select gender + 0, gender
from my_enum;
-- 數值插入枚舉元素
insert into my_enum
values(1),(2);
"""
# *****69_列屬性(空_描述_默認值_主鍵)*****
"""
-- 創(chuàng)建班級表
create table my_class(
name varchar(20) not null,
room varchar(20) null
-- 代表允許為空, 不寫默認就是允許為空
);
-- 創(chuàng)建表 【comment:描述表的語句的意思】
create table my_teacher(
name varchar(20) not null comment
'姓名',
money decimal(10,2) not null
comment '工資'
);
-- 默認值 【unsigend:無符號】
create table my_default(
name varchar(20) not null,
age tinyint unsigned default 0,
gender enum('男','女','保密') default '男'
);
--插入數據
insert into my_default (name)
values('阿飛');
insert into my_default
values('男閨蜜',18,default);
-- 增加主鍵
create table my_pril(
name varchar(20) not null comment
'姓名',
number char(10) primary key comment
'學號: bc2019+0001, 不能重復'
);
-- 復合主鍵 【default:默認xxx】
create table my_pri2(
number char(10) comment
'學號: bc20190007',
course char(10) comment
'課程代碼: bc25690007',
score tinyint unsigned default 60
comment '成績',
-- 增加主鍵的限制: 學號和課程號應該是
-- 對應的,具有唯一性
primary key(number, course)
);
-- 追加主鍵
create table my_pri3(
course char(10) not null comment
'課程代碼: bc25890001',
name varchar(10) not null comment
'課程名字'
);
-- 第一種方式 【modify:修改】
alter table my_pri3 modify course
char(10) primary key comment
'課程代碼: bc25890001';
-- 第二種方式
alter table my_pri3 add primary
key(course);
"""