Rdbms & SQL Basics

33
Presentation for FDW

Transcript of Rdbms & SQL Basics

Page 1: Rdbms & SQL Basics

Presentation for FDW

Page 2: Rdbms & SQL Basics

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.

Page 3: Rdbms & SQL Basics

Advantages of RDBMS

Improved conceptual simplicity

Easier database design, implementation, management, and use

Ad hoc query capability (SQL)

Powerful database management system

Page 4: Rdbms & SQL Basics

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

Page 5: Rdbms & SQL Basics

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).

Page 6: Rdbms & SQL Basics

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

Page 7: Rdbms & SQL Basics

What is Database Normalization?

Cures the ‘Spreadsheet Syndrome’ Store only the minimal amount of

information. Remove redundancies. Restructure data.

Page 8: Rdbms & SQL Basics

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!

Page 9: Rdbms & SQL Basics

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)

Page 10: Rdbms & SQL Basics

Our Table

name phone1 phone2 email1 email2

Mike Hillyer

403-555-1717

403-555-1919

[email protected]

[email protected]

Tom Jensen

403-555-1919

403-555-1313

[email protected]

[email protected]

Ray Smith 403-555-1919

403-555-1111

[email protected]

user

namenicknamephone1phone2phone3cellpageraddresscityprovincepostal_codecountryemail1email2web_urlcompanydepartmentpicturenotesemail_format

Page 11: Rdbms & SQL Basics

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

Page 12: Rdbms & SQL Basics

One Solution

first_name

last_name

phone email

Mike Hillyer 403-555-1717

[email protected]

Mike Hillyer 403-555-1919

[email protected]

Tom Jensen 403-555-1919

[email protected]

Tom Jensen 403-555-1313

[email protected]

Ray Smith 403-555-1919

[email protected]

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

Page 13: Rdbms & SQL Basics

Satisfying 1NF

user

PK user_id

first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlcompanydepartmentpicturenotes

phone

PK phone_id

country_codenumberextension

email

PK email_id

address

Page 14: Rdbms & SQL Basics

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

Page 15: Rdbms & SQL Basics

Satisfying 2NF

email

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

email

PK address

FK1 user_id

user

PK user_id

first_namelast_namenicknameaddresscityprovincepostal_codecountryweb_urlpicturenotesemail_format

Page 16: Rdbms & SQL Basics

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

Page 17: Rdbms & SQL Basics

Satisfying 3NF

email

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

Page 18: Rdbms & SQL Basics

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.

Page 19: Rdbms & SQL Basics

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)

Page 20: Rdbms & SQL Basics

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).

Page 21: Rdbms & SQL Basics

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

Page 22: Rdbms & SQL Basics

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

Page 23: Rdbms & SQL Basics

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

Page 24: Rdbms & SQL Basics

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

Page 25: Rdbms & SQL Basics

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));

Page 26: Rdbms & SQL Basics

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

Page 27: Rdbms & SQL Basics

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,....)

Page 28: Rdbms & SQL Basics

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

Page 29: Rdbms & SQL Basics

The Delete Statement

The DELETE statement is used to delete rows in a table.Syntax

DELETE FROM table_name

WHERE column_name = some_value

Page 30: Rdbms & SQL Basics

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

Page 31: Rdbms & SQL Basics

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

Page 32: Rdbms & SQL Basics

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

Page 33: Rdbms & SQL Basics

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