Database Concepts

39
DWH Team

description

database concepts

Transcript of Database Concepts

  • An Introduction to Database ConceptsDATABASEDBMS & RDBMSDATABASE MODELSDATABASE OBJECTSNORMALIZATIONSQL COMMANDS & FUNCTIONSJOINS

    Topics Covered

  • DatabaseA database is a structured collection of records or data that is stored in a computer system so that a computer program or person can easily retrieve and manipulate the data using a query language.

  • Database Management System[DBMS]A database management system (DBMS) is computer software designed for creating and maintaining databases and allows users to retrieve information from that database. MSAccess, Foxpro,DBase

  • Relational Database Management System[RDBMS]A type of DBMS in which the database is organized and accessed according to the relationships between data values. It is based on the relational model.

    Oracle, DB2, MS SQL Server,Teradata

  • Database Model

    A database model is a theory or specification describing how a database is structured and used.

    Three types of Data models are

    Hierarchical Model Network model Relational Model

  • Hierarchical ModelHierarchical data model organizes data in a tree structure,hierarchy is of parent and child data segments.hierarchical model structures data as a tree of records, with each record having one parent record and many children

  • Network Data ModelNetwork data model organize data as a network and the network model allows each record to have multiple parent and child records.

  • Relational Data ModelA relational database allows the definition of data structures, storage and retrieval operations and integrity constraints. In such a database the data and relations between them are organised in tables.

  • Database ObjectsTable Table is a database object or structure where the data is stored.A table will contains rows and columns. A row in a table is called 'tuple' and the column in the table is called 'Attribute'.

    ViewA view is a virtual, dynamic or logical table computed or collated from data in the database.Changing the data in a table alters the data shown in the view.

  • Database ObjectsStored Procedure A stored procedure is a subroutine available to applications accessing a relational database system. Stored procedures are actually stored in the database.

    Trigger A database trigger is procedural code that is automatically executed in response to certain events on a particular table in a database. Triggers can restrict access to specific data, perform logging, or audit data modifications.

  • Database ObjectsIndex A database index is a data structure that improves the speed of operations in a table.Indexes can be created using one or more columns.The disk space required to store the index is typically less than the storage of the table , or audit data modifications.

    Types of Index are Unique Index Non-unique Index

  • Keys in a table

    Primary Key It is a unique and non-nullable attribute of the tableThe primary key of a relational table uniquely identifies each record in the table.

    Foreign keyIt is a field in a relational table that matches the primary key column of another table. Foreign key is used to establish and enforce a link between the data in two tables.

  • Keys in a table

    Candidate KeysA key that uniquely identifies rows in a table. Any of the identified candidate keys can be used as the table's primary key.Super Key A Candidate Key as a Super Key that contains only the minimum number of columns necessary to determine uniqueness.

  • Database Normalization It is a technique for designing relational database tables to minimize duplication of information.

    The goals of normalization are,Eliminating redundant dataEnsuring data dependencies make sense

  • Database Normalization Types of Normalization are 1st Normal form 2nd Normal form 3rd Normal form Boyce-Codd Normal Form 4th Normal form 5th Normal form

  • Database Normalization 1st Normal formA relation to be in 1NF, each column must contain only a single value and each row must contain the same columns. 2nd Normal formIn order to be in Second Normal Form, a relation must first fulfill the requirements to be in First Normal Form. Additionally, each nonkey attribute in the relation must be functionally dependent upon the primary key.

  • Database Normalization 3rd Normal formIn order to be in Third Normal Form, a relation must first fulfill the requirements to be in Second Normal Form. Additionally, all attributes that are not dependent upon the primary key must be eliminated. Boyce-Codd Normal FormA relation is in Boyce-Codd Normal Form (BCNF) if every determinant is a candidate key.

  • Database Normalization 4th Normal formTo be in Fourth Normal Form, a relation must first be in Boyce-Codd Normal Form. Additionally, a given relation may not contain more than one multivalued dependency. 5th Normal form A 4NF table is said to be in the 5NF if and only if every join dependency in it is implied by the candidate keys.

  • Structured Query Language[SQL]

    SQL is an ANSI standard computer language for accessing and manipulating Database systems.

    Data Definition Language[DDL] Data Manipulation Language[DML] Data Control Language [DCL] Transaction Control Language[TCL]

  • Data Definition Language [DDL]

    SQL statements that can be used either interactively or within programming language source code to define databases and their components.DDL commands are

    CREATE-Create tablesALTER-Changing the table definitionDROP-Drop tablesRENAME-Renaming tableTRUNCATE-Deletes the data

  • Data Manipulation Language [DML]

    SQL statements that can be used to manipulate the data in a relational table. It includes

    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

  • Data Control Language[DCL]

    SQL statements that can be used for control access to data in a database. It includes

    GRANT -to allow specified users to perform specified tasks. REVOKE- to cancel previously granted or denied permissions

  • Transaction Control Language[TCL]

    SQL statements that can be used for used to control transactional processing in a database

    COMMIT -to apply the transaction. ROLLBACK -to undo all changes of a transaction

  • Select Optionswhere -it is used to apply condition on queries

    group by- we can use the GROUP BY clause to divide the rows in a table into groups, to return summary information for each group.

    having-it is used in combination with the GROUP BY clause. It can be used in a SELECT statement to filter the records that a GROUP BY returns.

    order by-It allows to specify the order in which rows appear in the Result.

  • Functions in SQL

    Important types of functions String Functions Conversion Functions Mathematical Functions Date Functions

    The syntax for built-in SQL functions is, SELECT function(column) FROM table

  • String Functions

    The string functions allows to manipulate stringsSome String functions are

    length - Length of the specified string.concat - Concatenate two strings together.substr -Used to extract a substring from a stringupper - Converts all letters uppercasetrim - Removes all specified characters either from the beginning or the ending of a string

  • Conversion Functions

    Conversion functions are used to convert the datatype.Some Conversion functions are

    Cast-converts one datatype to another.to_char-converts a number or date to a stringto_date-converts a string to a date.to_number-converts a string to a number.to_lob -converts LONG or LONG RAW values to LOB values.Convert -converts a string from one character set to another

  • Aggregation Functions

    Aggregate functions operate against a collection of values, but return a single value. Some Aggregation functions are

    avg(column)-Returns the average value of a columncount(column)-Returns the number of rows max(column)- Returns the highest value of a columnfirst(column)-Returns the value of the first record in a specified field sum(column)- Returns the total sum of a column

  • Date Functions

    Date functions used to manipulate date & time values. Some Date functions are

    current_date-returns the current date last_day-returnsthe last day of the month based on a date valuenext_day-returns the first weekdayto_date-converts a string to a dateRound- returns a date rounded to a specific unit of measure

  • rowid -it is a pseudo column that uniquely identifies a row within a table, but not within a database.rownum -For each row returned by a query, the ROWNUM pseudocolumn returns a number indicating the order in which Oracle selects the row from a table.coalesce function -the coalesce function returns the first non-null expression in the list. If all expressions evaluate to null, then the coalesce function will return null.sample -sample in queries used for getting sample output.Some other Commands / Functions

  • Set Operations in Database

    Union : the UNION operator combines the results of two SQL queries into a single table of all matching rows. The two queries must have the same number of columns and compatible data types in order to join them. Any duplicate records are automatically removed unless UNION ALL is used.

    Intersect: SQL INTERSECT operator takes the results of two queries and returns only rows that appear in both result sets.

    Except : The SQL EXCEPT operator takes the distinct rows of one query and returns the rows that do not appear in a second result set.

  • Joins

    A JOIN clause in SQL combines records from two tables in a relational database and results in a new (temporary) table, also called a "joined table".

    Structured Query Language specifies two types of joins: Inner join and Outer join

  • Inner Joins INNER JOIN returns all rows from both tables where there is a match. Inner join is subdivided into three typesEqui-joinNatural joinCross join

  • Inner JoinsEqui-join: The type of Join which links the columns of two tables using an equal relationship.Natural join: An inner join in which redundant columns are eliminated.Cross join : A cross join (or Cartesian Product join) will return a result table where each row from the first table is combined with each row from the second table. The number of rows in the result table is the product of the number of rows in each table

  • Outer JoinsA Join operation in which all source records are included in the result, even those that don't satisfy the join condition

    The Outer joins subdivided in to Left outer join Right outer join Full outer join

  • Outer JoinsLeft outer join : The resultant table will contain all the records from the first table plus the records from second table which satisfying the join condition.Right outer join : The resultant table will contain all the records from the second table plus the records from first table which satisfying the join conditionFull outer join : A full outer join combines the results of both left and right outer joins. The joined table will contain all records from both tables

  • Joins Diagrams