SQL语句之面试

这几天面试,发现自己的sql语句还有许多不足之处,于是想持续汇总面试时的sql语句,希望能帮到各位看官。

1.创建数据库:
CREATE DATABASE database-name;
2.删除数据库
drop database dbname;
3.创建新表:
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],…)
或者根据旧表创建新表:
A:create table_new like table_old;
B:create table_new as select col1,col2,…from table_old definition only;
4.删表
drop from table_name;
5.增加列
alter table tablename add colnum 列名 数据类型;
6.添加主键:
alter table tablename add primary key(col);
7.创建索引:
1 create [unique] index idxname on tabname(col….);
8.删除索引:
drop index idxname;
9.几个简单的基本的sql语句:
选择:
select * from table1 where 范围
插入:
insert into table1(field1,field2) values(value1,value2)
删除:
delete from table1 where 范围
–更新:
update table1 set field1=value1 where 范围
–查找:
select * from table1 where field1 like ’%value1%’ —like的语法很精妙,查资料!
–排序:
select * from table1 order by field1,field2 [desc]
–总数:
select count as totalcount from table1
–求和:
select sum(field1) as sumvalue from table1
–平均:
select avg(field1) as avgvalue from table1
–最大:
select max(field1) as maxvalue from table1
–最小:select min(field1) as minvalue from table1
#增加用户账号 设置密码;
create user tom identified by 123;
create user 用户名 identified by 密码 default tablespace 表空间表;
create user study identified by study default tablespace data_test;
授予权限
grant connect ,resource to tom; 链接权限
grant select on scott.emp to tom;
GRANT SELECT ON SCOTT.EMP TO U1;
从角色收回权限:
revoke from ;

表名guest 根据要求写出sql语句
Accounts Details Date Money Class
S0001 房费 2010-01-01 280 001
S0001 酒水 2010-01-02 120 001
S0001 房费 2010-01-08 300 003
S0002 酒水 2010-01-29 50
S0003 房费 2010-01-31 180 002
S0004 房费 2010-02-01 230 001
S0005 酒水 2010-02-01 100
S0005 房费 2010-02-02 128 001

查询出房费都大于200的帐号。
Select accounts form guest where money >’200’ and Detail =‘房费’;
查询出1月份每个帐号酒水和房费的总金额。
select accounts,count(details) from 表名 where date>=to_date(‘2010-01-01’,‘yyyy-mm-dd’) and date<=to_date(‘2010-01-31’,‘yyyy-mm-dd’) group by account;
删除1月份班次为空的记录。
Delete form guest where date=>’2010-01-01’ and date=<’2010-01-31’and class is null;
将不是房费的记帐代码的班次都更改为‘001’。
update guest set class = ‘001’ where details not in (‘房费’);
查询出消费都大于100的帐号。
Select distinct accounts form guest accounts not in (select distinct accounts form guest money <’100’);

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值