- In this section, you create a schema named ONLINE_SHOPPE. This schema portrays an online store that operates with a customer base and commodities. Information about customers is stored in the CUSTOMERS table, information about commodities is stored in the COMMODITIES table and order details are stored in the ORDERS table.
- The instance administrator
- A user with ADMIN privileges
- The object's owner
- System privilege: The right to perform a particular action on any object, such as, tables, views and indexes. Only the instance administrator or a user with the ADMIN privilege can assign or revoke system privileges.
- Object privilege: The right to perform a particular action on an object or to access another user's object. An object's owner has all object privileges for that object and can assign object privileges for that object to other database users.
- Create a table, a view, or an index that is owned by any user in the database
- Alter a table, a view, or an index in the database
- Drop a table, a view, or an index in the database
- Insert values into a table
- Create a foreign key dependency for a table
- Select from a table
- Update a table
- Table: Basic unit of data storage in a database. Within a table, data is stored in rows and columns. You define a table with a table name, a set of columns, a data type, and a width.
- Integrity constraints: Rules for columns in a table. You specify these rules to enforce data integrity within the columns for which they are defined. Basic constraints on Oracle Database include the following:
ORDER_ID column as the primary key COMMODITY_ID and CUSTOMER_ID as foreign keys UNITS and TOTAL_COST as NOT NULL values CHECK constraint on numeric columns to accept values greater than zero
Creating a User
- Database administrators perform many tasks. One of their more common tasks is creating database users and assigning them unique usernames. After users log in to the database with their username and password, they can issue database SQL statements to create objects, query objects, and manage the database.
Creating a user is a way to create a schema. In this section, you execute the CREATE USER statement to create and configure a database user.
Syntax: CREATE USER <USER> IDENTIFIED BY <password>;
CONNECT sys/<password> as sysdba;
CREATE USER online_shoppe IDENTIFIED BY online;
Assigning Privileges
- When multiple users access database objects, you can control the authorization of the objects with privileges. Privileges control whether a user can modify an object that is owned by another user. They are granted or revoked either by:
System privileges:
Object privileges:
- You use the GRANT statement to assign privileges to users and roles. To assign privileges, you must have been assigned either the ADMIN OPTION or the GRANT ANY PRIVILEGE system privilege.
Syntax: GRANT <grant_privilege> TO <user>;
GRANT CREATE SESSION to online_shoppe;
GRANT CREATE TABLE to online_shoppe;
GRANT UNLIMITED TABLESPACE to online_shoppe;
GRANT SELECT ANY TABLE to online_shoppe;
GRANT UPDATE ANY TABLE to online_shoppe;
GRANT INSERT ANY TABLE to online_shoppe;
GRANT DROP ANY TABLE to online_shoppe;
Creating Tables
- Before creating tables in the ONLINE_SHOPPE schema, you should understand the concepts of tables and integrity constraints.
In this section, you execute the CREATE TABLE statement to create tables.
Syntax: CREATE TABLE [schema.]table
(column datatype [DEFAULT expr][, ...]);
Perform the following steps to create the CUSTOMERS, COMMODITIES, and ORDERS tables in the schema.
CONNECT online_shoppe/online;
CREATE TABLE customers(
customer_id VARCHAR2(4),
customer_name VARCHAR2(20),
address VARCHAR2(60),
contact VARCHAR2(20),
CONSTRAINT cust_id_pk PRIMARY KEY(customer_id) );
CREATE TABLE commodities(
commodity_id VARCHAR2(4),
commodity_name VARCHAR2(20),
unit_price NUMBER(8,2) NOT NULL,
CONSTRAINT comm_id_pk PRIMARY KEY(commodity_id) );
order_id VARCHAR2(4),
customer_id VARCHAR2(4),
commodity_id VARCHAR2(4),
units NUMBER(8,2) NOT NULL,
total_cost NUMBER(8,2) NOT NULL,
CONSTRAINT ordr_id_pk PRIMARY KEY(order_id),
CONSTRAINT ordr_cust_fk FOREIGN KEY (customer_id)REFERENCES customers(customer_id),
CONSTRAINT ordr_comm_fk FOREIGN KEY (commodity_id)REFERENCES commodities(commodity_id),
CONSTRAINT check_unit CHECK(units > 0),
CONSTRAINT check_totl CHECK(total_cost > 0) );
Inserting, Modifying, and Deleting Records
- In this section, you manipulate the records in the tables that you created.
Syntax: INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);
Execute the following statements to insert data into the CUSTOMERS, COMMODITIES, and ORDERS tables.
INSERT INTO customers VALUES ('C001', 'BDAVIS', 'Boston', '650.551.4876');
INSERT INTO customers VALUES ('C002', 'SSTEPHEN', 'ST.Louis', '650.501.9321');
INSERT INTO customers VALUES ('C003', 'DCARTER', 'California', '650.507.6632');
INSERT INTO commodities VALUES ('M001', 'DVD Player', 109);
INSERT INTO commodities VALUES ('M002', 'Cereal', 03);
INSERT INTO commodities VALUES ('M003', 'Scrabble', 29);
INSERT INTO orders VALUES ('R001', 'C003', 'M002', 50, 150);
INSERT INTO orders VALUES ('R002', 'C001', 'M003', 30, 87);
INSERT INTO orders VALUES ('R003', 'C003', 'M001', 6, 654);
UPDATE commodities SET unit_price = 129 WHERE commodity_name = 'DVD Player';
DELETE FROM orders WHERE order_id = 'R001';
Undoing and Saving Records
- In this section, you use the COMMIT and ROLLBACK statements to change data permanently. You use the ROLLBACK statement to undo the work that was performed in your current transaction and you use the COMMIT statement to save the work that was performed in your current transaction.
COMMIT;
DELETE FROM orders WHERE order_id = 'R002';
SELECT * FROM orders;
The output shows that the record was deleted successfully.
ROLLBACK;
SELECT * FROM orders;
Note: You cannot undo transactions after you save them permanently with the COMMIT statement.
Removing Tables
- In this section, you execute the DROP TABLE statement to remove a table and its data from the database.
Syntax: DROP TABLE <table>;
DROP TABLE customers;
An error message is displayed because of the referential integrity constraint on the CUSTOMER_ID column.
DROP TABLE customers CASCADE CONSTRAINTS;
Revoking Privileges
- In this section, you execute the REVOKE statement to revoke user and role system privileges. To revoke a system privilege or a role, you must be assigned the privilege with the ADMIN OPTION.
Syntax: REVOKE <revoke_privilege> FROM <user>;
CONNECT sys/<password> as sysdba;
REVOKE CREATE SESSION FROM online_shoppe;
CONNECT online_shoppe/online;
You cannot connect because you do not have the CREATE SESSION privilege.