Академический Документы
Профессиональный Документы
Культура Документы
Entity: The object in the ER Model 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 related among themselves.
e.g.
Entity_1
Relates
Entity_2
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.
Divorced Marital status Single
Married
Widowed
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 cant be broken-down into small components.
AGE
Date
Month
Year
Single valued Attribute: The attribute can take on only a single value for each entity instance.
Phno
Multi-valued Attribute: An attribute that may take more than one value for a given entity. It is represented with double ellipse.
Addr Pincode
Stored Attribute: Attribute that need to be stored permanently. Eg: Name of employee
Derived Attribute: Attribute that can be calculated based on other attributes. It is represented with dotted lines.
EMP
Joining Date
Current Date
Keys:
Partial key: A weak entity type normally has a partial key which is the set of attributes that can uniquely identify weak entity that are related to the same owner entity.
Foreign key: A foreign key (FK) is an attribute or combination of attributes that is used to
establish 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. A
primary key that made up of more than one attribute is known as a composite key. For our database we identify entities as below. o o o o o Bus: Primary key: BusNo and ServiceNo Passenger: Primary key: PassengerId, Foreign key: TicketId Ticket: Primary key: TicketId, Foreign key: BusNo Reservation Cancellation
Customer Entity:
Account Entity:
Bank Entity:
Administration Entity:
Deposit Entity:
Withdraw Entity:
Transfer Entity:
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
E R Diagrams :
Create account
Close account
Check Balance
Deposit
Withdraw
Transfer
Management
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.
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.
Customer
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
Account Accoun_ no: varcha2(10) PRIMARY KEY Account_type: varchar2(10) Date_o_creating_account : date Date_of_closing_account : date Bank table schema: IFSC Bank_name Branch Ph_no Address
Emp_id
Emp_name
Designation
Department
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) Transfer schema: TO FROM _ACCOUNT _ACCOUT_NO Amount _NO
Cust_name Transfer
Date_of_transfer
Cust_name : varchar2(20)
Week4: Normalization
Database 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:
1st Normal Form: 1NF requires that the values in each column of a table are atomic. By atomic 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 the record into two records. Cust_id cust_name Age gender DOB Phno ADDRESS Preetam 1010054211 Sreeram 1010054212 Shankar 1010054213 Ashutosh 1010054214 Aruna 1010054215 Anvesh 1010054216 Saidatta 1010054217 31 M 03-12-1982 8066952326 20 M 23-01-1991 8011478532 20 F 04-05-1991 8011478531 31 M 05-08-1980 9866324324 32 M 23-07-1979 9440440440 27 M 02-12-1984 30 M 03-04-1981 9949887766 9873838272 9440565656 #1-345, Narayanaguda, Hyderabad #3-11/A, Bowenpally, Hyderabad #4-12/1/2, Malakpet, Hyderabad #1-234, Chikkadpally, Hyderabad #1-234, Chikkadpally, Hyderabad #2-89/A/1, Santoshnar, Hyderabad #2-45/6/2, Lungarhouse, Hyderabad
9949887766
#1-345, Narayanaguda, Hyderabad #1-345, Narayanaguda, Hyderabad #3-11/A, Bowenpally, Hyderabad Malakpet,
9873838272
1010054212
Sreeram
27
02-12-1984 23-07-1979
9440565656
1010054213
Shankar Ashutosh
32
M 05-08-1980
31
M 23-01-1991 8011478531
#1-234, Chikkadpally, Hyderabad #1-234, Chikkadpally, Hyderabad #2-89/A/1, Santoshnar, Hyderabad #2-89/A/1, Santoshnar, Hyderabad
20
F 23-01-1991 8011478532
20
M 03-12-1982 8066952326
29
2nd Normal Form: where the 1NF deals with atomicity of data, the 2NF deals with relationships between composite key columns and non-key columns. To achieve 2NF the tables 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 no Partial 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.
1234M4567D 1010054214
ACCOUNT_NO DATE OF DEPOSIT,AMOUNT ACCOUNT_NO DATE OF DEPOSIT 1234A21012 123J90187K 123FGT5678 1234M4567D 12-09-2010 09-12-2010 22-08-2010 23-04-2010 120000 130000 3000 40000 AMOUNT
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.The following 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 03-04-1981 1010054211 Preetam 30 M PHN_N0 9873838272 ADDRESS #1-345, Narayanaguda, Hyderabad #3-11/A, Bowenpally, Hyderabad
1010054212
Sreeram
27
02-12-1984 23-07-1979
9440565656
1010054213
Shankar Ashutosh
32
M 05-08-1980
9440440440 #4-12/1/2,Malakpet, Hyderabad 9866324324 #1-234, Chikkadpally, Hyderabad #1-234, Chikkadpally, Hyderabad #2-89/A/1, Santoshnar, Hyderabad #2-89/A/1, Santoshnar, Hyderabad
31
M 23-01-1991 8011478531
20
F 23-01-1991 8011478532
20
M 03-12-1982 8066952326
29
CUST_ID DOB Cust_id cust_name gender DOB 03-04-1981 1010054211 Preetam M PHN_N0 9873838272 ADDRESS #1-345, Narayanaguda, Hyderabad #3-11/A, Bowenpally, Hyderabad #412/1/2,Malakpet, Hyderabad #1-234, Chikkadpally, Hyderabad
1010054212
Sreeram
02-12-1984 23-07-1979
9440565656
9440440440
1010054213
Shankar Ashutosh
M 05-08-1980 M 9866324324
gender
Age
PHN_N0 9873838272
ADDRESS #1-345, Narayanaguda, Hyderabad #3-11/A, Bowenpally, Hyderabad #412/1/2,Malakpet, Hyderabad #1-234, Chikkadpally,
1010054211
Preetam
30
Hyderabad
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.
Windows Installer Installation: After downloading the Windows Installer 3.1 software run the set up.
Start the set up wizard and click NEXT. Next click on the LICENSE agreement to ACCEPT and again click NEXT. Now updating the system process starts. After installing the Windows Installer 3.1 software click on FINISH.
.NET Framework Installation: Now start the installation process of .net frame work to install Mysql Work Bench. Start the set up wizard. Next ACCEPT the LICENCE AGREEMENT and click INSTALL. Now the DOWNLOAD and INSTALLATION process starts. Before INSTALLATION, it first DOWNLOADS the components of .net frame work from internet and then starts the INSTALLATION process. After completing the INSTALLATION process click on EXIT and the setup is completed.
MySQL Work Bench Installation: Run the setup. It will show setup wizard like the following window.
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 click NEXT. Now click on INSTALL to start the installation process. Now the INSTALLATION process starts. Now click on FINISH to complete the setup wizard. The overall INSTALLATION process has been completed. And we can perform QUERIES by using MySQL Work Bench. We are going to execute the queries by using MySQL server.
MySQL Server Installation: Step 1: Download mysql server essential from the website www.mysql.com/downloads and save the .exe file.
Steps 4&5:
Steps: 6&7
Steps 8&9:
Steps: 10&11
Step: 12&13
Steps: 14&15
Steps: 16&17
Steps: 18&19
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));
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. Field CUST_ID CUST_NAME AGE DOB GENDER PH_NO ADDRESS Type NUMBER(11) Null NO Key PRI Not Null Not Null Not Null Not Null Not Null Not Null Default Extra
VARCHAR2(20) YES NUMBER(2) DATE VARCHAR2(2) NUMBER(10) YES YES YES YES
VARCHAR2(40) YES
* 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;
e.g.
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 Preetam
AG E 30
GEND ER M
DOB 03-04-1981
PHNO 9949887766
ADDRESS #1-345, Narayanaguda, Hyderabad #3-11/A, Bowenpally, Hyderabad #4-12/1/2, Malakpet, Hyderabad #1-234, Chikkadpally, Hyderabad #1-234, Chikkadpally, Hyderabad #2-89/A/1, Santoshnar, Hyderabad
1010054211 Sreeram 1010054212 Shankar 1010054213 Ashutosh 1010054214 Aruna 1010054215 Anvesh 1010054216 20 M 23-01-1991 8011478532 20 F 04-05-1991 8011478531 31 M 05-08-1980 9866324324 32 M 23-07-1979 9440440440 27 M 02-12-1984 9440565656
Saidatta 1010054217
31
03-12-1982
8066952326
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 Nagaraju
AG E 30
GEND ER M
DOB 03-04-1981
PHNO 9949887766
ADDRESS #1-345, Narayanaguda, Hyderabad #3-11/A, Bowenpally, Hyderabad #4-12/1/2, Malakpet, Hyderabad #1-234, Chikkadpally, Hyderabad #1-234, Chikkadpally, Hyderabad #2-89/A/1, Santoshnar, Hyderabad #2-45/6/2, Lungarhouse, Hyderabad
1010054211 Sreeram 1010054212 Shankar 1010054213 Ashutosh 1010054214 Aruna 1010054215 Anvesh 1010054216 Saidatta 1010054217 31 M 03-12-1982 8066952326 20 M 23-01-1991 8011478532 20 F 04-05-1991 8011478531 31 M 05-08-1980 9866324324 32 M 23-07-1979 9440440440 27 M 02-12-1984 9440565656
Delete: It is used to delete the records from the tables and the space will remain same in the table with empty space.
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 Nagaraju
AG E 30
GEND ER M
DOB 03-04-1981
PHNO 9949887766
ADDRESS #1-345, Narayanaguda, Hyderabad #3-11/A, Bowenpally, Hyderabad #4-12/1/2, Malakpet, Hyderabad #1-234, Chikkadpally, Hyderabad #1-234, Chikkadpally, Hyderabad #2-89/A/1, Santoshnar, Hyderabad
1010054211 Sreeram 1010054212 Shankar 1010054213 Ashutosh 1010054214 Aruna 1010054215 Anvesh 1010054216 20 M 23-01-1991 8011478532 20 F 04-05-1991 8011478531 31 M 05-08-1980 9866324324 32 M 23-07-1979 9440440440 27 M 02-12-1984 9440565656
MySQL>
CREATE
TABLE
ACCOUNT
REFERENCES REFERENCES
CUSTOMER(CUST_ID),
ACCOUNT_TYPE
BANK(IFSC), DATE_OF_CREATING_ACCOUNT date, DATE_OF_DELETING_ACCOUNT date, AMOUNT number(11)); MySQL> INSERT INTO ACCOUNT VALUES (1010054211, savings, 23239, 11-jan1998, NULL,15000); MySQL> INSERT INTO ACCOUNT VALUES (1010054212, savings, 23239, 11-jun2003, NULL,14000); MySQL> INSERT INTO ACCOUNT VALUES (1010054213, savings, 11007 30-jan2008, NULL,23000);
ACCOUNT_NO
ACCOUNT _TYPE
IFSC
DATE_OF_CREA DATE_OF_DE TING_ACCOUN T LETING_ACC OUNT NULL NULL NULL NULL NULL NULL
AMOUNT
Bank Table MySQL> CREATE TABLE BANK(BANK_ NAME varchar2(20), IFSC number(5) PRIMARYKEY, BRANCH varchar2(20), ADDRESS varchar2(30),
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 X roads,9030547823);
ADMINISTRATION TABLE EMP_ID 10023 10052 10072 10084 EMP_NAME Nagaraju Raghava Preetham Nagarjuna DESIGNATION MANAGER Asst MANAGER CASHIER DEPARTMENT
DEPOSITE TABLE
WITHDRAW TABLE
TRANSFER TABLE
CUST_NAME
TO_ACCOUNT_ NO
AMOUNT
Week 7: Querying
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;
ADDRESS #1-345, Narayanaguda, Hyderabad #3-11/A, Bowenpally, Hyderabad #4-12/1/2, Malakpet, Hyderabad #1-234, Chikkadpally, Hyderabad #1-234, Chikkadpally, Hyderabad #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;
5. Display the account number of custmers whose names start with a and end with h. MySQL> SELECT CUST_ID,CUST_NAME FORM CUSTOMER WHERE NAME LIKE A%H;
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
8. Display sorted list of customer names. MySQL> SELECT CUST_NAME FROM CUSTOMER ORDER BY CUST_NAME;
2. Write a query to display account number where amount is less than 20,000. MySQL> SELECT ACCOUNT_NO FROM ACCOUNT WHERE AMOUNT <20000;
3. Display the number of accounts in the bank IFSC is 11007. MySQL> 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 1010054211
NAME Nagaraju
AMOUNT 15,000
5. Write a query to display the account numbers and IFSC where brach name is dilshuknagar. MySQL>SELECT A.ACCOUNT_NO, A.IFSC FROM ACCOUNT A, BANK B WHERE A.IFSC=B.IFSC AND B.BRANCH=Dilshuknagar;