SQL--基础语句2

列名 数据类型 默认值 约束
create table customer(
id number(7) primary key,
name char(25) not null,
phone varchar2(20) unique
)

删除表
drop table customer

主键约束可以写在列后,也可以写在所有列之后。列级约束和表级约束
primary key 描述一列为主键,非空且唯一 not null 非空 unique 唯一
联合主键只能在表级中声明,无法再列级约束声明
create table customer(
id number(7) ,
name char(25) ,
phone varchar2(20),
emp_id number(7),
primary key(id),
foreign key(emp_id) references s_emp(id),
unique(name)
)

检查型约束,只能是指定的几个值
create table student(
gender varchar(5) check(gender in(‘男’,’女’))
)

表可以通过子查询创建(复制一张表)
通过子查询创建表,只会保留非空约束。即主键失效了
create table emp1(
id number(7),
last_name varchar2(25),
salary number(7)
)

只复制结构,不要数据
create table emp2
as
select *
from s_emp
where 1=0;

设置默认值 default
create table student2(
name varchar2(10) default ‘李狗蛋’,
age number(7) default 0
)

添加新的列
alter table emp1
add(
manager_id number(7)
)

删除表中列
alter table emp1
drop column manager_id

通过字典,系统表查询用户表的信息
select table_name,column_name,date_type,data_length
from user_tab_columns
order by table_name;

用户创建的表
select table_name,num_rows
from user_tables;

级联删除,当删除外键列时删除,会删除外键列所关联的信息
create table my_order(
id number(7) primary key,
name varchar2(20) not null,
emp_id number(7)references my_emp(id)
on delete cascade
);
清空表 清空数据
delete from
1 DML
2 删除数据,但是不清空表空间
移到回收站

truncate
1 DDL
2 删除数据,清空表空间
清空回收站

序列
create sequence seq1
increment by 2
start with 10;

获取序列的下一个值
select seq1.nextval
from dual;

视图 需要权限,通过grant create any view to briup(管理员权限)
create view v_emp
as
select id,last_name,salary
from s_emp

简单视图 直接通过单表条件查询,允许直接在视图上进行增,删,修改数据。会关联到原表中,更改原表中数据也会关联到视图中。
update v_emp
set last_name = ‘hello’
where id=99;
复杂视图 通过group by 或者 连接查询, 在视图上只能进行查询。但原表中的更改会影响视图
视图没有 alter命令,修改视图使用create or replace view
create or replace view v_emp
as
select e.last_name,e.id,d.name,e.salary
from s_emp e,s_dept d
where e.dept_id = d.id

with read only 只允许读
with check option 创建视图时,所用到的列不允许修改

行列互换
select name as “姓名”,
max(case subject when ‘语文’ then score else 0 end) as “语文” ,
max(case subject when ‘英语’ then score else 0 end ) as “英语” ,
max(case subject when ‘数学’ then score else 0 end ) as “数学”
from student
group by name
order by name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值