1.字段类型
一 数值类型
整数
- int 整数
- long 长整数
小数
- float 单精度
- double 双精度
- decimal 小数值 和钱挂钩的,用它
字符串
- char 定长字符串 0-255字节 abc–》abc255 自动补全,造成资源浪费
- varchar 'abc’变长字符串 0-65535字节 abc–》abc 来多少就多少位置,资源最大化利用
- text 文本
日期和时间
- date YYYY-MM-DD 2019-09-10
- time HH:MM:SS 10:10:10
- datetime 2019-09-10 10:10:10
- timestamp 2019-09-10 10:10:10
行业交流术语
DDL: 数据定义语言 create(创建) drop(删除) alter(修改)
DML: 数据操作语言 insert(增) update(改) delete(删) select(查)
DCL: 数据控制语言 grant (赋权限)
案例
create table rzdata(
id int ,
name varchar,
age int
)
##创建 表
insert into rzdata(id,name,age) values(1,'cq',18);
##增加插入列极其对应的值
select * from rzdata;
查询
update rzdata set age=22 where id=1;
修改 条件
delete from rzdata where id=1;
删 条件
drop table rzdata;
删除 表
生产上正常表结构
案例
create table studentinfo(
id int auto_increment primary key,##主键自增长
num int ,
name varchar(200), ##表的内容
age int,
create_time timestamp default current_timestamp, ##创建时间
cretae_user varchar(100), ##创建人
update_time timestamp default current_timestamp on update current_timestamp, ##修改时间
update_user varchar(100) ##修改人
);
insert into ruozedata.studentinfo(num,name,age) values(1,'cq',18); ##增加插入列极其对应的值
select * from studentinfo; ## 查询
总结
#1.id 一定要自增长 非业务字段 为了性能更好 (头)
#2.创建时间 创建人 修改时间 修改人 (尾)
#3.规范 统一风格 +假如表未减 你掌舵、 已建表 就看表的规则 遵循 (内容)
#4.千万不要用 汉语拼音或首字母 做 字段名称
#5.千万不要用中文 做字段名称
update
update ruozedata.studentinfo set age=28 where num=1; #生产上 update切记 是否要加where
select * from studentinfo;
总结
update_time修改时间的变化是因为我们在设置表的结构是写了语句:on update current_timestamp 当发生了变化它会自己的做一个更新。 update ##约束
update ruozedata.studentinfo set age=19; ##不加where多列 所有人的年龄发生变化,都一样的
update ruozedata.studentinfo set name='huhu2',age=19 where id=3;##修改两个列,以逗号拼接
delete
delete from ruozedata.studentinfo where id=3;#生产上 delete切记 要加where,不然数据全删除
insert into ruozedata.studentinfo;
values(3,3,'huhu',16,'2019-06-26 22:20:10','r','2019-06-26 22:20:10','r'); 这种可以
(num,name,age)##values(1,'cq',18)可以不要,但是列需全部补齐,时间和人以字符串的形式表达,需加‘’。
select
条件where确定好,ID亦或age,num,name然后根据需求><=去查询筛选
select * from ruozedata.studentinfo ;
select * from ruozedata.studentinfo where id=1;
select * from ruozedata.studentinfo where name>1;
select * from ruozedata.studentinfo where name!='ruoze1'; ##不等于 英文状态下的!
select * from ruozedata.studentinfo where age=26 or name='ruoze1'; ##or 或
select * from ruozedata.studentinfo where age=26 and name='ruoze1'; ##and且
select * from ruozedata.studentinfo
where age=26 and (name='ruoze1' or name='jepson'); ##查询年龄是26且名字是ruoze1或 jepson的人
select * from ruozedata.studentinfo
where name like '%xxx%'; ##like查询%模糊
select * from ruozedata.studentinfo
where name like '__o%'; ###第三个字母是o 占位符__
#约束 default xxxx
#主键 primary key 简写pk 一张表 就只能1个主键== 非空约束(一定要有值)+唯一约束
#加业务的唯一约束(属性-约束-新增)
拓展
#id+warehouse_no 联合主键
1、主键=非空(not null XXX)+唯一约束 (default)
2、主键是唯一约束的变态升级 (not null default XXX)
3、主键或唯一约束 可以多个字段设置 根据你的数据的特性:学校+学号。