-- 数据库连接命令
su - oracle
-- 登录数据库
-- 数据库管理员权限登录
sqlplus / as sysdba;
-- 使用用户名密码登录
conn 用户名/密码;
-- 查看表空间的路径
select name from v$datafile;
-- 创建表空间
CREATE TABLESPACE oracle_test LOGGING DATAFILE '/u01/app/oracle/oradata/oracle_test/oracletest.dbf' SIZE 100M AUTOEXTEND ON NEXT 32M MAXSIZE 500M EXTENT MANAGEMENT LOCAL;
-- 为表空间oracle_test创建用户test,密码为123456
create user test identified by 123456 default tablespace oracle_test;
-- 查询已创建的用户
select username from dba_users;
-- 为用户test授权
grant connect,resource to test;
-- 修改用户test的密码
alter user test identified by oracle123;
--当用户处于锁定状态时,需要解锁用户
alter user test account unlock;
-- 创建表
--student表+注释
create table student(
sno varchar2(3) not null,
sname varchar2(9) not null,
ssex varchar2(3) not null,
sbirthday date,
sclass varchar2(5),
constraint pk_student primary key(sno)
);
comment on column student.sno is '学号(主键)';
comment on column student.sname is '学生姓名';
comment on column student.ssex is '学生性别';
comment on column student.sbirthday is '学生出生年月日';
comment on column student.sclass is '学生所在班级';
-- 修改表ssex的字段属性;
alter table student (ssex varchar2(8));
-- 在表student中插入数据
insert into student(sno,sname,ssex,sbirthday,sclass) values(108,'jane','female',to_date('1995-09-01','yyyy-mm-dd'),95033);
insert into student(sno,sname,ssex,sbirthday,sclass) values(105,'kate','male',to_date('1992-10-02','yyyy-mm-dd'),95031);
insert into student(sno,sname,ssex,sbirthday,sclass) values(107,'elsa','female',to_date('1993-01-23','yyyy-mm-dd'),95033);
insert into student(sno,sname,ssex,sbirthday,sclass) values(101,'andrew','male',to_date('1990-02-20','yyyy-mm-dd'),95033);
insert into student(sno,sname,ssex,sbirthday,sclass) values(109,'lisa','female',to_date('1997-02-10','yyyy-mm-dd'),95031);
insert into student(sno,sname,ssex,sbirthday,sclass) values(103,'jack','male',to_date('1998-06-03','yyyy-mm-dd'),95031);
insert into student(sno,sname,ssex,sbirthday,sclass) values(212,'lily','male',to_date('1998-09-01','yyyy-mm-dd'),95033);
insert into student(sno,sname,ssex,sbirthday,sclass) values(245,'aimy','male',to_date('1989-10-02','yyyy-mm-dd'),95031);
insert into student(sno,sname,ssex,sbirthday,sclass) values(254,'rubo','female',to_date('1990-01-23','yyyy-mm-dd'),95033);
insert into student(sno,sname,ssex,sbirthday,sclass) values(248,'kevy','female',to_date('1978-02-20','yyyy-mm-dd'),95033);
insert into student(sno,sname,ssex,sbirthday,sclass) values(256,'bile','female',to_date('1979-02-10','yyyy-mm-dd'),95031);
insert into student(sno,sname,ssex,sbirthday,sclass) values(265,'dam','male',to_date('1996-06-03','yyyy-mm-dd'),95031);
--注:
1、插入数据报错:invalid character ,去掉分号。
2、插入数据报错value too large for column "QA"."STUDENT"."SSEX" (actual: 6)修改表字段ssex的属性,
alter table student (ssex varchar2(8));
3、用户被锁
SQL> conn SYSTEM
Enter password:
ERROR:
ORA-28000: the account is locked
解决方法:
SQL> conn sys/sys as sysdba;
Connected.
SQL> alter user SYSTEM account unlock;
User altered.
SQL> alter user SYSTEM identified by 123456;
SQL> conn SYSTEM
Enter password:
Connected.
4、连接数据库发现监听服务有问题,报错如下:
ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
解决方法:
在sqlplus下使用conn /as sysdba连接之后再启动数据库就好了
SQL> oracle@ba4d475cde17:~$ sqlplus /nolog
SQL*Plus: Release 11.2.0.2.0 Production on Thu Oct 24 02:08:56 2019
Copyright (c) 1982, 2011, Oracle. All rights reserved.
SQL> conn / as sysdba;
Connected to an idle instance.
SQL> startup
ORACLE instance started.
Total System Global Area 601272320 bytes
Fixed Size 2228848 bytes
Variable Size 440405392 bytes
Database Buffers 155189248 bytes
Redo Buffers 3448832 bytes
Database mounted.
Database opened.
SQL> conn SYSTEM;
Enter password:
Connected.
--查看servicename,用管理员账号登录,sqlplus / as sysdba;使用如下命令查看
show parameter service;