Oracle数据库之Oracle基础操作
小标 2018-08-20 来源 : 阅读 1473 评论 0

摘要:本文主要向大家介绍了Oracle数据库之Oracle基础操作,通过具体的内容向大家展现,希望对大家学习Oracle数据库有所帮助。

本文主要向大家介绍了Oracle数据库之Oracle基础操作,通过具体的内容向大家展现,希望对大家学习Oracle数据库有所帮助。

Oracle的操作步骤
--一、基本操作
--1.创建表空间 2.创建用户 3.为用户分配表空间 
--4.对用户进行授权 5.进行基本操作,例如:创建表,创建视图等等

--每一步具体的操作
--1.创建表空间
-- create tablespace tablespace_name datafile '你想要将表空间存放到的路径' size 50M //size 50M是你自定义表空间的大小

--2和3.创建用户并为用户分配表空间
-- create user username identified by 你的密码 default tablespace tablespace_name;
--4.给用户授权
-- grant (权限名/想要授予用户某个角色的所有权限) to username;
-- *如果想要单个权限授权,记得授权 session 不然无法连接到你创建的用户//grant create session to username;
-- 例如:grant create table to username;//这样只授权给用户创建表的权限。 
-- 若想要一次性授权完毕//grant dba to username;
--接下来你就可以进行创建表等操作了;


--二、创建表的过程中注意的事情
--1.约束:主键约束(primary key),非空约束(not null),外键约束(foreign key),唯一约束(unique),检查约束(check);
--外键创建:alter table table_name add constraint FK_NAME(自己随便起) foreign key(外键列名) references 源表(被引用的列);

--唯一约束创建:alter table table_name add constraint UNIQUE_NAME(自己随便起) unique(想要约束的列);

--主键约束(可以单独创建,也可以直接在创建表的时候声明)
-- alter table table_name add constraint PK_NAME(自己随便起) primary key(主键列名);

--检查约束:alter table table_name add constraint CHK_NAME(自己随便起) check(想要约束的列名 in('约束条件','约束条件'));
--例如: alter table table_name add constraint CHK_NAME check(SEX in('男','女'));

 

--删除约束
--alter table table_name drop constraint 外键名;


--三、新增,更新,删除

-----1.新增
-----create table table_name as select * from 源表//根据结果集创建表
-----insert into table_name('列名','列名','列名') values('','','');


-----2.更新
-----update table_name set 列名 = 值,列名 = 值 where 条件语句;

-----3.删除
-----Deleete from table_name where 条件
-----Truncate table table_name 消除表中所有数据,不能恢复,但是可以通过日志文件恢复

----------------------------------------------------------------------------------------------------------
-------创建表的例子
create table sporter(
sporterid varchar2(20) primary key,
name varchar2(20) not null,
gender char(4) check(gender in ('男','女')),
department char(20) not null);

 

 

create table item(
itemid char(20) primary key,
itemname varchar2(20),
location varchar2(20));

 

 

create table grade(
sporterid,
itemid,
mark int check(mark in(6,4,2,0,null)),
constraint pk_grade primary key(sporterid,itemid),
foreign key(sporterid) references sporter (sporterid),
foreign key(itemid) references item(itemid));

 


insert into sporter (sporterid ,name ,gender ,department )values('1001','李明','男','计算机系');
insert into sporter (sporterid ,name ,gender ,department )values('1002','张三','男','数学系');
insert into sporter (sporterid ,name ,gender ,department )values('1003','李四','男','计算机系');
insert into sporter (sporterid ,name ,gender ,department )values('1004','王二','男','物理系');
insert into sporter (sporterid ,name ,gender ,department )values('1005','李娜','女','心理系');
insert into sporter (sporterid ,name ,gender ,department )values('1006','孙丽','女','新闻系');

 


insert into item(itemid ,itemname ,location )values('x001','男子五千米','一操场');

insert into item(itemid ,itemname ,location )values('x002','男子标枪','一操场');

insert into item(itemid ,itemname ,location )values('x003','女子跳高','二操场');

insert into item(itemid ,itemname ,location )values('x004','女子三千米','二操场');

 


insert into grade(sporterid,itemid ,mark )values('1001','x001',6);
insert into grade(sporterid,itemid ,mark )values('1002','x001',4);
insert into grade(sporterid,itemid ,mark )values('1003','x001',2);
insert into grade(sporterid,itemid ,mark )values('1004','x001',0);
insert into grade(sporterid,itemid ,mark )values('1001','x003',4);
insert into grade(sporterid,itemid ,mark )values('1002','x003',6);
insert into grade(sporterid,itemid ,mark )values('1004','x003',2);
insert into grade(sporterid,itemid ,mark )values('1005','x004',6);
insert into grade(sporterid,itemid ,mark )values('1006','x004',4);    

本文由职坐标整理并发布,希望对同学们学习Oracle有所帮助,更多内容请关注职坐标数据库Oracle数据库频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved