diff --git a/DBMS/SQL/Week3/SQLPlus1.md b/DBMS/SQL/Week3/SQLPlus1.md new file mode 100644 index 0000000..fd1e73c --- /dev/null +++ b/DBMS/SQL/Week3/SQLPlus1.md @@ -0,0 +1,476 @@ +Credentials + +- `CCE230953344@ictorcl` +- `student` + +## SQL Logs +```sql +SQL> create table PERSON(driver_id# varchar(30) primary key,name varchar(50),address varchar(100)); + +Table created. + +SQL> desc person + Name Null? Type + ----------------------------------------- -------- ---------------------------- + DRIVER_ID# NOT NULL VARCHAR2(30) + NAME VARCHAR2(50) + ADDRESS VARCHAR2(100) + +SQL> create table CAR(regno varchar(20) primary key,model varchar(30),year int); + +Table created. + +SQL> create table ACCIDENT(report_number int, accd_date date, location varchar(50)); + +Table created. + +SQL> desc accident + Name Null? Type + ----------------------------------------- -------- ---------------------------- + REPORT_NUMBER NUMBER(38) + ACCD_DATE DATE + LOCATION VARCHAR2(50) + +SQL> alter table accident add constraint pkey primary key(report_number); + +Table altered. + +SQL> desc ACCIDENT; + Name Null? Type + ----------------------------------------- -------- ---------------------------- + REPORT_NUMBER NOT NULL NUMBER(38) + ACCD_DATE DATE + LOCATION + +SQL> create table OWNS (driver_id# varchar(30),regno varchar(20),primary key(driver_id#,regno),constraint fkey1 foreign key(driver_id#)references PERSON (driver_id#),constraint fkey2 foreign key(regno)references CAR); + +Table created. + +SQL> desc OWNS + Name Null? Type + ----------------------------------------- -------- ---------------------------- + DRIVER_ID# NOT NULL VARCHAR2(30) + REGNO NOT NULL VARCHAR2(20) + +SQL> select table_name from user_tables; + +TABLE_NAME +-------------------------------------------------------------------------------- +ACCIDENT +CAR +OWNS +PERSON + +SQL> create table PARTICIPATED (driver_id# varchar(30), regno varchar(20), report_number int, damage_amount int, primary key(driver_id#,regno,report_number),constraint fkey1participated foreign key(driver_id#) references Person(driver_id#),constraint fkey2participated foreign key(regno) references car(regno),constraint fkey3paricipated foreign key(report_number) references accident); + +Table created. + +SQL> desc PARTICIPATED + Name Null? Type + ----------------------------------------- -------- ---------------------------- + DRIVER_ID# NOT NULL VARCHAR2(30) + REGNO NOT NULL VARCHAR2(20) + REPORT_NUMBER NOT NULL NUMBER(38) + DAMAGE_AMOUNT NUMBER(38) +``` + +Now, we enter values. +```sql +SQL> insert into person values(1234,'Rohan','Delhi India'); + +1 row created. + +SQL> select * from person; + +DRIVER_ID# NAME +---------- -------------------------------------------------- +ADDRESS +-------------------------------------------------------------------------------- +1234 Rohan +Delhi India + + +SQL> alter table person modify driver_id# varchar (30); + +Table altered. + +SQL> insert into person values(1235,'Rohit','Banglalore India'); + +1 row created. + +SQL> insert into person values(1236,'Ramesh','Baharashtra India'); + +1 row created. + +SQL> insert into person values(1237,'Saarthak','Manipal India'); + +1 row created. + +SQL> desc person; + Name Null? Type + ----------------------------------------- -------- ---------------------------- + DRIVER_ID# NOT NULL VARCHAR2(30) + NAME VARCHAR2(50) + ADDRESS VARCHAR2(100) + +SQL> select * from person; + +DRIVER_ID# +------------------------------ +NAME +-------------------------------------------------- +ADDRESS +-------------------------------------------------------------------------------- +1234 +Rohan +Delhi India + +1235 +Rohit +Banglalore India + +DRIVER_ID# +------------------------------ +NAME +-------------------------------------------------- +ADDRESS +-------------------------------------------------------------------------------- + +1236 +Ramesh +Baharashtra India + +1237 +Saarthak + +DRIVER_ID# +------------------------------ +NAME +-------------------------------------------------- +ADDRESS +-------------------------------------------------------------------------------- +Manipal India +``` + +Now, we modify the varchar sizes so that we can fix the display formatting. + +```sql +SQL> alter table person modify address varchar (30); + +Table altered. + +SQL> select * from person; + +DRIVER_ID# NAME +------------------------------ ---------------------------------------- +ADDRESS +------------------------------ +1234 Rohan +Delhi India + +1235 Rohit +Banglalore India + +1236 Ramesh +Baharashtra India + + +DRIVER_ID# NAME +------------------------------ ---------------------------------------- +ADDRESS +------------------------------ +1237 Saarthak +Manipal India + +SQL> alter table person modify driver_id# varchar (4); + +Table altered. + +SQL> alter table person modify name varchar (10); + +Table altered. + +SQL> select * from person; + +DRIV NAME ADDRESS +---- ---------- ------------------------------ +1234 Rohan Delhi India +1235 Rohit Banglalore India +1236 Ramesh Baharashtra India +1237 Saarthak Manipal India + +``` + +Data updation. +```sql +SQL> update person set address='Maharashtra India' WHERE driver_id# = 1236; + +1 row updated. + +SQL> select * from person; + +DRIV NAME ADDRESS +---- ---------- ------------------------------ +1234 Rohan Delhi India +1235 Rohit Banglalore India +1236 Ramesh Maharashtra India +1237 Saarthak Manipal India + +SQL> insert into person values(1238, 'Amogh', 'Muzaffarnagar'); + +1 row created. +``` +### Listing Tables +```sql +SQL> select table_name from user_tables; + +TABLE_NAME +-------------------------------------------------------------------------------- +ACCIDENT +CAR +OWNS +PARTICIPATED +PERSON +``` +More Data Entry. +```sql +SQL> insert into car values('ABCD0001','Celtos',3); + +1 row created. + +SQL> insert into car values('EFGH2001','Ferrari',32); + +1 row created. + +SQL> insert into car values('DSDS0001','Urus',5); + +1 row created. + +SQL> insert into car values('ABCD4001','Honda City',1); + +1 row created. + +SQL> insert into car values('HFSP5601','mini cooper',7); + +1 row created. + +SQL> select * from car; + +REGNO MODEL YEAR +-------------------- ------------------------------ ---------- +ABCD0001 Celtos 3 +EFGH2001 Ferrari 32 +DSDS0001 Urus 5 +ABCD4001 Honda City 1 +HFSP5601 mini cooper 7 + +SQL> insert into accident values(0001,'01-Jan-2024','Delhi India'); + +1 row created. + +SQL> select * from accident; + +REPORT_NUMBER ACCD_DATE LOCATION +------------- --------- -------------------------------------------------- + 1 01-JAN-24 Delhi India + +SQL> insert into accident values(0003,'29-feb-2024',' India'); + +1 row created. + +SQL> insert into accident values(0004,'11-Oct-1993',' Daman and Diu India'); + +1 row created. + +SQL> insert into accident values(0005,'31-march-2200',' Gujrat India'); + +1 row created. + +SQL> insert into accident values(12,'17-july-2900',' karnataka India'); + +1 row created. + +SQL> select * from accident; + +REPORT_NUMBER ACCD_DATE LOCATION +------------- --------- -------------------------------------------------- + 1 01-JAN-24 Delhi India + 3 29-FEB-24 India + 4 11-OCT-93 Daman and Diu India + 5 31-MAR-00 Gujrat India + 12 17-JUL-00 karnataka India + +SQL> insert into owns values(1234,'ABCD0001'); + +1 row created. + +SQL> insert into owns values(1235,'EFGH2001'); + +1 row created. + +SQL> insert into owns values(1236,'DSDS0001'); + +1 row created. + +SQL> insert into owns values(1237,'ABCD4001'); + +1 row created. + +SQL> insert into owns values(1238,'HFSP5601'); + +1 row created. + +SQL> select * from owns; + +DRIVER_ID# REGNO +------------------------------ -------------------- +1234 ABCD0001 +1235 EFGH2001 +1236 DSDS0001 +1237 ABCD4001 +1238 HFSP5601 +``` +Note: +```sql +SQL> @/filename.sql +``` +will import the commands from a doc and run them. + +Saved commands into `participated.sql`. + +```sql +SQL> @C:\Users\student\Downloads\participated.sql + +1 row created. + + +1 row created. + + +1 row created. + + +1 row created. + + +1 row created. + + +SQL> select * from participated; + +DRIVER_ID# REGNO REPORT_NUMBER DAMAGE_AMOUNT +------------------------------ -------------------- ------------- ------------- +1234 ABCD0001 1 10000 +1236 DSDS0001 3 150 +1237 ABCD4001 4 799 +1238 HFSP5601 5 1500 +1238 HFSP5601 12 95500 +``` +## Q3 +Updating data entries: +```sql +SQL> update participated set damage_amount=25000 where regno='HFSP5601' and report_number=12; + +1 row updated. + +SQL> update participated set damage_amount=25000 where regno='ABCD0001' and report_number=12; + +0 rows updated. + +SQL> select * from participated; + +DRIVER_ID# REGNO REPORT_NUMBER DAMAGE_AMOUNT +------------------------------ -------------------- ------------- ------------- +1234 ABCD0001 1 10000 +1236 DSDS0001 3 150 +1237 ABCD4001 4 799 +1238 HFSP5601 5 1500 +1238 HFSP5601 12 25000 +``` + +## Q4 +### Dropping an attribute +```sql +SQL> alter table Participated + + 2 drop constraint fkey3paricipated; + +Table altered. + +SQL> select * from participated; + +DRIVER_ID# REGNO REPORT_NUMBER DAMAGE_AMOUNT +------------------------------ -------------------- ------------- ------------- +1234 ABCD0001 1 10000 +1236 DSDS0001 3 150 +1237 ABCD4001 4 799 +1238 HFSP5601 5 1500 +1238 HFSP5601 12 25000 +``` + +Adding back the attribute. + +```sql +SQL> alter table Participated + 2 add constraint fkey3participated foreign key(report_number) references accident(report_number) + 3 on delete cascade; + +Table altered. +``` + +### Conditional Deletion +```sql +SQL> select * from accident; + +REPORT_NUMBER ACCD_DATE LOCATION +------------- --------- -------------------------------------------------- + 1 01-JAN-24 Delhi India + 3 29-FEB-24 India + 4 11-OCT-93 Daman and Diu India + 5 31-MAR-00 Gujrat India + 12 17-JUL-00 karnataka India + +SQL > delete from accident where extract(year from accd_date)=1993; + +1 row deleted. + +SQL> select * from accident; + +REPORT_NUMBER ACCD_DATE LOCATION +------------- --------- -------------------------------------------------- + 1 01-JAN-24 Delhi India + 3 29-FEB-24 India + 5 31-MAR-00 Gujrat India + 12 17-JUL-00 karnataka India +``` + +## Q5 + +```sql +SQL> alter table Participated drop constraint fkey3participated; + +Table altered. + +SQL> alter table Participated add constraint fkey3participated foreign key(report_number) references accident(report_number) on delete cascade; + + +Table altered. +``` + +## Q6 + +```sql +SQL> alter table participated add constraint checkparticipated check(Damage_amount>=0); + +Table altered. + +SQL> select * from participated; + +DRIVER_ID# REGNO REPORT_NUMBER DAMAGE_AMOUNT +------------------------------ -------------------- ------------- ------------- +1234 ABCD0001 1 10000 +1236 DSDS0001 3 150 +1238 HFSP5601 5 1500 +1238 HFSP5601 12 25000 + +``` \ No newline at end of file