MySQL补充

1.序列

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;

2.索引

  • 特点:唯一

  • 做查询时,为什么用主键?优点:查询效率高;

  • 缺点:索引文件,占用磁盘空间

    -- 索引
    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='语文'
    

3.视图

sql查询后的结果是临时表,视图也是一张临时表。

  • 视图面试题

4.联合主键

一张表中有多个主键列

需求:创建成绩表,分别存储学生编号、科目名称,描述一个学生一门课程的成绩。约束一个学生在输入成绩时,相同科目,只能输入一次成绩,重复数据拒绝插入

-- 主键是唯一的,表中只有一个主键的时候,重复的主键数据插不进去。联合主键,多个列联合称为主键,组成映射关系,当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

5.in all any some

  • 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)
      

6.exists 与 in

  • 用作子查询

    -- exists 存在   相关子查询(前提条件) 如果相关子查询有数据,exists返回true,反之返回false
    select score from t_score1 where exists(select score from t_score1  where subject_name='PE')
    

7.distinct 、并集:union[distinct] 、union all

-- 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 = '刘权国'

8.时间、字符串类型

-- 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; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值