1.linux下的Oracle
(1)nolog方式登录安全性较高
sqlplus /nolog
(2)连接到Oracle空闲历程
conn / as sysdba
(3)启动/关闭数据库
SQL> startup
shutdown
(4)Scott登录
SQL> @$ORACLE_HOME/rdbms/admin/utlsampl.sql
SQL> conn scott/tiger
(5)查看当前用户
SQL> show user
2.关于scott,sysdba,system等
scott:演示用户,学习oracle使用的
sysdba:拥有最高的系统权限,装载数据库,打开数据库,登录后用户是sys
sysoper:启动,关闭数据库,登录后的用户是public
sys:所有oracle的数据字典的基表和视图都存放在sys用户中,拥有dba,sysdba,sysoper等角色或权限,是oracle权限最高的用户
system:只能用普通用户身份登录(除非授予了系统权限),存放一级的内部数据
3.基本语句
(1)创建数据库
create database [数据库名]
删除数据库
drop database [数据库名]
(2)创建表
例:
create table student(
sid number primary key not null,
sname varchar(20) not null,
age number
);
create table sc(
sid number,
score number,
foreign key (sid) references student(sid) #单列的外键,必须是另一个表的主键
);
设置多列主键外键
constraint [主键名] primary key (列1,列2...) #主键
constraint [外键名] foreign key (列1,列2) reference [表名](列1,列2) #外键
(3)修改表
alter table [表名] add (列,数据类型 not null); #添加一列
alter table [表名] modify (列,数据类型); #更改列
alter table [表名] rename column (旧列名) to (新列名); #更改某一列的列名
alter table [表名] drop column (列名); #删除某一列
alter table (旧表名) rename to (新表名); #更改表名
#修改某一列为主键
alter table [表名] add constraint [键名] primary key (列名);
#设置多列为主键
alter table [表名] add constraint [键名] primary key (列1,列2...);
#添加外键
alter table [表名] add constraint [键名] foreign key (列名) references [表名](列名);
#添加多个外键
alter table [表名] add constraint [键名] foreign key (列1,列2...) references [表名](列1,列2...);
(4)删除表
drop table [表名];
(5)查看表结构
DESC [表名];