Upload
shruthig29111988
View
223
Download
1
Embed Size (px)
Citation preview
Presentation for FDW
What is RDBMS ?
The Relational Database Model:
Relational database management systems, where all data are kept in tables or relations.
More flexible & easy to use.
Almost any item of data can be accessed more quickly than the other models.
Retrieval time is reduced so that interactive access becomes more feasible.
Advantages of RDBMS
Improved conceptual simplicity
Easier database design, implementation, management, and use
Ad hoc query capability (SQL)
Powerful database management system
Disadvantages of RDBMS
Possibility of poor design and implementation
Relational databases do not have enough storage area to handle data such as images, digital and audio/video.
The requirement that information must be in tables where relationships between entities are defined by values
Relational Database: Definitions
Relational database: a set of normalized relations with distinct relation names.
Relation: made up of 2 parts: Instance : a table, with rows and columns.
#Rows = cardinality, #fields = degree / arity. Schema : specifies name of relation, plus name and
type of each column.
E.G. Students(sid: string, name: string, login: string, age: integer, gpa: real).
Example Instance of Students Relation
sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8
Cardinality = 3, degree = 5, all rows distinct
What is Database Normalization?
Cures the ‘Spreadsheet Syndrome’ Store only the minimal amount of
information. Remove redundancies. Restructure data.
Benefits of Database Normalization? Decreased storage requirements!
1 VARCHAR(20) converted to 1 TINYINT UNSIGNED in a table of 1 million rows is a savings of ~20 MB Faster search performance!
Smaller file for table scans. More directed searching.
Improved data integrity!
What are the Normal Forms?
First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) Boyce-Codd Normal Form (BCNF) Fourth Normal Form (4NF) Fifth Normal Form (5NF)
Our Table
name phone1 phone2 email1 email2
Mike Hillyer
403-555-1717
403-555-1919
Tom Jensen
403-555-1919
403-555-1313
Ray Smith 403-555-1919
403-555-1111
user
namenicknamephone1phone2phone3cellpageraddresscityprovincepostal_codecountryemail1email2web_urlcompanydepartmentpicturenotesemail_format
First Normal Form Remove horizontal redundancies
No two columns hold the same information No single column holds more than a single
item Each row must be unique
Use a primary key Benefits
Easier to query/sort the data More scalable Each row can be identified for updating
One Solution
first_name
last_name
phone email
Mike Hillyer 403-555-1717
Mike Hillyer 403-555-1919
Tom Jensen 403-555-1919
Tom Jensen 403-555-1313
Ray Smith 403-555-1919
Ray Smith 403-555-1111
• Multiple rows per user• Emails are associated with only one other phone• Hard to Search
user
first_namelast_namenicknamephonecellpageraddresscityprovincepostal_codecountryweb_urldepartmentpicturenotes
Satisfying 1NF
user
PK user_id
first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlcompanydepartmentpicturenotes
phone
PK phone_id
country_codenumberextension
PK email_id
address
Second Normal Form Table must be in First Normal Form Remove vertical redundancy
The same value should not repeat across rows Composite keys
All columns in a row must refer to BOTH parts of the key
Benefits Increased storage efficiency Less data repetition
Satisfying 2NF
PK address
typeFK1 user_id
phone
PK phone_id
country_codenumberextensiontype
user
PK user_id
first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotes
user_phone
PK,FK1 user_idPK,FK2 phone_id
company
PK company_id
name
user_company
PK,FK1 user_idPK,FK2 company_id
department
PK address
FK1 user_id
user
PK user_id
first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotesemail_format
Third Normal Form Table must be in Second Normal Form
If your table is 2NF, there is a good chance it is 3NF
All columns must relate directly to the primary key
Benefits No extraneous data
Satisfying 3NF
PK address
FK1 user_idformat
phone
PK phone_id
country_codenumbertype
user
PK user_id
first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotes
user_phone
PK,FK1 user_idPK,FK2 phone_id
extension
company
PK company_id
name
user_company
PK,FK1 user_idPK,FK2 company_id
department
Relational Query Languages
A major strength of the relational model: supports simple, powerful querying of data.
Queries can be written intuitively, and the DBMS is responsible for efficient evaluation.
05/03/23
What is SQL?
When a user wants to get some information from a database file, he can issue a query.
A query is a user–request to retrieve data or information with a certain condition.
SQL is a query language that allows user to specify the conditions. (instead of algorithms)
05/03/23
Concept of SQL
The user specifies a certain condition.
The result of the query will then be stored in form of a table.
Statistical information of the data.
The program will go through all the records in the database file and select those records that satisfy the condition.(searching).
SQL Data Definition Language (DDL)The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables.
The most important DDL statements in SQL are:
•CREATE TABLE - creates a new database table •ALTER TABLE - alters (changes) a database table •DROP TABLE - deletes a database table •CREATE INDEX - creates an index (search key) •DROP INDEX - deletes an index
SQL Data Manipulation Language (DML)SQL (Structured Query Language) is a syntax for executing queries. But the SQL language also includes a syntax to update, insert, and delete records.These query and update commands together form the Data Manipulation Language (DML) part of SQL:
•SELECT - extracts data from a database table •UPDATE - updates data in a database table •DELETE - deletes data from a database table •INSERT INTO - inserts new data into a database table
05/03/23
Basic structure of an SQL queryBasic structure of an SQL query
GeneralStructure
SELECT, ALL / DISTINCT, *,AS, FROM, WHERE
Comparison IN, BETWEEN, LIKE "% _"
Grouping GROUP BY, HAVING,COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
Display Order ORDER BY, ASC / DESC
LogicalOperators
AND, OR, NOT
Output INTO TABLE / CURSORTO FILE [ADDITIVE], TO PRINTER, TO SCREEN
Union UNION
SQL Database Tables
A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.Below is an example of a table called "Persons":
LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger
SQL CREATE STATEMENTThe create statement is used to create a table.
The SQL syntax for CREATE TABLE isCREATE TABLE "table_name"
("column 1" "data_type_for_column_1","column 2" "data_type_for_column_2",... )
So, if we are to create the persons table specified as above, we would type in
CREATE TABLE persons(lastname varchar2(50),firstname varchar2(50),address varchar2(50),City varchar2(50));
SQL The SELECT Statement
The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set).
SyntaxSELECT column_name(s)FROM table_name
SELECT LastName FROM Persons
Gives a result set like this: LastNameHansenSvendsonPettersen
The INSERT INTO StatementThe INSERT INTO statement is used to insert new rows into a table.
Syntax
INSERT INTO table_nameVALUES (value1, value2,....)
You can also specify the columns for which you want to insert data:
INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)
The Update Statement
The UPDATE statement is used to modify the data in a table.Syntax
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value
The Delete Statement
The DELETE statement is used to delete rows in a table.Syntax
DELETE FROM table_name
WHERE column_name = some_value
Update one Column in a RowLastName FirstName Address CityNilsen Fred Kirkegt 56 StavangerRasmussen Storgt 67
We want to add a first name to the person with a last name of "Rasmussen":
UPDATE Person SET FirstName = 'Nina'WHERE LastName = 'Rasmussen'
LastName FirstName Address CityNilsen Fred Kirkegt 56 StavangerRasmussen Nina Storgt 67
Update several Columns in a Row
We want to change the address and add the name of the city:
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'WHERE LastName = 'Rasmussen'
LastName FirstName Address CityNilsen Fred Kirkegt 56 StavangerRasmussen Nina Stien 12 Stavanger
LastName FirstName Address CityNilsen Fred Kirkegt 56 StavangerRasmussen Storgt 67
Delete a RowLastName FirstName Address CityNilsen Fred Kirkegt 56 StavangerRasmussen Nina Stien 12 Stavanger
"Nina Rasmussen" is going to be deleted:
DELETE FROM Person WHERE LastName = 'Rasmussen'
LastName FirstName Address CityNilsen Fred Kirkegt 56 Stavanger
Delete All Rows
It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact:
DELETE FROM table_name
Or
DELETE * FROM table_name