创建表格:
create table user(
id int ( 8 ) primary key unique not null auto_increment,
p_name char ( 20 ) unique not null,
p_level int ( 3 ) default 1
)charset=utf8;
关键词:primary key (主键),unique (),not null (不为空),auto_increment (自增长),default (默认值).
删除表:
drop table hehe;
增加列:
alter table lol add lol_type char(10) not null;
删除列:
alter table lol drop lol_type;
添加数据:
insert into t_user(t_name,t_adr) values ("诸葛亮","lovo2");
查询所有数据:
select * from user;
查询去重:
select name distinct from user;
条件查询:
select * from user where id=1;
select name from user where id=1;
多条件查询:
select name from user where name=? and addr=? ;
select name from user where name=? or addr=? ;
select eName from emp where job in("clerk","manager‘");
范围查询:
select name from lol where s_int not between 1000 and 3000 and sex="男";
select name from user where id>10 and id<20;
select * from log_check where data_dt>="2015-04-29" and data_dt<="2015-04-30";
多表查询:
select dname from dept where deptNo in (select deptNo from emp group by deptNo having count(*)>(select count(*) from emp where deptNo=20) );
查询更名:
select p_name as name,p_addr as addr from user;
模糊查询:
select * from t_user where t_name like‘%name%‘;
select * from t_user where t_name like‘%name‘;
select * from t_user where t_name like‘name_‘;
修改数据:
update t_user set name=?,addr=? where id=?;
设置外键:
foreign key ( f_userid ) references t_user( pk_id ) on delete set null;
foreign key ( f_userid ) references t_user( pk_id ) on delete cascade;
分组查询:
select * from emp where sal in ( select max( sal ) from emp group by deptNo ) order by deptNo;
降序查询:
select * from emp order by sal desc;
升序查询:
select * from emp order by sal;
最大最小查询:
select min( hireDate ) as "最早日期" ,max( hireDate ) as "最晚日期" from emp;
求和查询:
select sum( sal ) as "员工工资总和" from emp;
空值查询:
select eName from emp where comm="" or comm is null or comm=0;
统计查询:
select count(dname) from dept where deptNo in(select deptNo from emp);