ddb lab record

Upload: venu-gopal-p

Post on 14-Apr-2018

239 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 DDB Lab Record

    1/52

  • 7/30/2019 DDB Lab Record

    2/52

  • 7/30/2019 DDB Lab Record

    3/52

  • 7/30/2019 DDB Lab Record

    4/52

    1.B.NAGARAJU2.SRIDHAR REDDY3.P.SHAILAJA4.PADMAVATHI5.QURRATHUL AIN HAROON

  • 7/30/2019 DDB Lab Record

    5/52

    Week 1: E-R Model

    E-R Model describes data as entities, relationships and attributes .The ER-Model is

    important preliminary for its role in database design. ER Model is usually shown pictorially

    using entity relationship diagrams.

    Some basic concepts of E-R model are described below.

    Entity: The object in the ERModel represents as an entity. Entity is the thing in the real world

    with an independent existence. In ER modeling an entity is shown as a rectangle and name in it.

    e.g.

    Entity_Name

    E-R Diagram: Shows the entities and relations pictorially. That is how the entities are relatedamong themselves.

    e.g.Entity_1 Entity_2

    Relates

  • 7/30/2019 DDB Lab Record

    6/52

    Attributes:

    The properties that characterize an entity set are called its attributes. An attribute is

    referred to by the terms data items, data element, data field item.

    Ex: attributes for bus entity and ticket entity.

    Types of attributes: There are different types of attributes

    Domain Attribute: The set of possible values for an attribute is called the domain of the

    attribute.

    Key Attribute: The attribute (or combination of attributes) that is unique for every entity

    instance.

    Eg: The account number of account

    The employee id of an employee

    Simple Attribute:The attribute which cantbe broken-down into small components.

    Divorced

    Marital status

    Single

    Married Widowed

  • 7/30/2019 DDB Lab Record

    7/52

    Composite Attribute: The attribute which can be split into components.

    Single valued Attribute: The attribute can take on only a single value for each entity instance.

    Multi-valued Attribute: An attribute that may take more than one value for a given entity. It is

    represented with double ellipse.

    Stored Attribute: Attribute that need to be stored permanently.

    Eg: Name of employee

    Addr

    Hno

    Street City

    Pincode

    Day

    Month

    Year

    Date

    AGE

    Phno

  • 7/30/2019 DDB Lab Record

    8/52

    Derived Attribute: Attribute that can be calculated based on other attributes. It is represented

    with dotted lines.

    Keys:

    Candidate Key: It can be defined as an attribute or combination of attributes that identifiesthe record uniquely but none of its proper subsets can identify the record uniquely.

    Primary Key: A candidate key that is used by the database designer for unique identificationof each row in a table is known as primary key. A primary key consists of one or more

    attributes of the table.

    EMP

    Current

    Date

    Joining

    Date

  • 7/30/2019 DDB Lab Record

    9/52

    Partial key: A weak entity type normally has a partial key which is the set of attributes thatcan uniquely identify weak entity that are related to the same owner entity.

    Super Key: This can be defined as the combination of all keys.

    Foreign key: A foreign key (FK) is an attribute or combination of attributes that is used toestablish and enforce a link between the data in two tables.

    In a foreign key reference, a link is created between two tables when the column or columns

    that hold the primary key value for one table are referenced by the column or columns in

    another table. This column becomes a foreign key in the second table.

    Composite key: Sometimes it requires more than one attribute to uniquely identify an entity. Aprimary key that made up of more than one attribute is known as a composite key.

    For our database we identify entities as below.

    o Bus: Primary key: BusNo and ServiceNoo Passenger: Primary key: PassengerId, Foreign key: TicketIdo Ticket: Primary key: TicketId, Foreign key: BusNoo Reservationo Cancellation

    Week 2: Concept Design with E-R Model

    Relationship: A relationship type between two entity types defines the set of all associations

    between these entity types

    Each instance of the relationship between members of these entity types is called a relationship

    instance.

    e.g.Customer creates an acount in bank, here creating account is a relation between Customer

    and Bank entity.

    Entity diagrams with Attributes:

  • 7/30/2019 DDB Lab Record

    10/52

    Customer Entity:

    Account Entity:

    Bank Entity:

  • 7/30/2019 DDB Lab Record

    11/52

    Administration Entity:

    Deposit Entity:

  • 7/30/2019 DDB Lab Record

    12/52

    Withdraw Entity:

    Transfer Entity:

  • 7/30/2019 DDB Lab Record

    13/52

    Cardinalities: Cardinality relationships can have different connectivity

    A ratio, such as 1:1, 1: N, M: 1, and M: N, gives a cardinality constraint or numeric restriction

    on the possible relationships.

    One-to-one (1:1): relationship from entity type S to entity type T is one in which an entity from

    S is related to at most one entity from T and vice versa.

    One-to-many (1: N): relationship from entity type S to entity type T is one in which two or more

    entities from T can be related to an entity from S.

    Many-to- One (M: 1): relationship from entity type S to entity type T is one in which an entity

    from S can be related to two or more entities from T.

    Many-to-many (M:N):relationship from entity type S to entity type T is one in which an entity

    from S can be related to two or more entities from T, and an entity from T can be related to two

    or more entities from

  • 7/30/2019 DDB Lab Record

    14/52

    E R Diagrams :

    Create account

    Close account

    Check Balance

  • 7/30/2019 DDB Lab Record

    15/52

    Deposit

    Withdraw

  • 7/30/2019 DDB Lab Record

    16/52

    Transfer

    Management

    Bank Management System

  • 7/30/2019 DDB Lab Record

    17/52

    Types of Entities:

    There are 2 types of entities.

    Strong Entity:A Strong Entity is one that exists on its own, independent of other entities. An

    entity set that has primary key is termed as strong entity set.

    Weak Entity: A weak entity can be identified uniquely only by considering the primary key of

    another (owner) entity. Owner entity set and weak entity set must participate in a one-to-many

    relationship set (one owner, many weak entities).Weak entity set must have total participation in

    this identifying relationship set.

    Types of Relationships:

    Aggregation: Aggregation refers to an abstraction in which a relationship between objects is

    regarded as a higher-level object.

    Generalization: It is a relationship that exists between a high level entity set and one or more

    lower level entity set.

  • 7/30/2019 DDB Lab Record

    18/52

    Specialization: Is the process of defining a set of subclasses of a super class .The set of

    subclasses is based upon some distinguishing characteristics of the entities in the super class.

    Super class/subclass relationships and specialization can be diagrammatically represented in ER

    diagrams.

    Week 3: Relational Model

    Relation can be defined as a set of tuples. Here all tuples in a relation must be distinct, thatis no two tuple can have the same combination of values for all their attributes.

    Schema is the basic design of a relation i.e. nothing but a table design.

    In our database the schema of tables would be as follows:

    Customer table schema:

    cust_id cust_name Age DOB Gender Ph_no Address

    Customer

  • 7/30/2019 DDB Lab Record

    19/52

    cust_id: number(11) PRIMARY KEY

    cust_name: varcharchar2(20)

    Age : number(2)

    Gender : varchar2(6)

    DOB : date

    Ph_no : number(10)

    Address : varchar2(50)

    Account table schema:

    Account_no Account_type Date_of_creating_account Date_of_closing_account

    AccountAccoun_ no: varcha2(10) PRIMARY KEY

    Account_type: varchar2(10)

    Date_o_creating_account : dateDate_of_closing_account : date

    Bank table schema:

    IFSC Bank_name Branch Ph_no Address

    Bank

    IFSC : number(10) PRIMARY KEY

    Bank_ name : varchar2(20)

    Branch : varchar2(20)

    Ph_no: number(10)

    Address: varchar2(30)

    Administration Schema:

    Emp_id Emp_name Designation Department

    Administration

    Emp_id : number(10) PRIMARY KEY

    Emp_name: varchar2(25)

  • 7/30/2019 DDB Lab Record

    20/52

    Designation: varchar2(15)

    Department: varchar2(15)

    Deposit Schema:

    Account_no Cust_id Cust_name Date_of_deposit Amount

    Deposit

    Account_no : varchar(11) foreign key(Account_no) reference to Account(Account_no)

    Cust_id number(11) foreign key(cust_id) reference to Customer(Cust_id)

    Date_of_deposit : date,

    Cust_name : varchar2(30)Amount: number(11)

    Withdraw schema:

    Account_no Cust_id Cust_name Date_of_withdraw Amount

    Withdraw

    Account_no : varchar(11) foreign key(Account_no) reference to Account(Account_no)

    Cust_id number(11) foreign key(cust_id) reference to Customer(Cust_id)

    Cust_name : varchar2(20)

    Date_of_withdraw :date

    Accoun_ no : number(11) references customer(cust_id)

    Amount: number(11)

    Tr ansfer schema:

    Cust_name Date_of_transfer Amount

    TO_ACCOUNT

    _NO

    FROM_ACCOUT_NO

    Transfer

    Cust_name : varchar2(20)

  • 7/30/2019 DDB Lab Record

    21/52

    Date_of_transfer :date

    To account:number(10)

    From account:number(11)

    Week4: NormalizationDatabase normalization is a technique for designing relational database tables to

    minimize duplication of information and, in doing so , to safeguard the database against certain

    types of logical or structural problems namely data anomalies.

    Normal Forms:

  • 7/30/2019 DDB Lab Record

    22/52

    1st Normal Form: 1NF requires that the values in each column of a table are atomic. Byatomic we mean that there are no sets of values within a column.

    So the relation gets normalized under the 1ST normal form as below by splitting therecord into two records.

    Cust_id cust_name Age gender DOB Phno ADDRESS

    1010054211

    Preetam 30 M 03-04-1981 9949887766

    9873838272

    #1-345, Narayanaguda,

    Hyderabad

    1010054212

    Sreeram 27 M 02-12-1984 9440565656 #3-11/A, Bowenpally,

    Hyderabad

    1010054213

    Shankar 32 M 23-07-1979 9440440440 #4-12/1/2, Malakpet,

    Hyderabad

    1010054214

    Ashutosh 31 M 05-08-1980 9866324324 #1-234, Chikkadpally,

    Hyderabad

    1010054215

    Aruna 20 F 04-05-1991 8011478531 #1-234, Chikkadpally,

    Hyderabad

    1010054216

    Anvesh 20 M 23-01-1991 8011478532 #2-89/A/1, Santoshnar,

    Hyderabad

    1010054217

    Saidatta 31 M 03-12-1982 8066952326 #2-45/6/2, Lungarhouse,

    Hyderabad

    Normalized Relation:

    Cust_id cust_name Age gender DOB PHN_N0 ADDRESS

  • 7/30/2019 DDB Lab Record

    23/52

    1010054211 Preetam 30 M

    03-04-1981 9949887766 #1-345, Narayanaguda,

    Hyderabad

    1010054211 Preetam 30 M

    03-04-1981 9873838272 #1-345, Narayanaguda,

    Hyderabad

    1010054212 Sreeram 27 M 02-12-19849440565656

    #3-11/A, Bowenpally,

    Hyderabad

    1010054213 Shankar 32 M

    23-07-1979 9440440440 #4-12/1/2, Malakpet,

    Hyderabad

    1010054214

    Ashutosh

    31 M

    05-08-1980 9866324324 #1-234, Chikkadpally,

    Hyderabad

    1010054215Aruna

    20 F23-01-1991 8011478531 #1-234, Chikkadpally,

    Hyderabad

    1010054216

    Anvesh

    20 M

    23-01-1991 8011478532 #2-89/A/1, Santoshnar,

    Hyderabad

    1010054217

    Saidatta

    29 M

    03-12-1982 8066952326 #2-89/A/1, Santoshnar,

    Hyderabad

    2nd Normal Form: where the 1NF deals with atomicity of data, the 2NF deals withrelationships between composite key columns and non-key columns. To achieve 2NF thetables should be in 1NF. The 2NF any non-key columns must depend on the entire

    primary key. In case of a composite primary key, this means that non-key column cant

    depend on only part of the composite key.

    That is noPartial Functional Dependencies shoud exist. In our entities Deposit entity is having the keys with the combination of

    account_no and cust_id . But there exists the following partial functional

    dependency:

    ACCOUNT_NO CUST_ID,CUST_NAME

    So in order to avoid this we normalize the Deposit table as below.

  • 7/30/2019 DDB Lab Record

    24/52

    ACCOUNT_NO Cust_id Cust_name Date of deposit Amount

    1234A21012 1010054211 Preetam 12-09-2010 120000

    123J90187K 1010054212 Sreeram 09-12-2010 130000

    123FGT5678 1010054213 Shankar 22-08-2010 3000

    1234M4567D 1010054214 Ashutosh 23-04-2010 40000

    Normalized Relations:

    ACCOUNT_NO CUST_ID,CUST_NAME

    Account_no Cust_id Cust_name

    1234A21012 1010054211 Preetam

    123J90187K 1010054212 Sreeram

    123FGT5678 1010054213 Shankar

    1234M4567D 1010054214 Ashutosh

    ACCOUNT_NO DATE OF DEPOSIT,AMOUNT

    ACCOUNT_NO DATE OF

    DEPOSIT

    AMOUNT

    1234A21012 12-09-2010 120000

    123J90187K 09-12-2010 130000

    123FGT5678 22-08-2010 3000

    1234M4567D 23-04-2010 40000

  • 7/30/2019 DDB Lab Record

    25/52

    3rd Normal Form: 3NF requires that all columns depend directly on the primary key.Tables violate the third normal form when one column depends an another column,

    which in turn depends on the primary key(transitive dependency). One way to identify

    transitive dependency is to look at your tables and see if any columns would require

    updating if another column in the table was updated. If such a column exists, it probably

    violates 3NF.

    In our entities CUSTOMER entity faces the Transitive Dependency problem.Thefollowing transitive dependencies exist:

    CUST_ID DOB

    DOB AGE

    To eliminate this problem the relation is normalized as follows.

    Cust_id cust_name Age gender DOB PHN_N0 ADDRESS

    1010054211 Preetam 30 M

    03-04-1981 9873838272 #1-345, Narayanaguda,

    Hyderabad

    1010054212 Sreeram 27 M 02-12-19849440565656

    #3-11/A, Bowenpally,

    Hyderabad

    1010054213 Shankar 32 M

    23-07-1979 9440440440 #4-12/1/2,Malakpet,

    Hyderabad

    1010054214

    Ashutosh

    31 M

    05-08-1980 9866324324 #1-234, Chikkadpally,

    Hyderabad

    1010054215

    Aruna

    20 F

    23-01-1991 8011478531 #1-234, Chikkadpally,

    Hyderabad

    1010054216

    Anvesh

    20 M

    23-01-1991 8011478532 #2-89/A/1, Santoshnar,

    Hyderabad

    1010054217

    Saidatta

    29 M

    03-12-1982 8066952326 #2-89/A/1, Santoshnar,

    Hyderabad

  • 7/30/2019 DDB Lab Record

    26/52

    CUST_ID DOB

    DOB AGE

    Cust_id cust_name gender DOB PHN_N0 ADDRESS

    1010054211 Preetam M

    03-04-1981 9873838272 #1-345,

    Narayanaguda,

    Hyderabad

    1010054212 Sreeram M02-12-1984

    9440565656

    #3-11/A,

    Bowenpally,

    Hyderabad

    1010054213 Shankar M

    23-07-1979 9440440440 #4-

    12/1/2,Malakpet,

    Hyderabad

    1010054214

    Ashutosh

    M

    05-08-1980 9866324324 #1-234,

    Chikkadpally,

    Hyderabad

    Cust_id cust_name gender Age PHN_N0 ADDRESS

    1010054211 Preetam M 30

    9873838272 #1-345,

    Narayanaguda,

    Hyderabad

    1010054212 Sreeram M 27

    9440565656

    #3-11/A,

    Bowenpally,

    Hyderabad

    1010054213 Shankar M 32

    9440440440 #4-

    12/1/2,Malakpet,

    Hyderabad

    1010054214Ashutosh

    M 319866324324 #1-234,

    Chikkadpally,

  • 7/30/2019 DDB Lab Record

    27/52

    Boyce Codd Normal Form:

    Takes care of candidate keys.A relation in a Boyce Codd normal form iff every determinant in

    the relation is a candidate key. That is this normal form eliminates the overlapping candidate

    keys.

    Week 5: Installation of MySQL

    Download MySQL software from the web sitehttp://www.soft82.com/get/download/mysqlworkbench

    Save the file and then run the set up after downloading. Before running Mysqlworkbench we have to install .net frameworkof version 2.0 (or) higher.Now we have

    to download the .net frameworkfromthe web site:

    http://www.brothersoft.com/net.framework-download-112623.html

    Before installing the .net frame workwe have to download the Windows Installer 3.1to support the .net frame workfrom the web site:

    http://www.microsoft.com/downloads/

    Windows I nstaller I nstallation:

    After downloading the Windows Installer 3.1 software run the set up.

    Hyderabad

    http://www.soft82.com/get/download/mysqlworkbenchhttp://www.soft82.com/get/download/mysqlworkbenchhttp://www.brothersoft.com/net.framework-download-112623.htmlhttp://www.brothersoft.com/net.framework-download-112623.htmlhttp://www.microsoft.com/downloads/http://www.microsoft.com/downloads/http://www.microsoft.com/downloads/http://www.brothersoft.com/net.framework-download-112623.htmlhttp://www.soft82.com/get/download/mysqlworkbench
  • 7/30/2019 DDB Lab Record

    28/52

    Start the set up wizard and clickNEXT. Next click on the LICENSE agreement to ACCEPT and again clickNEXT. Now updating the system process starts. After installing the Windows Installer 3.1 software click on FINISH.

    .NET Framework I nstallation:

    Now start the installation process of.net frame workto install Mysql Work Bench. Start the set up wizard. Next ACCEPT the LICENCE AGREEMENT and clickINSTALL. Now the DOWNLOAD and INSTALLATIONprocess starts.

    Before INSTALLATION, it first DOWNLOADS the components of.net frame workfrom internet and then starts the INSTALLATIONprocess.

    After completing the INSTALLATIONprocess click on EXIT and the setup iscompleted.

    MySQL Work Bench Installation:

    Run the setup. It will show setup wizard like the following window.

  • 7/30/2019 DDB Lab Record

    29/52

    Now click on NEXT to start the INSTALLATION process. Now select the Setup Type.

    Complete

    Custom Click on COMPLETE to install all program feature and clickNEXT. Now click on INSTALL to start the installation process. Now the INSTALLATIONprocess starts. Now click on FINISH to complete the setup wizard. The overall INSTALLATIONprocess has been completed. And we can perform

    QUERIESby using MySQL Work Bench.

    We are going to execute the queries by using MySQL server.

    MySQL Server I nstallation:

    Step 1: Download mysql server essential from the websitewww.mysql.com/downloads and savethe .exe file.

    http://www.mysql.com/downloads%20and%20save%20thehttp://www.mysql.com/downloads%20and%20save%20thehttp://www.mysql.com/downloads%20and%20save%20thehttp://www.mysql.com/downloads%20and%20save%20thehttp://www.mysql.com/downloads%20and%20save%20thehttp://www.mysql.com/downloads%20and%20save%20the
  • 7/30/2019 DDB Lab Record

    30/52

    Steps 2&3: Double click on the mysql.exe file to start installation.

    Steps 4&5:

  • 7/30/2019 DDB Lab Record

    31/52

    :

    Steps: 6&7

  • 7/30/2019 DDB Lab Record

    32/52

    Steps 8&9:

  • 7/30/2019 DDB Lab Record

    33/52

    Steps: 10&11

  • 7/30/2019 DDB Lab Record

    34/52

    Step: 12&13

  • 7/30/2019 DDB Lab Record

    35/52

    Steps: 14&15

  • 7/30/2019 DDB Lab Record

    36/52

    Steps: 16&17

  • 7/30/2019 DDB Lab Record

    37/52

    Steps: 18&19

  • 7/30/2019 DDB Lab Record

    38/52

    DDL Commands

    Data Definition Language commands are used for the basic operations like creating,

    deleting etc..of database tables. There are many DDL Commands some of them are described

    below.

    Create: It is used to create the tables in the database.On a specific database a new table is

    created using create command.

    Syntax: CREATE TABLE TABLE_NAME(ATTRIBUTE_LIST);

    e.g. CREATE TABLE CUSTOMER (CUST_ID NUMBER(11) PRIMARY KEY,

    CUST_NAME VARCHAR2(20), AGE NUMBER(2) ,DOB DATE, GENDER VARCHAR2(1),

    PH_NO NUMBER(10),ADDRESS VARCHAR2(40));

  • 7/30/2019 DDB Lab Record

    39/52

    Above example creates a table called CUSTOMER. With DESC command we can view

    the basic table scheme.

    DESC CUSTOMER;

    Above command shows the table design as follows.

    * Lets consider our DB is having a table called DEMO.

    Rename: It is used to change the table name in the database.

    Syntax: RENAME TABLE OLD_TABLE_NAME TO NEW_TABLE_NAME;

    e.g. RENAME TABLE DEMO TO DEMO1;

    Alter: It is used to add or remove the columns/fields from existing table.

    Syntax: ALTER TABLE TABLE_NAME ADD/REMOVE FIELD_NAME;

    e.g. ALTER TABLE DEMO1 ADD EXTRA_FIELD CHAR(10);

    Above example adds one extra field EXTRA_FIELD to the DEMO1 table.

    Truncate: It is used to delete only the records of the table in the database

    Syntax: TRUNCATE TABLE TABLE_NAME;

    e.g. TRUNCATE TABLE DEMO1;

    Above command deletes all records of DEMO1 table.

    Drop: It is used to delete the entire table from the database.

    Syntax: DROP TABLE TABLE_NAME;

    e.g. DROP TABLE DEMO1;

    Field Type Null Key Default Extra

    CUST_ID

    CUST_NAME

    AGE

    DOB

    GENDER

    PH_NO

    ADDRESS

    NUMBER(11)

    VARCHAR2(20)

    NUMBER(2)

    DATE

    VARCHAR2(2)

    NUMBER(10)

    VARCHAR2(40)

    NO

    YES

    YES

    YES

    YES

    YES

    YES

    PRI

    Not Null

    Not Null

    Not Null

    Not Null

    Not Null

    Not Null

  • 7/30/2019 DDB Lab Record

    40/52

    Above table deletes DEMO1 table completely from database.

    Week 6: DML Commands

    Data Manipulation Language commands are used for access/ manage the data items in

    database table. Some of the DML commamds are described below.

    Insert: It is used to insert the data items in to the created tables in the database.

    Syntax: INSERT INTO TABLE_NAME VALUES(values);

  • 7/30/2019 DDB Lab Record

    41/52

    e.g. INSERT INTO CUSTOMER VALUES (1010054211,Preetham,30,m,03-01-1981,

    9949887766, #1-345, Narayanaguda, Hyderabad);

    INSERT INTO CUSTOMER VALUES (1010054212,Sreeram,27,m,02-12-1984,

    9440565656, #3-11/A, Bowenpally, Hyderabad);

    Above 2 queries insert the values in CUSTOMER table.

    Select: It is used to select/retrieve the data from the tables in the database.

    Syntax: SELECT FIELD(S) FROM TABLE_NAME;

    e.g. SELECT * FROM CUSTOMER;

    Above command retrieves all the records of BUS table and shows like the follow.

    Here * indicates all fields values. into two records.

    CUST_ID

    CUST_NA

    ME

    AG

    E

    GEND

    ER DOB PHNO ADDRESS

    1010054211Preetam 30 M 03-04-1981 9949887766 #1-345, Narayanaguda,

    Hyderabad

    1010054212

    Sreeram 27 M 02-12-1984 9440565656 #3-11/A, Bowenpally,

    Hyderabad

    1010054213

    Shankar 32 M 23-07-1979 9440440440 #4-12/1/2, Malakpet,

    Hyderabad

    1010054214

    Ashutosh 31 M 05-08-1980 9866324324 #1-234, Chikkadpally,

    Hyderabad

    1010054215

    Aruna 20 F 04-05-1991 8011478531 #1-234, Chikkadpally,

    Hyderabad

    1010054216

    Anvesh 20 M 23-01-1991 8011478532 #2-89/A/1, Santoshnar,

    Hyderabad

  • 7/30/2019 DDB Lab Record

    42/52

    1010054217

    Saidatta 31 M 03-12-1982 8066952326 #2-45/6/2, Lungarhouse,

    Hyderabad

    Update: It is used to update the existing data within a table in the database.

    Syntax: UPDATE TABLE_NAME SET FIELD=VALUE WHERE CONDITION;

    e.g. UPDATE CUSTOMER SET CUST_NAME=nagaraju WHERE CUST_ID=1010054211;

    Above query sets name nagaraju in CUST_NAME field in the 1010054211 CUST_ID

    record in CUSTOMER table.

    If we select the records from BUS table, it will show table as follows.

    CUST_ID

    CUST_NA

    ME

    AG

    E

    GEND

    ER DOB PHNO ADDRESS

    1010054211

    Nagaraju 30 M 03-04-1981 9949887766 #1-345, Narayanaguda,

    Hyderabad

    1010054212

    Sreeram 27 M 02-12-1984 9440565656 #3-11/A, Bowenpally,

    Hyderabad

    1010054213

    Shankar 32 M 23-07-1979 9440440440 #4-12/1/2, Malakpet,

    Hyderabad

    1010054214

    Ashutosh 31 M 05-08-1980 9866324324 #1-234, Chikkadpally,

    Hyderabad

    1010054215

    Aruna 20 F 04-05-1991 8011478531 #1-234, Chikkadpally,

    Hyderabad

    1010054216

    Anvesh 20 M 23-01-1991 8011478532 #2-89/A/1, Santoshnar,

    Hyderabad

    1010054217

    Saidatta 31 M 03-12-1982 8066952326 #2-45/6/2, Lungarhouse,

    Hyderabad

    Delete: It is used to delete the records from the tables and the space will remain same in the

    table with empty space.

  • 7/30/2019 DDB Lab Record

    43/52

    Syntax: DELETE TABLE_NAME WHERE CONDITION;

    e.g. DELETE CUSTOMER WHERE CUST_ID=1010054217;

    Above query will delete record related to the customer id is 1010054217 from CUSTOMER

    table.

    CUST_ID

    CUST_NA

    ME

    AG

    E

    GEND

    ER DOB PHNO ADDRESS

    1010054211

    Nagaraju 30 M 03-04-1981 9949887766 #1-345, Narayanaguda,

    Hyderabad

    1010054212

    Sreeram 27 M 02-12-1984 9440565656 #3-11/A, Bowenpally,

    Hyderabad

    1010054213

    Shankar 32 M 23-07-1979 9440440440 #4-12/1/2, Malakpet,

    Hyderabad

    1010054214

    Ashutosh 31 M 05-08-1980 9866324324 #1-234, Chikkadpally,

    Hyderabad

    1010054215

    Aruna 20 F 04-05-1991 8011478531 #1-234, Chikkadpally,

    Hyderabad

    1010054216Anvesh 20 M 23-01-1991 8011478532 #2-89/A/1, Santoshnar,

    Hyderabad

    DataBase Tables creation

    Account Table

  • 7/30/2019 DDB Lab Record

    44/52

    MySQL> CREATE TABLE ACCOUNT (ACCOUNT_NO REFERENCES

    CUSTOMER(CUST_ID), ACCOUNT_TYPE varchar2(10), IFSC REFERENCES

    BANK(IFSC), DATE_OF_CREATING_ACCOUNT date,

    DATE_OF_DELETING_ACCOUNT date, AMOUNT number(11));

    MySQL> INSERT INTO ACCOUNT VALUES (1010054211, savings, 23239, 11-jan-

    1998, NULL,15000);

    MySQL> INSERT INTO ACCOUNT VALUES (1010054212, savings, 23239, 11-jun-

    2003, NULL,14000);

    MySQL> INSERT INTO ACCOUNT VALUES (1010054213, savings, 11007 30-jan-

    2008, NULL,23000);

    MySQL> SELECT * FROM ACCOUNT;

    ACCOUNT_NO ACCOUNT

    _TYPE

    IFSC DATE_OF_CREA

    TING_ACCOUN

    T

    DATE_OF_DE

    LETING_ACC

    OUNT

    AMOUNT

    1234A21012 Savings 23239 11-jan-1998 NULL 15,000

    123J90187K Joint 23239 11-jun-2003 NULL 14,000

    123FGT5678 Savings 11007 30-jan-2008 NULL 23,000

    1234M4567D Current 11557 28-apr-2003 NULL 11,000

    1234GH56710 Savings 45379 17-aug-2009 NULL 29,000

    1230978GFW Savings 11007 15-sep-2006 NULL 15,000

    Bank Table

    MySQL> CREATE TABLE BANK(BANK_ NAME varchar2(20),

    IFSC number(5) PRIMARYKEY, BRANCH varchar2(20), ADDRESS varchar2(30),

  • 7/30/2019 DDB Lab Record

    45/52

    PH_NO number(10));

    MySQL> INSERT INTO BANK

    VALUES(SBI,11007,Dilshuknagar,DSNR,9441808080);

    MySQL> INSERT INTO BANK VALUES(SBI,11667,Gaddiannaram,Gaddiannaram ,

    9440566022);

    MySQL> INSERT INTO BANK VALUES(SBI,23239,BNReddy Nagar,BNReddy

    Nagar, 9490997654);

    MySQL> INSERT INTO BANK VALUES(SBI,45379,Uppal,Uppal Xroads,9030547823);

    MySQL> SELECT * FROM BANK;

    BANK_NAME IFSC BRANCH ADDRESS PH_NO

    SBI 11007 Dilshuknagar DSNR 9441808080

    SBI 11557 Gaddiannaram Gaddiannaram 9440566022

    SBI 23239 BNReddy Nagar BNReddyNagar 9490997654

    SBI 45379 Uppal Uppal X roads 9030547823

    ADMINISTRATION TABLE

    EMP_ID EMP_NAME DESIGNATION DEPARTMENT

    10023 Nagaraju MANAGER

    10052 Raghava Asst MANAGER

    10072 Preetham CASHIER

    10084 Nagarjuna

  • 7/30/2019 DDB Lab Record

    46/52

    DEPOSITE TABLE

    ACCOUNT_NO Cust_id Cust_name Date of deposit Amount

    1234A21012 1010054211 Preetam 12-09-2010 120000

    123J90187K 1010054212 Sreeram 09-12-2010 130000

    123FGT5678 1010054213 Shankar 22-08-2010 3000

    1234M4567D 1010054214 Ashutosh 23-04-2010 40000

    WITHDRAW TABLE

    ACCOUNT_NO Cust_id Cust_name Date of

    withdraw

    Amount

    1234A21012 1010054211 Preetam 12-09-2010 120000

    123J90187K 1010054212 Sreeram 09-12-2010 130000

    123FGT5678 1010054213 Shankar 22-08-2010 3000

    1234M4567D 1010054214 Ashutosh 23-04-2010 40000

  • 7/30/2019 DDB Lab Record

    47/52

    TRANSFER TABLE

    CUST_NAME TO_ACCOUNT_

    NO

    FROM_ACCOUNT

    _NO

    DATE_OF_TR

    ANSFER

    AMOUNT

    Nagaraju 1010054211 1010054215 18-AUG-2009 5,000

    Aruna 1010054215 1010054213 31-DEC-2009 4,000

    Shankar 1010054213 1010054211 27-NOV-2007 8,000

    Anvesh 1010054216 1010054213 19-JUL-2007 6,000

    Week 7: Querying

    Practicing the some queries

    1. Display CUSTOMER NAME, CUSTOMER ID of all customers.MySQL> SELECT CUST_ID,CUST_NAME FROM CUSTOMER;

    CUSTOMER_ID CUST_NAME

    1010054211 Nagaraju

    1010054212 Sreeram

    1010054213 Shankar

    1010054214 Ashutosh

    1010054215 Aruna

    1010054216 Anvesh

    2. Display all the names of male customers.

  • 7/30/2019 DDB Lab Record

    48/52

    MySQL> SELECT CUST_NAME CUSTOMER WHERE GENDER=M;

    CUST_NAME

    Nagaraju

    Sreeram

    Shankar

    Ashutosh

    Anvesh

    3. Display names and address of all the customers.MySQL> SELECT CUST_NAME,ADDRESS FROM CUSTOMER;

    CUST_NAME ADDRESS

    Nagaraju #1-345, Narayanaguda, Hyderabad

    Sreeram #3-11/A, Bowenpally, Hyderabad

    Shankar #4-12/1/2, Malakpet, Hyderabad

    Ashutosh #1-234, Chikkadpally, Hyderabad

    Aruna #1-234, Chikkadpally, Hyderabad

    Anvesh #2-89/A/1, Santoshnar, Hyderabad

    4. Display the IFSC, bank name, branch details for all banks.MySQL> SELECT BANK_NAME,IFSC,BRANCH FROM BANK;

  • 7/30/2019 DDB Lab Record

    49/52

    BANK_NAME IFSC BRANCH

    SBI 11007 Dilshuknagar

    SBI 11557 Gaddiannaram

    SBI 23239 BNReddy Nagar

    SBI 45379 Uppal

    5. Display the account number of custmers whose names start with a and end withh.

    MySQL> SELECT CUST_ID,CUST_NAME FORM CUSTOMER WHERE NAME

    LIKE A%H;

    CUST_ID CUST_NAME

    1010054214 Ashuthosh

    1010054216 Anvesh

    6 . Find names of customers whose age is between 30 and 45.MySQL> SELECT CUST_NAME FROM CUSTMER WHERE AGE BETWEEN 30

    AND 45;

    CUST_NAME

    Nagaraju

    Ashuthosh

    Shankar

    7. Display all customer names whose name starts with a.MySQL>SELECT CUST_NAME FROM CUSTOMER WHERE NAME LIKEA%;

    CUST_NAME

  • 7/30/2019 DDB Lab Record

    50/52

    Ashutosh

    Aruna

    Anvesh

    8. Display sorted list of customer names.MySQL> SELECT CUST_NAME FROM CUSTOMER ORDER BY CUST_NAME;

    CUST_NAME

    Anvesh

    Aruna

    Ashutosh

    Nagaraju

    Preetam

    Shankar

    Sreeram

    Week 8 and 9: Querying (Continued)

    1. Write a query to display the customer name ,customer id, IFSCMySQL>SELECT C.CUST_ID,C.CUST_NAME,A.IFSC FROM ACCOUNT A,

    CUSTOMER C WHERE C.CUST_ID=A.ACCOUNT_NO;

    CUSTOMER_ID CUST_NAME IFSC

    1010054211 Nagaraju 23239

    1010054212 Sreeram 23239

  • 7/30/2019 DDB Lab Record

    51/52

    1010054213 Shankar 11007

    1010054214 Ashutosh 11557

    1010054215 Aruna 45379

    1010054216 Anvesh 11007

    2. Write a query to display account number where amount is less than 20,000.MySQL> SELECT ACCOUNT_NO FROM ACCOUNT WHERE AMOUNT SELECT COUNT(ACCOUNT_NO) FROM ACCOUNT HAVING

    IFSC=11007;

    COUNT(ACCOUNT_NO)

    2

    4. Display the account number, name, amount of all customers.MySQL>SELECT C.CUST_ID ACCOUNT_NO,C.CUST_NAME NAME,A.AMOUNT

    FROM CUSTOMER C, ACCOUNT A WHERE A.ACCOUNT_NO=C.CUST_ID;

    ACCOUNT_NO NAME AMOUNT

    1010054211 Nagaraju 15,000

  • 7/30/2019 DDB Lab Record

    52/52

    1010054212 Sreeram 14,000

    1010054213 Shankar 23,000

    1010054214 Ashutosh 11,000

    1010054215 Aruna 29,000

    1010054216 Anvesh 15,000

    5. Write a query to display the account numbers and IFSC where brach name isdilshuknagar.

    MySQL>SELECT A.ACCOUNT_NO, A.IFSC FROM ACCOUNT A, BANK B

    WHERE A.IFSC=B.IFSC AND B.BRANCH=Dilshuknagar;

    ACCOUNT_NO IFSC

    1010015F413 11007

    10100DS54216 11007