0% found this document useful (0 votes)
7 views

SQL Assignment

The document outlines SQL commands for creating a user and granting privileges, as well as creating various tables such as REGIONS, COUNTRIES, EMPLOYEES, and more, with specified constraints and foreign keys. It also includes examples of string, aggregate, analytical, and date/time functions, along with DML operations like merge statements and database administration tasks. Overall, it serves as a comprehensive guide for setting up a database schema and performing operations within it.

Uploaded by

Nikitha Pole
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQL Assignment

The document outlines SQL commands for creating a user and granting privileges, as well as creating various tables such as REGIONS, COUNTRIES, EMPLOYEES, and more, with specified constraints and foreign keys. It also includes examples of string, aggregate, analytical, and date/time functions, along with DML operations like merge statements and database administration tasks. Overall, it serves as a comprehensive guide for setting up a database schema and performing operations within it.

Uploaded by

Nikitha Pole
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

-----------------------------------------------------------------------------------------------------

Create User
-------------------------------------------------------------------------------------------------------
-- create new user
CREATE USER OT IDENTIFIED BY yourpassword;

-- grant priviledges
GRANT CONNECT, RESOURCE, DBA TO OT;

---------------------------------------------------------------------------
-- Execute the following statements to create tables
---------------------------------------------------------------------------
-- regions

CREATE TABLE REGIONS


(
region_id NUMBER GENERATED BY DEFAULT AS IDENTITY
START WITH 5 PRIMARY KEY,
region_name VARCHAR2( 50 ) NOT NULL
);
-- countries table
CREATE TABLE COUNTRIES
(
country_id CHAR( 2 ) PRIMARY KEY ,
country_name VARCHAR2( 40 ) NOT NULL,
region_id NUMBER , -- fk
CONSTRAINT fk_countries_regions FOREIGN KEY( region_id )
REFERENCES regions( region_id )
ON DELETE CASCADE
);

-- location
CREATE TABLE LOCATIONS
(
location_id NUMBER GENERATED BY DEFAULT AS IDENTITY START WITH 24
PRIMARY KEY ,
address VARCHAR2( 255 ) NOT NULL,
postal_code VARCHAR2( 20 ) ,
city VARCHAR2( 50 ) ,
state VARCHAR2( 50 ) ,
country_id CHAR( 2 ) , -- fk
CONSTRAINT fk_locations_countries
FOREIGN KEY( country_id )
REFERENCES countries( country_id )
ON DELETE CASCADE
);
-- warehouses
CREATE TABLE WAREHOUSES
(
warehouse_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 10
PRIMARY KEY,
warehouse_name VARCHAR( 255 ) ,
location_id NUMBER( 12, 0 ), -- fk
CONSTRAINT fk_warehouses_locations
FOREIGN KEY( location_id )
REFERENCES locations( location_id )
ON DELETE CASCADE
);

-- employees
CREATE TABLE EMPLOYEES
(
employee_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 108
PRIMARY KEY,
first_name VARCHAR( 255 ) NOT NULL,
last_name VARCHAR( 255 ) NOT NULL,
email VARCHAR( 255 ) NOT NULL,
phone VARCHAR( 50 ) NOT NULL ,
hire_date DATE NOT NULL ,
manager_id NUMBER( 12, 0 ) , -- fk
job_title VARCHAR( 255 ) NOT NULL,
CONSTRAINT fk_employees_manager
FOREIGN KEY( manager_id )
REFERENCES employees( employee_id )
ON DELETE CASCADE
);

-- product category
CREATE TABLE PRODUCT_CATEGORIES
(
category_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 6
PRIMARY KEY,
category_name VARCHAR2( 255 ) NOT NULL
);

-- products table
CREATE TABLE PRODUCTS
(
product_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 289
PRIMARY KEY,
product_name VARCHAR2( 255 ) NOT NULL,
description VARCHAR2( 2000 ) ,
standard_cost NUMBER( 9, 2 ) ,
list_price NUMBER( 9, 2 ) ,
category_id NUMBER NOT NULL ,
CONSTRAINT fk_products_categories
FOREIGN KEY( category_id )
REFERENCES product_categories( category_id )
ON DELETE CASCADE
);

-- customers
CREATE TABLE CUSTOMERS
(
customer_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 320
PRIMARY KEY,
name VARCHAR2( 255 ) NOT NULL,
address VARCHAR2( 255 ) ,
website VARCHAR2( 255 ) ,
credit_limit NUMBER( 8, 2 )
);

-- contacts
CREATE TABLE CONTACTS
(
contact_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 320
PRIMARY KEY,
first_name VARCHAR2( 255 ) NOT NULL,
last_name VARCHAR2( 255 ) NOT NULL,
email VARCHAR2( 255 ) NOT NULL,
phone VARCHAR2( 20 ) ,
customer_id NUMBER ,
CONSTRAINT fk_contacts_customers
FOREIGN KEY( customer_id )
REFERENCES customers( customer_id )
ON DELETE CASCADE
);

