20r6-L7 TO 20L8-19) - Rise Krishna Sai Prakasam Group Of ...
L7
Transcript of L7
Outline
Database definition, Functionalities and application of database,
Sample db Tables, fields, values Entity, attributes, keys
Relationships DBMS Language and querying
Database: Definition Data: known facts that can be recorded
Database: A collection of described and interrelated data stored and managed by general purpose DBMS.
represents some aspect of the real world logically coherent collection designed, built & populated for a specific purpose
Database Management System: the software that manages the data
Database: Functionalities Data Storage Management Data Transformation and Presentation Security Multi-user Access Control Backup and Recovery Data Integrity Database Access Language Database Communication Interface
Database: Applications
Banking: transactions Airlines: reservations, schedules Universities: registration, grades Sales: customers, products, purchases Online retailers: order tracking, customized recommendations
Manufacturing: production, inventory, orders, supply chain
Human resources: employee records, salaries, tax deductions
Almost all applications we use in real world has database!
Database tables, fields, values In a database, all the data are stored in a table. For example, Student table.
Columns contains what information is to be stored, which are called fields.
Each row is called a record, where, values for each field is stored.
Entity An entity is a thing of interest to a system about which information is stored. For example in a Hospital Administration System, some likely entities would include: Patient, Doctor, Operation, Ward Each of these things are of interest to the system and would have data stored about them.
ExampleA sale always starts with a customer receiving an estimate.The estimate then becomes an order. An order can be for one or more stock items. Each stock item belongs to a certain stock category (e.g. taps, sinks, cupboards etc.)
Entities, attributes and relationships
Entities, attributes and relationshipsAttributes An attribute is an item of data held about an entity
In computer systems an attribute is a field of information
Example : Sales System
Entity : Customer Attributes : Customer Name, Customer Address, Customer Phone Number
Entities, attributes and relationshipsRelationships A relationship is an association among several entities. Example: students are enrolled in courses. Instructor teaches courses.
Example : 44553 (Peltier) advisor 22222
(Einstein) student entity relationship set
instructor entity
A Cardinality Constraint defines when a certain relation can only have a certain number of instances for a relationship.
Different types of cardinalities are:
One-to-one: Where one instance in an entity refers to one and only one instance in the related entity. Denoted using 1 in both end in the diagram:
Entities, attributes and relationshipsRelationships
Many-to-one: Where many instance of an entity can relate to only one instance of the second entity.
One-to-many: One instance in an entity refers to one or more instances in the related entity.
Many-to-many: Exists when one instance of the first entity (parent) can relate to many instances of the second entity (child), and one instance of the second entity can relate to many instances of the first entity.
KeysA key attribute is used to identify an specific record of an entity.
Keys are of four types-Super Key: A super key of an entity set is a set of
one or more attributes whose values uniquely determine each entity. Example:
{Student ID,FirstName }{Student ID, LastName }{Student ID,FirstName,LastName}
Candidate Key: A candidate key of an entity set is a minimal super key. Since a super key is defined as a set of attributes, we can define a candidate key as a minimal super key of which no proper subset is possible. Example:
{StudentID}{Last Name, First Name}
KeysPrimary key: A primary key is a candidate key and a
single field that is most appropriate to be the main reference key for the table.
Example: {StudentID} Foreign Key: A foreign key is generally a primary
key from one table that appears as a field in another table to establish a relation between the first and second table.
Example: {CourseId}
Query: DBMS Languages DDL: Data Definition Language
used to define/change the structure of the database. Example: create, alter and delete database or table etc.
DML: Data Manipulation Language used to query the database, change data in the database. Example: insert, delete, update data, select information from the database.
Most commonly used database language is SQL(Structured Query Language) which has both DDL and DML configuration.
Data Types in SQL char(n). Fixed length character string, with user-specified length n.
varchar(n). Variable length character strings, with user-specified maximum length n.
int. Integer (a finite subset of the integers that is machine-dependent).
float(n). Floating point number, with user-specified precision of at least n digits. etc.
Create Database CREATE DATABASE dbname; Example: CREATE DATABASE my_db;
CREATE TABLE table_name(column_name1 data_type(size),column_name2 data_type(size),....);
Example: create table instructor ( ID char(5), name varchar(20) not null, dept_name varchar(20), salary numeric(8,2))
Create Table
SQL Syntax
DROP TABLE table_name;DROP DATABASE database_name;
Example: drop database Student_db; drop table instructor;
Delete Database/Table
SQL Syntax
SQL Syntax
INSERT INTO table_nameVALUES (value1,value2,value3,...);
Example: insert into instructor values (‘10211’, ’Smith’, ’Biology’, 66000);
insert into instructor values (‘10211’, null, ’Biology’, 66000);
Insert into table
SQL Syntax
A typical SQL query has the form:
select A1, A2, ..., An
from r1, r2, ..., rm
where P
Ai represents an attribute (desired column names)
Ri represents a relation (table names) P is a predicate (Condition)
Basic Query Structure
SQL Syntax SELECT column1, column2, …columnN FROM table_name WHERE condition;
find the names of all instructors:select namefrom instructor
find all the attributes of all instructors: select *from instructor
To find all instructors in Comp. Sci. dept with salary > 80000 select name
from instructorwhere dept_name = ‘Comp. Sci.' and salary >
80000