0% found this document useful (0 votes)
149 views

SQL Viva Questions

The document discusses SQL primary keys and foreign keys. It states that a primary key uniquely identifies records in a table and cannot contain null values. A foreign key in one table points to the primary key in another table to link the tables together and ensure referential integrity of data. The document provides examples of creating primary key and foreign key constraints when creating tables in SQL.

Uploaded by

manish_delhi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

SQL Viva Questions

The document discusses SQL primary keys and foreign keys. It states that a primary key uniquely identifies records in a table and cannot contain null values. A foreign key in one table points to the primary key in another table to link the tables together and ensure referential integrity of data. The document provides examples of creating primary key and foreign key constraints when creating tables in SQL.

Uploaded by

manish_delhi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL PRIMARY KEY Constraint

Previous
Next Chapter

SQL PRIMARY KEY Constraint


The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary key.

SQL PRIMARY KEY Constraint on CREATE TABLE


The following SQL creates a PRIMARY KEY on the "P_Id" column when the "Persons" table is
created:
MySQL:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)

SQL FOREIGN KEY Constraint


A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
Let's illustrate the foreign key with an example. Look at the following two tables:
The "Persons" table:

P_Id
1
2
3

LastName
Hansen
Svendson
Pettersen

FirstName
Ola
Tove
Kari

Address
Timoteivn 10
Borgvn 23
Storgt 20

City
Sandnes
Sandnes
Stavanger

The "Orders" table:


O_Id
1
2
3
4

OrderNo
77895
44678
22456
24562

P_Id
3
3
2
1

Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in the "Persons"
table.
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links between
tables.
The FOREIGN KEY constraint also prevents that invalid data form being inserted into the
foreign key column, because it has to be one of the values contained in the table it points to.

SQL FOREIGN KEY Constraint on CREATE TABLE


The following SQL creates a FOREIGN KEY on the "P_Id" column when the "Orders" table is
created:
MySQL:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)

SQL: VIEWS
A view is, in essence, a virtual table. It does not physically exist. Rather, it is created by a query
joining one or more tables.

Creating a VIEW
The syntax for creating a VIEW is:
CREATE VIEW view_name AS
SELECT columns
FROM table
WHERE predicates;

For example:
CREATE VIEW sup_orders AS
SELECT suppliers.supplier_id, orders.quantity, orders.price
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id
and suppliers.supplier_name = 'IBM';

trigger

(n.) In a DBMS, a trigger is a SQL procedure that initiates an action (i.e., fires an action) when
an event (INSERT, DELETE or UPDATE) occurs. Since triggers are event-driven specialized
procedures, they are stored in and managed by the DBMS. A trigger cannot be called or
executed; the DBMS automatically fires the trigger as a result of a data modification to the
associated table. Triggers are used to maintain the referential integrity of data by changing the
data in a systematic fashion.
Each trigger is attached to a single, specified table in the database.
Triggers can be viewed as similar to stored procedures
Stored procedure

Stored procedure by definition is a segment of code


which contains declarative or procedural SQL statements.
A stored procedure is resided in the catalog of the
database server so we can call it from a trigger, another
stored procedure or even from client appliations.
Advantage

It is used to increases the performance of


application because when we create stored
procedures
The network traffic between application server
and database server is also signification
reduced
Stored procedures can be used for database
security purpose because each store procedure
can have its own database privileges.

One of the most advantage of stored procedure


is code reusability. Once created, a stored
procedure can be reused over and over again
by multiple applications.

You might also like