-- orders table
CREATE TABLE ORDERS
(
order_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 106
PRIMARY KEY,
customer_id NUMBER( 6, 0 ) NOT NULL, -- fk
status VARCHAR( 20 ) NOT NULL ,
salesman_id NUMBER( 6, 0 ) , -- fk
order_date DATE NOT NULL ,
CONSTRAINT fk_orders_customers
FOREIGN KEY( customer_id )
REFERENCES customers( customer_id )
ON DELETE CASCADE,
CONSTRAINT fk_orders_employees
FOREIGN KEY( salesman_id )
REFERENCES employees( employee_id )
ON DELETE SET NULL
);

-- order items
CREATE TABLE ORDER_ITEMS
(
order_id NUMBER( 12, 0 ) , -- fk
item_id NUMBER( 12, 0 ) ,
product_id NUMBER( 12, 0 ) NOT NULL , -- fk
quantity NUMBER( 8, 2 ) NOT NULL ,
unit_price NUMBER( 8, 2 ) NOT NULL ,
CONSTRAINT pk_order_items
PRIMARY KEY( order_id, item_id ),
CONSTRAINT fk_order_items_products
FOREIGN KEY( product_id )
REFERENCES products( product_id )
ON DELETE CASCADE,
CONSTRAINT fk_order_items_orders
FOREIGN KEY( order_id )
REFERENCES orders( order_id )
ON DELETE CASCADE
);

-- inventories
CREATE TABLE INVENTORIES
(
product_id NUMBER( 12, 0 ) , -- fk
warehouse_id NUMBER( 12, 0 ) , -- fk
quantity NUMBER( 8, 0 ) NOT NULL,
CONSTRAINT pk_inventories
PRIMARY KEY( product_id, warehouse_id ),
CONSTRAINT fk_inventories_products
FOREIGN KEY( product_id )
REFERENCES products( product_id )
ON DELETE CASCADE,
CONSTRAINT fk_inventories_warehouses
FOREIGN KEY( warehouse_id )
REFERENCES warehouses( warehouse_id )
ON DELETE CASCADE
);

-- members
CREATE TABLE members (
member_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50) NOT NULL,
last_name VARCHAR2(50) NOT NULL,
rank VARCHAR2(20)
);

-- member_staging
CREATE TABLE member_staging AS
SELECT * FROM members;

String Functions:

01. Convert this string 'Ä Ê Í' from this character set'US7ASCII' to "IBM West European EBCDIC Code Page
1047"

02. Print Name with comma seperated for each letter from customers table

03. Print First Name from employees table where first name length should be lessthan 5 characters

04. Prepare a statement using group by clause employees by the lengths of their first names

05. Print list of first names from employees tables where 2 times of l character in the string

06. Pring first character of first_name from employees table of M & N characters

Aggregate Functions:

01. Print category_name and max list_price from products & product_categories

02. Using max function and having clause print category_name and max list_price where list price is
between 3000 and 5000

03. Using MIN() in subquery, to get more information on the cheapest product whose the list price is the
lowest

04. Print category_name and min list_price from products & product_categories

05. Using max function and having clause print category_name and max list_price where list price
products is greater than 500

06. Using sum function try achive the similar scenarios from the above list (using group by, having,
subquery ...)
Analytical Functions:

01. Calculate rank values with the list price and print the top-10 cheapest product

02. Prepare a query and print Using Oracle ROW_NUMBER() function

03. Prpeare a query lag and lead in a single query (next to next columns and try to use source table as
"salesman_performance")

DateTime Functions:

01. Print any five timezones in a report format (Time Zone Name and Value in seperate columns)

02. Alter the sessoon NLS_DATE_FORMAT and print the current timestamp with number timezone

03. Print each value (like hour, minute, second, day, month, year) in seperate columns using extract
function

04. Print the last day of the last month

05. Calculate the number of days left of the current month

06. Print the date in different languages

DML

Write a Merge Statement to perform the following -

 Update the rows with member id 1, 3, 4, and 6 because the rank or the last name of these
members in these tables are different.
 Insert the rows with member id 7 to 10 are because these rows exist in the members table but
not in the member_staging table.

In total 8 rows should be merged.

Database Administration:

01. Print all list of users which are available in the database.

02. Create a user with the name "TNDORACLE" and grant required privilleges.

03. Create list of tables and insert data as per this "oracle-sample-database" zip file in this user
"TNDORACLE".

04. Grant select access to warehouses table for "OT" user.


05. Grant update access from this specific columns (WAREHOUSE_NAME, LOCATION_ID) to "OT" user.

06. Revoke select access to warehouses table for "OT" user.

07. Print long running job status.

08. Print any objects are in locked state.

09. Print table space name, how much space is available, used ...

10. Print all counts from the below list of tables in single report.

ORDER_ITEMS

ORDERS

INVENTORIES

PRODUCTS

PRODUCT_CATEGORIES

WAREHOUSES

EMPLOYEES

CONTACTS

CUSTOMERS

LOCATIONS

COUNTRIES

REGIONS

You might also like