Sie sind auf Seite 1von 4

TABLES AND INTEGRITY CONSTRAINTS

TYPE A: Very Short Answer Questions


1 What is a constraint? Name some constraints that you can apply to enhance database integrity.
Ans. A constraint is a property assigned to column or the set of columns in a table that prevents certain types of
inconsistent data values from being placed in the column(s).
1. Primary key constraint
2. Not null constraint
3. Default constraint
2 What is primary key? What is PRIMARY KEY constraint?
Ans. A primary key is a unique identifier for a database record. When a table is created, one of the fields is typically
assigned as the primary key. While the primary key is often a number, it may also be a text field or other data type.
The PRIMARY KEY constraint uniquely identifies each record in a database table.
3 What is NOT NULL constraint? What is DEFAULT constraint?
Ans. The not-null constraint is a restriction placed on a column in a relational database table. It enforces the condition
that, in that column, every row of data must contain a value - it cannot be left blank during insert or update
operations. If this column is left blank, this will produce an error message and the entire insert or update operation
will fail.
The DEFAULT constraint is used to insert a default value into a column.
4 Can you have multiple primary keys in your table?
Ans. No
5 Can you have multiple foreign key constraints in a table?
Ans. Yes
6 In MySQL, what table types (storage engine) is required for foreign keys to work?
Ans. reference table
7 How would you find the storage engine of your table?
Ans. SHOW CREATE TABLE tablename; will display the storage engine of table.
8 With which command, can you change storage engine of your table?
Ans. ALTER TALE tablename ENGINE = InnoDB; command change the storage engine of table.

TYPE B: Short Answer Questions


1 What is foreign key? How do you define a foreign key in your table?
Ans. A foreign key is a field that points to the primary key of another table. The purpose of foreign key is to ensure
referential integrity of the data.

Whenever two tables are related by a common column (or set of columns), then the related column(s) in the parent
table (or primary table) should be either declared a PRIMARY KEY or UNIQUE key and the related column(s) in the
child table (or related table) should have FOREIGN KEY constraint.
For example, if there are two tables:
Items (Itemno, Description, Price, QOH)
Orders (Orderno, Orderdate, itemno, Qty)
Both the tables are related through common column Itemno.
The column Itemno is primary key in parent table Items and it should be declared as foreign key in the child table
Orders to enforce referential integrity.

2 How are FOREIGN KEY commands related to the PRIMARY KEY?


Ans.
3 Modify table Empl, add another column called Grade of VARCHAR type, size 1 into it.
Ans. ALTER TABLE Empl ADD (Grade VARCHAR(1));
4 Add a constraint (NN_Grade) in table Empl that declares column Grade NOT NULL.
Ans. This very unusual question, as NOT NULL can be added by simply without constraint name.
ALTER TABLE Empl ADD CONSTRAINT NN_Grade CHECK (Grade IS NOT NULL);
5 Modify the definition of column Grade. Increase its size to 2.
Ans. ALTER TABLE Empl MODIFY Grade VARCHAR(2);
6 Drop the table Empl.
Ans. DROP TABLE Empl;
7 Create table Customer as per following Table Instance Chart. ( - primary key)
Column Name Cust_ID Cust_Name Cust_Address1 Cust_Address2 PinCode Cust_Phone
Datatype NUMBER VARCHAR VARCHAR VARCHAR NUMBER VARCHAR
Length 7 30 20 30 6 10
Ans. CREATE
8 Add one column Email of data type VARCHAR and size 30 to the table Customer.
Ans. ALTER TABLE Customer ADD (Email VARCHAR(30));
9 Add one more column CustomerIncomeGroup of datatype VARCHAR (10).
Ans. ALTER TABLE Customer ADD (CustomerIncomeGroup VARCHAR(10));
10 Drop the column CustomerIncomeGroup from table Customer.
Ans. ALTER TABLE Customer DROP CustomerIncomeGroup;
11 Create table Department as per following Table Instance Chart.
Column Name DeptID DeptName
Key Type Primary
Nulls/Unique NOT NULL
Datatype NUMBER VARCHAR
Length 2 20
Ans. CREATE TABLE `Department` (
`DeptID` NUMERIC(2),
`DeptName` VARCHAR(20) NOT NULL,
PRIMARY KEY (`DeptID`)
)
12 Create table Employee as per following Table Instance Chart.
Column EmpID EmpName EmpAddress EmpPhone EmpSal DeptID
Name
Key Type Primary Foreign
Null/Unique NOT NULL
FK Table Department
FK Column Dept_ID
Datatype NUMBER VARCHAR VARCHAR VARCHAR NUMBER VARCHAR
Length 6 20 30 10 9,2 2
Ans. CREATE TABLE `Employee`(
`EmpID` NUMERIC(10),
`EmpAdd` VARCHAR(30) NOT NULL,
`EmpPh` VARCHAR(10),
`EmpSal` NUMERIC(9,2),
`DeptID` NUMERIC(2),
PRIMARY KEY (`EmpID`),
CONSTRAINT fk_Dept FOREIGN KEY (`DeptID`) REFERENCES `department`(`DeptID`));
13 View the constraints of all tables created by you.
Ans. DESCRIBE Employee;
14 What do following actions do with Foreign key constraint:
a) ON DELETE CASCADE
b) ON UPDATE RESTRICT
c) ON DELETE SET NULL
Ans. a) ON DELETE CASCADE referential action for a foreign key that allows you to delete data from child tables
automatically when you delete the data from the parent table.
b) ON UPDATE RESTRICT rejects the update operation for the parent table when you try to update the data
from the child table..
c) ON DELETE SET NULL delete the row from the parent table, and set the foreign key column or columns in
the child table to NULL.
15 Given following CREATE TABLE command, identify constraints and their complete details
CREATE TABLE LEGO_CONSTRUCTION
(
ID INT(11) NOT NULL auto_increment,
reference varchar (15) NOT NULL UNIQUE,
nom varchar (255) NOT NULL,
description text NULL,
date_creation timestamp (10) NOT NULL,
theme int (11) NOT NULL,
CONSTRAINT affilier FOREIGN KEY (theme)
REFERENCE LEGO_THEME (id)
ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (id)
) TYPE = InnoDB;
Ans.
16 Given following CREATE TABLE command, identify constraints and their complete details:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
REFERENCE Persons(P_Id)
);
Ans.
17 Carefully study the following script and tell what exactly is it doing along with detailed discussion of integrity
constraints.
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE emplyee(
id smallint(5) unsigned NOT NULL,
firstname varchar(30),
lastname varchar(30),
birthdate date,
PRIMARY KEY (id),
KEY idx_lastname (lastname)
)ENGINE = InnoDB;
CREATE TABLE borrowed(
ref int(10) unsigned NOT NULL auto_increment,
employeeid smallint(5) unsigned NOT NULL,
book varchar(50),
PRIMARY KEY (ref)
) ENGINE = InnoDB;
ALTER TABLE borrowed
FOREIGN KEY (employeeid) REFERENCES employee(id)
ON UPDATE CASCADE
ON DELETE CASCADE;
Ans.
18 Considering the script of previous question, what would happen if following commands take place?
UPDATE employee SET id = 22 WHERE id = 2;
DELETE FROM employee WHERE id = 1;
Ans.
19 Write a MySql command for creating a table “PAYMENT” whose structure is given below:

Ans. CREATE TABLE PAYMENT (Loan_number INTEGER(4) PRIMARY KEY , Payment_number VARCHAR(3) , Payment_date
DATE , Payment_amount INTEGER (8) NOT NULL) ;

Das könnte Ihnen auch gefallen