Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhjoshi committed Feb 4, 2016
1 parent a37902d commit 4604dfb
Showing 1 changed file with 227 additions and 0 deletions.
227 changes: 227 additions & 0 deletions lab2_4-4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
-- Create Table from existing table
CREATE TABLE students_copy(idno, name, dob, cga, age) AS
SELECT idno, name, dob, cgpa, age
FROM students;

-- User defined types
/* CREATE OR REPLACE TYPE <type_name>
AUTHID <CURRENT USER | DEFINER>
AS OBJECT (
<attribute> <attribute data_type>,
<inheritence clause>,
<subprogram spec>)
<FINAL | NOT FINAL> <INSTANTIABLE | NOT INSTANTIABLE>;
*/

CREATE OR REPLACE TYPE phone_t AS OBJECT (
a_code CHAR(3),
p_number CHAR(8));

CREATE TABLE phone (
per_id NUMBER(10),
per_phone phone_t);

INSERT INTO phone (per_id, per_phone)
VALUES (1, phone_t('206', '555-1212'));

SELECT p.per_phone.p_number
FROM phone p
WHERE p.per_phone.a_code = '206';

SELECT * from phone;

-- ALTER TABLE (BIG HEADER)
/* ALTER TABLE <table name> <action>; */

--Rename table
ALTER TABLE phone RENAME TO phone1;

--Add [COLUMN]<column definition>
/* Column definition may be types and constraints or any other ele of col def. */

/*
RULES for adding a col :
1) add col anytime if NOT NULL isnt specified.
2) add NOT NULL in 3 steps
2.1) Add col without NOT NULL specified
2.2) Fill every row with data
2.3) Modify col to be NOT NULL
*/
alter table staff add email varchar(20);
alter table students add hostel varchar(20);

-- MODIFY <column name> [SET DEFAULT <value expression> | DROP DEFAULT]
/* to change or drop specified default value.
RULES for modifying a col :
1) can increase CHAR col's width anytime
2) can increase no of dig in NUMBER col anytime
3) can increase or decrease no of decimal places in NUMBER col anytime
if col is NULL for every row of table
4) can change its datatype
5) can decrease CHAR col's width
6) can decrease no of dig in NUMBER col
*/
alter table course modify compcode numeric(5,0);

-- DROP COLUMN <column name>[CASCADE | RESTRICT]
/* if ext dependencies, drop will be rejected. CASCADE will attempt to
all external dependencies b4 deleting the col. RESTRICT is opp(default).
*/
alter table staff drop column email;

-- ADD <tbale constraint>
ALTER TABLE students ADD CONSTRAINT agecheck check(age>15);

-- Add multiple things (constraints/cols)
ALTER TABLE students ADD (
testcol number(9) NOT NULL,
constraint cgcheck check(cgpa>=2.0)
);

--DROP CONTRAINT <constraint name> {CASCADE | RESTRICT]
alter table course drop constraint un_course;





-- Generated Values (for auto generation of data use IDENTITY)
/* following gives error , but is standard, not supported in oracle*/
CREATE TABLE purchases (
orderno INTEGER GENERATED BY DEFAULT AS IDENTITY,
verndorid CHAR(5),
ordertime DATETIME
);




-- SEQUENCES (db obj that generates values)
/* more general way of auto generating values */
/*
CREATE SEQUENCE <sequence name> [AS <data type>]
[START WITH <signed numberic litreral>]
[INCREMENT BY <signed numberic literal>]
[MAXVALUE <signed numeric literal> | NO MAXVALUE]
[MINVALUE <signed numeric literal> | NO MINVALUE]
[CYCLE | NO CYCLE]
*/

CREATE SEQUENCE customers_seq
START WITH 1000
INCREMENT BY 1
NOCACHE
NOCYCLE;

/* CACHE 30 - precomputing 30 seq values and keeping in memory. */
SELECT customers_seq.NEXTVAL FROM DUAL;
SELECT customers_seq.CURRVAL FROM DUAL;

CREATE TABLE customers(
customer_id number(9) primary key,
customer_name char(50)
);

INSERT INTO customers(customer_ID, customer_name) VALUES (customers_seq.nextval, 'Jiva');

SELECT * from customers;



-- Drop table <tablename> [CASCADE | RESTRICT];
drop table students_copy;




-- MODIFYING DATA (DML)

--1) INSERT
-- create new from old
create table customers_copy(
customer_id number(9) primary key,
customer_name char(50)
);

insert into customers_copy(customer_id, customer_name) select * from customers;

--2) UPDATE
insert into students values ('2000A7PS001', 'lol', '04/Mar/1996', 9.2, 20, 'Mal', 1);

update students
set name='test', cgpa = '9.6'
where idno='2000A7PS001';

select * from students;


--3) DELETE
delete from students
where idno='2000A7PS001';

cREATE TABLE LOL(
NAME CHAR(20),
id number(10));

insert into LOL values ('lakhan', 7);
select * from LOL;

drop table LOL;
/*
TRUNCATE removes all rows without making entries into recovery
log but it can't be rolled back. TRUNCATE is faster than DELETE.
TRUNCATE is DDL statement and DELETE is a DML statement.
*/


-----------------EXERCISE--------------
create table Category_details (
category_id numeric(2),
category_name varchar(30)
);

create table Sub_category_details(
sub_category_id numeric(2),
category_id numeric(2),
sub_category_name varchar(30)
);

create table Product_details(
Product_id numeric (6),
category_id numeric(2),
sub_category_id numeric(2),
product_name varchar(30)
);

--1
alter table category_details add constraint categorypk primary key(category_id);

--2 (how to add without constraint name ? -> REMOVE KEYWORD CONSTRAINT)
alter table sub_category_details add primary key(sub_category_id);

--3
alter table sub_category_details add constraint forkey foreign key(category_id) REFERENCING category_details(category_id);

--4
alter table product_details add(
constraint prodpk primary key(product_id),
constraint forkeyprod foreign key(category_id) referencing category_details(category_id),
constraint subforkeyprod foreign key(sub_category_id) referencing sub_category_details(sub_category_id)
);

--5
alter table product_details add price numeric(2);

--6
alter table product_details modify price numeric(6,2);

--7









0 comments on commit 4604dfb

Please sign in to comment.