sql复习语句--只作为复习,不作为研究

本文详细介绍了SQL的基础查询、更新、插入与删除操作,涵盖了去重、排序、限制查询结果等功能。同时深入探讨了高级SQL特性,包括表结构修改、联合查询、子查询、约束设置、索引创建及函数应用等,适合初学者与进阶读者。

SQL:

select(distinct)--查询(去重)desc 降序,ASC升序--默认

update--更新数据

delete

insert into

高级SQL:

添加列、删除列--alter add、drop

    ALTER TABLE table_name ADD column_name datatype--加类型

    ALTER TABLE table_name  DROP COLUMN(列) column_name

limit--select * from table limit 2-->返回前2条消息

select top 20 percent * from table-->返回前20%

like--一般查找姓名

in(要取的值)

(not)between--->where (属性) between 1 and 20---该属性取值1-20

join--(一共7种----常用的有左、右、全连接)--有join就没有where,用on代替

union--合并结果集,前提是列一一对应

select into --将一个表查询的结果转存到另一张表

     例:select * into new_table from old_table where...

约束:

1、primary key--(一般可能对应auto_increment自增)

2、foreign key--外键约束:

     建表时:foreign key  (当前表中的外键) references  要关联的表(关联表的属性)--foreign key (p_id) references Person(p_id)

    改表时:alter table 表名 add foreign key (当前表中的外键) references  要关联的表(关联表的属性)--foreign key (p_id)              references Person(p_id)

3、check约束:用于列值的范围

    在建表时:最后加check(p_id>0)

    在alter表时:alter table 表名 add cheak(p_id>0)

    撤销check约束:

    alter table 表名 drop cheak(check名称)--mysql中

    alter table 表名 drop constraint(check名称)--other中

4、默认约束default

    create时:直接在字段后面加 如:city varchar(255) default "北京"(defaule getdate()--系统值也可以默认)

    alter时:alter table 表名 alter city set default "北京"--mysql中,其他的也不一样,此处略

索引:create index(聚集索引 and 非聚集索引)--主键也是一种索引

例: create (unique) index index_name on table_name(column_name--列名,多个用,隔开)

                    唯一索引(对应上面语句的unique)

函数:

min()

max()

sum()

count()

avg()

group by()

order by()

round()--将值取位--round(100/3,2)--取小数点后俩位,不取小数点则不写第二个参数

case when 范围  then 1 else 0 end 当在此范围中为1,否则为0

   例--case when a.s_score>=70 and a.s_score<80 then 1 else 0 end

having--where关键字无法与聚合函数一起使用,个人理解可以当做where中的where

having count(c_id)=(select count(c_id) from Score where s_id='01')---在where中使用

其他:is  not null

 

 

 

评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值