创建数据库
1 create database studentdb charset utf8;
1 #创建数据库 2 create database studentdb charset utf8; 3 #查看数据库的字符集 4 show create database studentdb; 5 #创建数据表 6 create table students( 7 id int auto_increment, #int 代表数字类型,char 代表字符串类型 8 name char(32) not null, #date 代表时间类型, not null 代表不能为空 9 age int not null, #auto_increment 代表自增 10 addr char(32), # primary key (id) 代表id 为主键 11 register_date date not null, 12 primary key (id));
#插入数据到表 insert into students (name,age,register_date) values ("zhangchen",21,"2016-11-30"); #insert into 表名 (字段)values (字段对应的值);
#查询数据表
select * from students where id >3 and age >24;
select * from students limit 7 ; #查询前7行
select * from students limit 7 offset 2; #从第3行起往后查7行 offset 默认为0
select * from students where register_date like "2016-09%"; #模糊查询
#修改表
update students set name="xuxiaoyu" where age=22; #update 表名 set [字段=值],[字段=值] where age = 22;
delete from students where name = "liruixin"; #删除数据
select * from students order by register_date asc; #order by 后边的字段按升序排序
select * from students order by register_date desc; #order by 后边的字段按降序排序
select name,count(*) from students group by name; #分组统计 统计这个表中相同名字的人数
#字段操作
alter table students add sex enum("M","F") not null; 增加一个字段
alter table students drop addr #删除一个字段
alter table students modify sex enum("f","m") null; alter table students modify age int(20); #修改字段的属性
连表查询
# A , B 为两个表 a,b分别为A和B表的一个字段 select * from A inner join B on A.a = B.b; #查询A表和表的字段的交集 select * from A left join B on A.a = B.b; #求两个表中两个字段的差集 select * from A left join B on A.a = B.b union select * from A right join B on A.a = B.b; #查询两个表中字段的并集