数据库的一些基本sql语句

本文通过一系列SQL语句示例,涵盖了数据库的基本操作,包括创建与删除数据库、操作表、数据增删改查、联表查询、子查询、分组与聚合函数等。还涉及到了去重、模糊查询、索引、视图、数据库优化等内容,是学习SQL的实用教程。
摘要由CSDN通过智能技术生成

create database Test创建数据库
use Tset是进入这个数据库
drop table表名 删除一张表
delete *from 表名 删除一条记录
insert into 表名 values()增加一条数据
update 表名 set 列名=新数据 where 列名 = ‘条件’ 更新数据
select top 3 *from emp order by sal
distinct是去重
Union是去重 union all不去重联表
left join和join区别 left是左边即使没有右边对应 也会显示
select e.* from emp e where not exists(
select 1 from hist h where h.eid=e.eid
)
exists返回真假
identity是自动增长 创建主键需要
check是约束
desc是降序
asc是升序
top n是取前n名
like是模糊查询
是先执行order by 再取前三

select * from emp;
    -- * 表示所有的
    -- from emp 表示从emp表查询

select empno, ename from emp;

select ename, sal from emp;

select ename, sal*12 as "年薪" from emp;
        --as 可以省略 记住: "年薪" 不要写成'年薪' 也不要写成 年薪

select ename, sal*12 as "年薪", sal "月薪", job from emp;

select 888 from emp;
    --ok
    --输出的行数是emp表的行数  每行只有一个字段,值是888

select 5;  --ok 
           --不推荐    

select deptno from emp; --14行记录 不是3行记录


去重个查询
select distinct deptno from emp;  --distince deptno 会过滤掉重复的deptno
select distinct comm from emp;  --distinct也可以过滤掉重复的null  或者说如果有多个null 只输出一个
select distinct comm, deptno from emp;--把comm和deptno的组合进行过滤 
select deptno, distinct comm from emp;--error 逻辑上有冲突

select 10000 from emp; --14行记录


between查询
--查找工资在1500到3000之间(包括1500和3000)的所有的员工的信息
select * from emp 
    where sal>=1500 and sal<=3000
等价于
select * from emp 
    where sal between 1500 and 3000

--查找工资在小于1500或大于3000之间的所有的员工的信息
select * from emp 
    where sal<1500 or sal>3000
等价于
select * from emp 
    where sal not between 1500 and 3000

    in查询
    select * from emp where sal in (1500, 3000, 5000)
等价于
select * from emp 
    where sal=1500 or sal=3000 or sal=5000

select * from emp where sal not in (1500, 3000, 5000) --把sal既不是
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值