Escolar Documentos
Profissional Documentos
Cultura Documentos
SQL is short for Structured Query Language Microsoft SQL Server (Microsoft Corporation)
SQL is a declarative programming language for interaction DB2 (IBM)
with Relational Database Management Systems (RDBMS) Oracle (Oracle Corporation)
SQL is pronounced as either "S-Q-L" or "sequel" MySQL (Oracle Corporation)
SQL was originally developed at IBM in the 1970s. PostgreSQL (Open Source)
RDBMS store data in tables Microsoft Access (Microsoft Corporation)
ramnibas@hotmail.com
Primary Key – It is a column or set of columns in a table that uniquely identifies tuples (rows) in that table.
Super Key – A super key is a set of one of more columns (attributes) to uniquely identify rows in a table.
Candidate Key – A super key with no redundant attribute is known as candidate key
Alternate Key – Out of all candidate keys, only one gets selected as primary key, remaining keys are known as
alternate or secondary keys.
Composite Key – A key that consists of more than one attribute to uniquely identify rows (also known as
records & tuples) in a table is called composite key.
Foreign Key – Foreign keys are the columns of a table that points to the primary key of another table. They act
as a cross-reference between tables.
Natural keys. A natural key is one or more existing data attributes that are unique to the business
concept. For the Customer table there was two candidate keys, in this
case CustomerNumber and SocialSecurityNumber.
Surrogate key. Introduce a new column, called a surrogate key, which is a key that has no business
meaning. Addresses don't have an "easy" natural key because you would need to use all of the columns of
the Address table to form a key for itself (you might be able to get away with just the combination
of Street and ZipCode depending on your problem domain), therefore introducing a surrogate key is a much
better option in this case.
The primary key is a unique key in your table that you choose that best uniquely identifies a record in the
table. All tables should have a primary key, because if you ever need to update or delete a record you need
to know how to uniquely identify it.
A surrogate key is an artificially generated key. They're useful when your records essentially have no natural
key (such as a Person table, since it's possible for two people born on the same date to have the same name,
or records in a log, since it's possible for two events to happen such they they carry the same timestamp).
Most often you'll see these implemented as integers in an automatically incrementing field, or as GUIDs that
are generated automatically for each record. ID numbers are almost always surrogate keys.
CARDINALITY
In terms of data modeling, cardinality refers to the relationship between two tables. They can be of four types:
One to One – A single row of table 1 associates with single row of table 2
One to Many – A single row of table 1 associates with more than one rows of table 2
Many to One – Many rows of table 1 associate with a single row of table 2
Many to Many – Many rows of table 1 associate with many rows of table 2
be having the low cardinality. These cardinality scores helps in query optimization.
ACID Property:
To ensure the integrity of data during a transaction the database system maintains ACID properties:
Atomicity: This property ensures that either all the operations of a transaction reflect in database or
none.
Consistency: To preserve the consistency of database, the execution of transaction should take place in
isolation (that means no other transaction should run concurrently when there is a transaction already
running
Isolation: For every pair of transactions, one transaction should start execution only when the other
finished execution.
Durability: Once a transaction completes successfully, the changes it has made into the database should
be permanent even if there is a system failure. The recovery-management component of database
systems ensures the durability of transaction.
Types of constraints
NOT NULL : NOT NULL constraint makes sure that a column does not hold NULL value
UNIQUE : If a column has a unique constraint, it means that column cannot have duplicate values in a table.
DEFAULT: The DEFAULT constraint provides a default value to a column when there is no value provided
while inserting a record into a table.
CHECK: It ensures that the specified column must have the value satisfying the cond. Eg : ROLL_NO INT NOT
NULL CHECK(ROLL_NO >1000)
Key Constraints – PRIMARY KEY, FOREIGN KEY
Primary key uniquely identifies each record in a table. It must have unique values and cannot contain nulls. In
the below example the ROLL_NO field is marked as primary key, that means the ROLL_NO field cannot have
duplicate and null values.
CREATE TABLE STUDENT(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO) );
Foreign key:
Foreign keys are the columns of a table that points to the primary key of another table. They act as a cross-
reference between tables.
Joining types
Inner Join: Inner join return rows when there is at least one match of rows between the tables.
Right Join: Right join return rows which are common between the tables and all rows of Right hand side table.
Simply, it returns all the rows from the right hand side table even though there are no matches in the left hand
side table.
Left Join: Left join return rows which are common between the tables and all rows of Left hand side table.
Simply, it returns all the rows from Left hand side table even though there are no matches in the RHS table.
Full Join: Full join return rows when there are matching rows in any one of the tables. This means, it returns all
the rows from the left hand side table and all the rows from the right hand side table.
What is normalization?
Normalization is the process of minimizing redundancy and dependency by organizing fields and table of a
database. The main aim of Normalization is to add, delete or modify field that can be made in a single table.
The normal forms can be divided into 4 forms, and they are explained below -.
First Normal Form (1NF): This should remove all the duplicate columns from the table. Creation of tables for
the related data and identification of unique columns.
Second Normal Form (2NF): Meeting all requirements of the first normal form. Placing the subsets of data in
separate tables and Creation of relationships between the tables using primary keys.
Third Normal Form (3NF): This should meet all requirements of 2NF. Removing the columns which are not
dependent on primary key constraints.
Fourth Normal Form (4NF): Meeting all the requirements of third normal form and it should not have
multivalued dependencies.
What is an Index? What are all the different types of indexes?
An index is performance tuning method of allowing faster retrieval of records from the table. This indexing
does not allow the field to have duplicate values if the column is unique indexed. Unique index can be applied
automatically when primary key is defined.
Clustered Index: This type of index reorders the physical order of the table and search based on the key
values.Each table can have only one clustered index.
Non Clustered Index: NonClustered Index does not alter the physical order of the table and maintains logical
order of data. Each table can have 999 non clustered indexes .
Eg:CREATE INDEX idx_age ON CUSTOMERS ( AGE );
What is a trigger? What are the types of Triggers?
A DB trigger is a code or programs that automatically execute with response to some event on a table or view
in a database. Mainly, trigger helps to maintain the integrity of the database.
Example: When a new student is added to the student database, new records should be created in the related
tables like Exam, Score and Attendance tables.
• Insert
• Delete
• Update
• Instead of
Difference between function and Procedure
A procedure does not have a return value, whereas a function has.
Example:
CREATE OR REPLACE PROCEDURE my_proc
(p_name IN VARCHAR2 := 'John') as begin ... end