oracle创建表空间,用户,创建表

连接数据库:

sqlplus / as sysdba
startup
shutdown immediate

创建用户:

查看oracle现在的状态 ,状态为 OPEN 则正常,表
select status from v$instance;
查询所有账户
SELECT * FROM ALL_USERS;
使用以下命令获取安装的数据库的服务名称。
select value from v$parameter where name='service_names';
命令操作
创建公用用户
说明:公用用户需要使用C##或者c##作为该用户名的开头
注意:sysdba身份登录进入数据库
如果使用传统创建用户的方法会报错
create user C##ogg identified by ogg;
-- 修改密码
alter user C##ogg identified by ogg;
--首先查询一下用户的profile的类型
select username ,profile from dba_users;
--查看制定概要文件(默认为DEFAULT)的密码有效期:
select  * from dba_profiles where profile='DEFAULT' and resource_name='PASSWORD_LIFE_TIME';
--然后将密码的有效期有180天设置为“无限制”;
ALTER PROFILE  DEFAULT  LIMIT PASSWORD_LIFE_TIME UNLIMITED;
-- 查询所有用户
SELECT * FROM ALL_USERS;
--  给用户授予权限
grant create session to C##ogg;
grant connect,resource to C##ogg;
GRANT ALTER SYSTEM TO C##ogg;
-- 解除锁定
alter user C##ogg account unlock;
commit;

查看表空间 SELECT NAME FROM V$DATAFILE;

 创建表空间 oggspace:

表空间名称 datafile:指定dbf文件在linux的存放地址 size:可以理解为文件初始化大小 autoextend:当size堆满后,每次自动扩展的大小

create tablespace oggspace datafile '/u01/app/oracle/oradata/xe/ogg.dbf' size 50M autoextend on next 1024M maxsize 10240M;

创建之前,可以先查看一下当前数据库的表空间

select tablespace_name from dba_data_files;

创建用户,如果已有用户可以不进行操作

create user C##ogg identified by ogg default tablespace oggspace;
授权用户

grant create session to C##ogg;
grant create table to C##ogg;
grant create tablespace to C##ogg;
grant create view to C##ogg;
grant connect,resource,dba to C##ogg;
alter user C##ogg default tablespace oggspace;
#设置系统权限
GRANT ALTER SYSTEM TO C##ogg;

创建表与主键自增

--设置自增序列,名称为"seq_userinfo",名字任意命名
drop if sequence pk_si_sno;
create sequence pk_si_sno
 increment by 1--每次+1
 start with 1--从1开始
 nomaxvalue--不限最大值
 nominvalue--不限最小值
 cache 20;--设置取值缓存数为20
drop TABLE C##OGG.student_info;
-- 创建表: student_info 属主: C##OGG (默认当前用户)
create table C##OGG.student_info (
  sno         number(10) DEFAULT pk_si_sno.nextval,
  sname       varchar2(10),
  sex         varchar2(6),
  create_date date,
  update_date date
);
-- 添加注释
comment on table C##OGG.student_info is '学生信息表';
comment on column C##OGG.student_info.sno is '学号';
comment on column C##OGG.student_info.sname is '姓名';
comment on column C##OGG.student_info.sex is '性别';
comment on column C##OGG.student_info.create_date is '创建日期';
comment on column C##OGG.student_info.update_date is '更新日期';



-- 语句授权,如:给 hr 用户下列权限
grant select, insert, update, delete on C##OGG.student_info to hr;


-- 插入
insert into C##OGG.student_info ( sname, sex, create_date, update_date)
values ('张三', '男', sysdate, sysdate);
insert into C##OGG.student_info ( sname, sex, create_date, update_date)
values ( '李四', '女', sysdate, sysdate);
insert into C##OGG.student_info ( sname, sex, create_date, update_date)
values ( '王五', '男', sysdate, sysdate);
-- 修改
update C##OGG.student_info si
   set si.sex = '女'
 where si.sno = 3;
-- 删除 
delete C##OGG.student_info si where si.sno = 1; 
-- 提交
commit; 
-- 查询
select * from C##OGG.student_info;

猜你喜欢

转载自blog.csdn.net/qq_40408317/article/details/128234650