create table t_t( id int(4) auto_increment primary key )engine=innodb auto_increment = 100 charset=utf8; insert into t_t values(null),(null),(null),(null),(null),(null),(null); select * from t_t; alter table t_t auto_increment = 100;
特点:唯一
做查询时,为什么用主键?优点:查询效率高;
缺点:索引文件,占用磁盘空间
-- 索引 create table t_score1( student_id int(10) , # 索引 subject_name varchar(50) , score double, primary key(student_id) )engine= innodb charset = utf8; select * from t_score1 where subject_name='语文' # 0.033s insert into t_score1 values (101,'数学',90); insert into t_score1 values (102,'语文',80); insert into t_score1 values (103,'English',80); insert into t_score1 values (104,'PE',80); insert into t_score1 values (105,'history',80); alter table t_score1 add index subject(subject_name); select * from t_score1 where subject_name='语文'
sql查询后的结果是临时表,视图也是一张临时表。
一张表中有多个主键列
需求:创建成绩表,分别存储学生编号、科目名称,描述一个学生一门课程的成绩。约束一个学生在输入成绩时,相同科目,只能输入一次成绩,重复数据拒绝插入
-- 主键是唯一的,表中只有一个主键的时候,重复的主键数据插不进去。联合主键,多个列联合称为主键,组成映射关系,当A、B两个都含有重复数据时,不能插入 create table t_score( student_id int(10) , subject_name varchar(50) , score double, primary key(student_id,subject_name) )engine= innodb charset = utf8; drop table t_score insert into t_score values (101,'语文',50) insert into t_score values (101,'数学',90) insert into t_score values (101,'语文',80) select * from t_score
in: 枚举查询
select * from t_account where id in(2,5)
all、any、some :必须和比较操作符联合使用,必须使用子查询
all:都满足
-- && 都成立则为true select * from t_account where id > all(select id from t_account where id = 2 or id = 5)
any:满足一个
-- || 有一个成立则成立 select * from t_account where id > any(select id from t_account where id = 2 or id = 5)
some: any别名,效果同any
select * from t_account where id > some(select id from t_account where id = 2 or id = 5)
用作子查询
-- exists 存在 相关子查询(前提条件) 如果相关子查询有数据,exists返回true,反之返回false select score from t_score1 where exists(select score from t_score1 where subject_name='PE')
-- distinct 对某一个字段进行去重查询,只能放在去重字段前面 select distinct name, balance from t_account; -- group by select name,balance from t_account group by name; -- 用来查询 字段非重复数据总条数 select count(distinct name) as '全班名字' from t_account;
-- union [distinct] 并集 多个查询结果,并在一起:不同表中相同列的数据并在一起(不包括重复数据) -- union all 不同表中相同列的数据并在一起(包括重复数据) select id,name from t_account union all (select id,name from t_account) -- 表1 id book author price -- 表2 id boke author shijian create table t_book( id int(4) auto_increment primary key, book varchar(50) not null, author varchar(20), price decimal(5,2) ) insert into t_book values (null,'西游记','孙菁',5.5),(null,'红楼梦','刘权国',5.9),(null,'水浒','马贵花',72.5),(null,'金瓶梅','唐启芳',52.5) create table t_boke( id int(4) auto_increment primary key, boke varchar(50) not null, author varchar(20), shijian date ) insert into t_boke values (null,'山海经','孙菁',now()),(null,'百年孤独','刘权国',now()),(null,'神话','马贵花',now()),(null,'孝庄秘史','唐启芳',now()) select author,book from t_book where author = '刘权国' union select author,boke from t_boke where author = '刘权国'
-- java.sql.Date-->String--->SimpleDateFormat-->java.util.Date select now(),CURRENT_DATE(),CURRENT_TIME(),dayname(now()); -- varchar 拼接 + select LOWER(CONCAT('A','B','c')) select UPPER(CONCAT('A','B','c')) -- 替换 replace(字符串,要替换的字符串from,替换结果to) select REPLACE("ABCDDE","AB","ab") select TRIM(" abcde ") # 只能删前|后的空格 -- char固定长度 数组 varchar可变长度 list text 长文本二进制 create table test( name char(255) primary key, text varchar(20) )charset=utf8; insert into test values('A','B') select length(name) from test;