oracle的create、alter、update、insert into 语法模板
Oracle create、alter、update、insert into 常用SQL语句汇总和解析。
文章目录
前言
常用Oracle SQL语句简单归结。
语法符号:花括号表示必选项,方括号可选项,|表示分隔符。
一、create table
创建一个表一般包括:字段 、数据类型、字段约束三个要素。
语法结构:
create table {table_name}
(
column1 datatype [constraint],
column2 datatype [constraint],
...
primary key (column1)
)
常用数据类型
| 序号 | 名称 | 说明 |
|---|---|---|
| 1 | varchar2 | 可变长字符型 |
| 2 | number(3,1) | 数字型 |
| 3 | date | 日期 |
常用oracle约束
| 序号 | 名称 | 说明 |
|---|---|---|
| 1 | not null | 不为空 |
| 2 | unique | 唯一,但可为空值 |
| 3 | default | 默认值 |
| 4 | check | 检查 |
| 5 | primary key | 主键 |
| 6 | foreign key | 外键 |
| 7 | index | 索引 |
二、alter table
修改一个表一般包括:添加字段、删除字段、修改字段和改字段名。
alter table{table_name}
{[ add | drop column | modify | rename column old_column to new_column ]}
--修改表名 将GJB_SXSS改名为GJB_XSXX
alter table GJB_SXSS rename to GJB_XSXX;
--添加字段
alter table {table_name} add {column_name datatype} [default] [constraint]
--删除字段
alter table {table_name} drop column {column_name}
--修改字段,默认值,约束
alter table {table_name} modify column_name [datatype] [default] [constraint]
--修改字段名
alter table {table_name} rename column {old_name} to {new_name}
--例如
alter table gzl02_temp rename column id to "gzl02id";
--删除唯一性约束
alter table {table_name} drop unique(column_name)
--添加主键
alter table {table_name} add primary key (column_name)
--添加外键,自定义约束名FK_1
alter table {table1} add constraint FK_1 foreign key (table1_column1)
references table2(table2_column1)
三、update table
3.1 单表
只涉及一张表,根据条件更新一个或多个字段。
--根据条件更新字段
update table
set column1 = '',column2=''
where
3.2 多表(根据其他表或结果集更新)
--根据其他表或结果集更新
update table t1
set (t1.column1,t1.column2)=
(select t2.column1,t2.column2
from table2 t2
where t2.column3=t1.column3)
where
exists
(select t2.column1,t2.column2
from table2 t2
where t2.column3=t1.column3)
--这里在主句也添加where exists相同条件,是为了防止插入空值错误,是一种健壮性写法。
注意oracle 不支持 update 连接表的操作,不能按照select多表查询形式来完成更新操作。
比如:有两张,成绩总表zcjb和成绩表cjb,现在实现把cjb更新到zcjb。
可以理解老师提交一次成绩后生成zcjb,现在发现部分成绩错误,需要把zcjb部分数据改成cjb的数据。
不能写成以下形式
update zcjb z,cjb c
set z.cj=c.cj
where
1=1
and z.xh=c.xh
而是写成
update zcjb z
set z.cj=(select c.cj from cjb c where c.xh=z.xh)
where
exists (select 'x' from cjb c where c.xh=z.xh)
如果是更新多个字段,比如同时更新平时成绩和期末成绩则写成
update zcjb z
set (z.pscj,z.qmcj)=
(select c.pscj,c.qmcj from cjb c where c.xh=z.xh)
where
exists (select 'x' from cjb c where c.xh=z.xh)
3.2 多表(根据其他表或结果集更新)取首行
update table t1
set (t1.column1,t1.column2)=
(select t2.column1,t2.column2
from table2 t2
where t2.column3=t1.column3 and rownum = 1 )
where
exists
(select 'x'
from table2 t2
where t2.column3=t1.column3 and rownum = 1 )
这里补充说明一下rownum机制。从语句执行顺序来看,
1、select t2.column1,t2.column2 from table2 t2 where t2.column3=t1.column3筛选出数据。
2、and rownum =1 对结果集编号并筛选行编号为1的结果集。
并不是 t2.column3=t1.column3 and rownum = 1 一起执行。
这样你就明白当我们查询出来结果集是空的,这里就会发生错误,因为无法对空结果集编号。
有关SQL语句执行顺序可以看下学习select语句的体会
四、insert into
插入数据包括,插入一行、多行和插入查询数据,形式有所不同。
--插入一行
insert into
table_name(column1,column2)
values
('','');
--插入多行
insert all
into table_name(column1,column2)
values('','')
into table_name(column1,column2)
values('','')
select count(*) from dual;--这行不能少,需要一条查询语句,查询结果不重要(表随意)。
--插入查询数据
insert into table1(table1_column1,table1_column2)
select table2_column1,table2_column2 from table2;
总结
SQL语句一定不要刻意去记忆。
SQL是结构化查询语言,所谓结构化就是固定语法结构,固定的词汇搭配,目的是告诉计算如何操作,另外语言的特点就是多练就会掌握。
| 序号 | 操作命令 | 操作对象 | 操作命令 | 操作对象 |
|---|---|---|---|---|
| 1 | create table | 表名(字段 数据类型 约束) | ||
| 2 | alter table | 表名 | add | 字段 |
| 3 | update table | 表名 | set | 字段 |
| 4 | insert into table | 表名(字段1,字段2) | values(’ ‘,’ ') |

更多推荐

所有评论(0)