experiment no 1 .1 (1)

Download Experiment No 1 .1 (1)

Post on 10-Apr-2018

214 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • 8/8/2019 Experiment No 1 .1 (1)

    1/44

    DATABASE MANAGEMENT SYSTEM

    LAB

    (Subject Code: ETCS-357)

    FACULTY NAME: Mrs. Neetu Gupta STUDENT NAME : AshishChaudhary

    ROLL NO :0361482808

    SEMESTER :V

    BATCH :ECE 123

    MAHARAJA AGRESEN INSTECEUTE OF

    TECHNOLOGY

    PSP AREA,SECTOR-22,

    ROHINI,NEW DELHI-110085

  • 8/8/2019 Experiment No 1 .1 (1)

    2/44

    Experiment no- 1

    Table creation-

    SQL> connect

    Enter user-name: system

    Enter password:

    Connected.

    SQL> create table student ( name varchar2(10),roll_no number(3),branch varchar2(10));

    Table created.

    Adding data into tables-

    Add data to all attributes in the order mentioned during table

    creation:add a record in student table-

    SQL> insert into student values('ashish','101','ECE');

    1 row created.

    Add data to all the attributes in the different order:add a record in

    student table

    SQL> insert into student(roll_no,branch,name) values('102',' ECE','vinay');

    1 row created.

    Add data to few attributes:add a record to student table (only to

    roll_no and name)

    SQL> insert into student(roll_no,name) values(103,'mayank');

    1 row created.

  • 8/8/2019 Experiment No 1 .1 (1)

    3/44

    Adding multiple rows using one query-

    SQL> insert into student values('&name','&roll','&branch');

    Enter value for name: smith

    Enter value for roll: 104

    Enter value for branch: ECE

    old 1: insert into student values('&n ame','&roll','&branch')

    new 1: insert into student values('smith','104',' ECE')

    1 row created.

    SQL> /

    Enter value for name: siya

    Enter value for roll: 201

    Enter value for branch: CSE

    old 1: insert into student values('&name','&roll','&branch')

    new 1: insert into student values('siya','201','CSE')

    1 row created.

    SQL> /

    Enter value for name: amit

    Enter value for roll: 202

    Enter value for branch: CSE

    old 1: insert into student values('&name','&roll','&branch')

    new 1: insert into student values('amit','202','CSE')

  • 8/8/2019 Experiment No 1 .1 (1)

    4/44

    1 row created.

    SQL> /

    Enter value for name: tripti

    Enter value for roll: 203

    Enter value for branch: CSE

    old 1: insert into student values('&name','&roll','&branch')

    new 1: insert into student values('tripti','203','CSE')

    1 row created.

    SQL> /

    Enter value for name: tripti

    Enter value for roll: 203

    Enter value for branch: CSE

    old 1: insert into student values('&name','&roll','&branch')

    new 1: insert into student values('tripti','203','CSE')

    1 row created.

    Viewing data of a table-

    All columns and all rows - to view all the details of all the students

    from student table

    SQL> select * from student;

  • 8/8/2019 Experiment No 1 .1 (1)

    5/44

    NAME ROLL_NO BRANCH

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

    ashish 101 ECE

    vinay 102 ECE

    mayank 103

    smith 104 ECE

    siya 201 CSE

    amit 202 CSE

    tripti 203 CSE

    tripti 203 CSE

    8 rowsselected.

    All columns and selected rows - to view all the details of all the

    students in ECE branch from student table-

    SQL>select*fromstudentwherebranch='ECE';

    NAME ROLL_NO BRANCH

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

    ashish 101 ECE

    vinay 102 ECE

    smith 104 ECE

    selected columns and all rows to view roll_no and name of all the

    students from student table-

    SQL>selectroll_no,namefromstudent;

  • 8/8/2019 Experiment No 1 .1 (1)

    6/44

    ROLL_NO NAME

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

    101 ashish

    102 vinay

    103 mayank

    104 smith

    201 siya

    202 amit

    203 tripti

    203 tripti

    8 rowsselected.

    Selected columns and selected rows to view roll number and

    name of all the students in ECE branch from student table

    SQL>selectroll_no,namefromstudentwherebranch='ECE';

    ROLL_NO NAME

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

    101 ashish

    102 vinay

    104 smith

    Eliminatingduplicaterowswhenusingselectstatement:

    To view the distinct branch names from student table

    SQL>selectdistinct(branch)fromstudent;

  • 8/8/2019 Experiment No 1 .1 (1)

    7/44

    BRANCH

    ----------

    ECE

    CSE

    Toviewthedatainsortedform-

    Sorted in ascending order to view the details of all the students

    order by name in ascending order:

    SQL>select*fromstudentorderbyname;

    NAME ROLL_NO BRANCH

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

    amit 202 CSE

    ashish 101 ECE

    mayank 103

    vinay 102 ECE

    siya 201 CSE

    smith 104 ECE

    tripti 203 CSE

    tripti 203 CSE

    8 rowsselected.

    OR

    SQL>select*fromstudentorderbynameasc;

  • 8/8/2019 Experiment No 1 .1 (1)

    8/44

    NAME ROLL_NO BRANCH

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

    amit 202 CSE

    ashish 101 ECE

    mayank 103

    vinay 102 ECE

    siya 201 CSE

    smith 104 ECE

    tripti 203 CSE

    tripti 203 CSE

    8 rowsselected.

    Sorted in descending order to view the details of all the students

    order by name in ascending order:

    SQL>select*fromstudentorderbynamedesc;

    NAME ROLL_NO BRANCH

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

    tripti 203 CSE

    tripti 203 CSE

    smith 104 ECE

    siya 201 CSE

    vinay

    102

    ECE

    mayank 103

    ashish 101 ECE

    amit 202 CSE

    8 rowsselected.

  • 8/8/2019 Experiment No 1 .1 (1)

    9/44

    Creatingtablefromanexistingtable:

    SQL>create

    table

    student1(sname,sroll,sbranch)

    as

    select

    name,roll_no,branch

    fromstudent;

    Tablecreated.

    Addingdataintoatablefromanexistingtable:

    SQL>createtablestudent2(s_namevarchar2(10),s_roll number(3),s_branch

    varchar2(10));

    Tablecreated.

    Query to add data from existing table:

    SQL>insertintostudent2selectname,roll_no,branch fromstudent;

    8 rowscreated.

  • 8/8/2019 Experiment No 1 .1 (1)

    10/44

    Experiment No 2

    Deletingrecordsorrowsfromatable

    Deleting few rows:

    Status before deleting

    SQL>select*fromstudent2;

    S_NAME S_ROLL S_BRANCH

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

    ashish 101 ECE

    vinay 102 ECE

    mayank 103

    smith 104 ECE

    siya 201 CSE

    amit 202 CSE

    tripti 203 CSE

    tripti 203 CSE

    8 rowsselected.

    Delete all the details of roll number 101 from student2 table:

    SQL>deletefromstudent2wheres_roll=101;

    1rowdeleted.

  • 8/8/2019 Experiment No 1 .1 (1)

    11/44

    Status after deletion

    SQL>select*fromstudent2;

    S_NAME S_ROLL S_BRANCH

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

    vinay 102 ECE

    mayank 103

    smith 104 ECE

    siya 201 CSE

    amit 202 CSE

    tripti 203 CSE

    tripti 203 CSE

    7 rowsselected.

    Deleting all the rows:

    Status before deletion

    SQL>select*fromstudent2;

    S_NAME S_ROLL S_BRANCH

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

    vinay 102 ECE

    mayank 103

    smith 104 ECE

    siya 201 CSE

    amit

    202

    CSE

    tripti 203 CSE

    tripti 203 CSE

    7 rowsselected.

  • 8/8/2019 Experiment No 1 .1 (1)

    12/44

    To delete all the records of student2 table

    SQL>deletefromstudent2;

    7 rowsdeleted.

    Status after deletion

    SQL>select*fromstudent2;

    norowsselected

    updatingthecontentsofatable:

    updation using where clause:update the name of a student from

    Riya to Siya in student table:

    status of student table before updation:

    SQL>select*fromstudent;

    NAME ROLL_NO BRANCH

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

    ashish 101 ECE

    vinay 102 ECE

    mayank 103

    smith 104 ECE

    siya 201 CSE

    amit 202 CSE

    tripti 203 CSE

    tripti 203 CSE

    8 rowsselected.

    Update query

    SQL>updatestudentsetname='riya'wherename='siya';

  • 8/8/2019 Experiment No 1 .1 (1)

    13/44

    1rowupdated.

    Status of student table after updation

    SQL>select*fromstudent;

    NAME ROLL_NO BRANCH

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

    ashish 101 ECE

    vinay 102 ECE

    mayank 103

    smith 104 ECE

    riya 201 CSE

    amit 202 CSE

    tripti 203 CSE

    tripti 203 CSE

    8 rowsselected.

    Update without where clause: add 100 to roll number of all the

    studemts in student table:

    SQL>updatestudentsetroll_no=roll_no+100;

    8 rowsupdated.

    Status of student table after updation:

    SQL>select*fromstudent;

    NAME ROLL_NO BRANCH

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

    ashish 201 ECE

    vinay 202 ECE

    mayank 203

  • 8/8/2019 Experiment No 1 .1 (1)

    14/44

    smith 204 ECE

    riya 301 CSE

    amit 302 CSE

    tripti 303 CSE

    tripti 303 CSE

    8 rowsselected.

    Modifyingstructureofatable

    Adding new columns to a table: add contact (numeric size-10) to

    student table

    SQL>altertablestudentadd(contactnumber(10));

    Tablealtered.

    Status of student table after adding new column:

    SQL>select*fromstudent;

    NAME ROLL_NO BRANCH CONTACT

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

    ashish 201 ECE

    vinay 202 ECE

    mayank 203

    smith 204 ECE

    riya 301 CSE

    amit 302 CSE

View more >