1.通过命令提示符操作数据库
2.创建表
create table t_test(
id int primary key auto_increment,
name varchar(20) unique not null,
sex varchar(20) check(sex in ('男','女')),
salary decimal(10,2),
birthday datetime,
state int default 1
);
auto_increment代表的是自增长,插入数据时数据设为null即可
unique就是唯一,not null就是不能为空
check就是该字段的内容需要是后面规定的内容
decimal中的10代表的是有效位,2代表的是小数位
default代表默认值为后面的数值
3.操作字段
删除字段:alter table t_test drop column state;
添加字段:alter table t_test add column state int;在后面加上default 1 时创建的该字段的数值为1
修改字段:alter table t_test change a a int; 修改字段的类型
alter table t_test change a b int; 修改字段名
4.增删查改
查:
select * from t_test;
select * from t_test where id = 1 and sex = "男";
select * from t_test where id = 1 and sex = "男" limit 0,3; 分页查询0代表页码,从0页开始,3代表每页的尺寸
select * from d_book order by salenum desc limit 3
降序查询前3条数据
select * from d_book order by salenum asc limit 2
升序查询前2条数据
select count(*) from d_book where cid = #{id}
三表连接查询:
select a.*,b.*,c.*
from a left join b
on a.id = b.aid
join c
on b.id = c.bid
where c.id = #{id}
增:insert into t_test values(null,"ycc","男","15000","1999-11-11",1);
删:delete from t_test where id = 1;
改:update t_test set name = "ycc666" where id =1;