一、安装服务端(自选是否需要安装客户端)
注意:如果安装客户端的话,记得建库完成后将服务端安装路径下的Oracle\product\10.2.0\db_1\network\ADMIN\tnsnames.ora文件
复制到客户端安装路径
Oracle\product\10.2.0\client_1\network\ADMIN下。
这样客户端软件Enterprise Manager Console中即可自动识别本地数据库。
二、创建监听、创建数据库实例
(1)Net Manager创建监听,一般默认“下一步”即可完成创建。
(2)Net Configuration Assistant创建实例,设置基础表空间和重做日志组等。
表空间参考配置如下(按需设置):
UNDOTBS1表空间,双击undotbs01.dbf,关闭自动扩展选项,并在General标签中定义其大小为固定的4096M。按照同样的方式设置SYSTEM和SYSAUX表空间大小为2048M,TEMP表空间为4096M。
重做日志组参考配置如下:
修改为100-200M,避免在数据库日志中频繁出现checkpoint not complete警告。
三、创建表空间、用户、赋权
// 创建表空间(确保数据文件路径存在)
create tablespace MAINSPACE
datafile 'D:\OracleData\cppc\mainspace.dbf'
size 1024M
autoextend on next 100M maxsize 4096M;
// 创建用户,指定默认和临时表空间
// 如果不想用TEMP表空间可以自行创建临时表空间,方法同创建表空间
create user cppc identified by "xxxxx"
default tablespace MAINSPACE
temporary tablespace TEMP;
// 给用户赋权
grant connect,resource,dba to cppc;
四、创建表
用SQLPlus / DBeaver/ PLSQL Developer等方式登录数据库。
利用SQL语句创建表,创建在登录用户下,该用户前面已经指定了表空间。
参考SQL如下:
BEGIN
-------------------------
-- Create Customers table
-------------------------
CREATE TABLE Customers
(
cust_id char(10) NOT NULL ,
cust_name char(50) NOT NULL ,
cust_address char(50) NULL ,
cust_city char(50) NULL ,
cust_state char(5) NULL ,
cust_zip char(10) NULL ,
cust_country char(50) NULL ,
cust_contact char(50) NULL ,
cust_email char(255) NULL
);
--------------------------
-- Create OrderItems table
--------------------------
CREATE TABLE OrderItems
(
order_num int NOT NULL ,
order_item int NOT NULL ,
prod_id char(10) NOT NULL ,
quantity int NOT NULL ,
item_price decimal(8,2) NOT NULL
);
----------------------
-- Create Orders table
----------------------
CREATE TABLE Orders
(
order_num int NOT NULL ,
order_date date NOT NULL ,
cust_id char(10) NOT NULL
);
------------------------
-- Create Products table
------------------------
CREATE TABLE Products
(
prod_id char(10) NOT NULL ,
vend_id char(10) NOT NULL ,
prod_name char(255) NOT NULL ,
prod_price decimal(8,2) NOT NULL ,
prod_desc varchar(1000) NULL
);
-----------------------
-- Create Vendors table
-----------------------
CREATE TABLE Vendors
(
vend_id char(10) NOT NULL ,
vend_name char(50) NOT NULL ,
vend_address char(50) NULL ,
vend_city char(50) NULL ,
vend_state char(5) NULL ,
vend_zip char(10) NULL ,
vend_country char(50) NULL
);
----------------------
-- Define primary keys
----------------------
ALTER TABLE Customers ADD CONSTRAINT PK_Customers PRIMARY KEY (cust_id);
ALTER TABLE OrderItems ADD CONSTRAINT PK_OrderItems PRIMARY KEY (order_num, order_item);
ALTER TABLE Orders ADD CONSTRAINT PK_Orders PRIMARY KEY (order_num);
ALTER TABLE Products ADD CONSTRAINT PK_Products PRIMARY KEY (prod_id);
ALTER TABLE Vendors ADD CONSTRAINT PK_Vendors PRIMARY KEY (vend_id);
----------------------
-- Define foreign keys
----------------------
ALTER TABLE OrderItems
ADD CONSTRAINT FK_OrderItems_Orders FOREIGN KEY (order_num) REFERENCES Orders (order_num);
ALTER TABLE OrderItems
ADD CONSTRAINT FK_OrderItems_Products FOREIGN KEY (prod_id) REFERENCES Products (prod_id);
ALTER TABLE Orders
ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (cust_id) REFERENCES Customers (cust_id);
ALTER TABLE Products
ADD CONSTRAINT FK_Products_Vendors FOREIGN KEY (vend_id) REFERENCES Vendors (vend_id);
END;