Oracle课堂实验一“表的使用”代码。

时间:2024-03-25 17:33:50

--创建本地管理表空间CustomerTBS
CREATE TABLESPACE CustomerTBS
         DATAFILE 'd:\Oracle11\product\11.2.0\oradata\CustomerTBS.dbf' SIZE 130M
EXTENT MANAGEMENT LOCAL AUTOALLOCATE;

--创建用户ORACLEUSER,指定默认表空间为CustomerTBS,在该表空间的配额10MB
create user ORACLEUSER identified by 514224717
default tablespace CustomerTBS
quota 10M on CustomerTBS;

--授予用户创建表的权限
grant create table to ORACLEUSER;

--为了登录,为用户创建用户登录权限
grant connect,resource to ORACLEUSER;

--CustomerType表的创建
create table CustomerType(
    TypeId number(10) primary key,
    TypeName varchar2(20)
  )
--Customer表的创建
CREATE TABLE Customer(
    Id number(10) primary key,
    name varchar2(20) not null,
    pwd varchar2(20) default 111111,
    type number(10) constraint FK_Customer references CustomerType(TypeId)
    );
   
--往CustomerType表中插入数据
insert into CustomerType values(1,'普通客户');
insert into CustomerType values(2,'中级客户');
insert into CustomerType values(3,'高级客户');

--往Customer表中插入数据
insert into Customer values(1,'张三','514224717',1);
insert into Customer values(2,'李四','514224717',2);
insert into Customer values(3,'王五','514224717',3);

--修改数据
update CustomerType set TypeName='初级客户' where TypeId='1';
update Customer set name='张二' where id='1';

--删除数据
delete from customer where name='张二';
delete from customertype where typename='初级客户';

--用子查询创建表VIPCustomer(Id,name,pwd,type),将Customer表中type为000000的数据复制到此表中
create table VIPCustomer(Id,name,pwd,type) as select Id,name,pwd,type from customer;
insert into VIPCustomer(Id,name, pwd,type) (select Id, name, pwd, type from Customer where type='000000');

--修改表结构,为Customer表增加birthday列,修改列、删除列。
alter table Customer
add(birthday date);
alter table Customer rename Column type to typename;
alter table Customer drop column type;