0% found this document useful (0 votes)
2 views7 pages

SQL Constraints

The document provides an overview of Relational Database Management Systems (RDBMS), detailing key concepts such as primary keys, foreign keys, and various SQL commands categorized into DDL, DML, DQL, and TCL. It also discusses referential integrity, constraints, and practical SQL examples for modifying tables and ensuring data integrity. Additionally, it covers different types of joins and date formatting in SQL queries.

Uploaded by

rkratos22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

SQL Constraints

The document provides an overview of Relational Database Management Systems (RDBMS), detailing key concepts such as primary keys, foreign keys, and various SQL commands categorized into DDL, DML, DQL, and TCL. It also discusses referential integrity, constraints, and practical SQL examples for modifying tables and ensuring data integrity. Additionally, it covers different types of joins and date formatting in SQL queries.

Uploaded by

rkratos22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

RDBMS – Relational Database Management System

PRIMARY KEY An attribute or group of attributes that can


uniquely identify tuples within a relation.
 Must contain unique values
 It arranges the table in an order
 Cant be left NULL
CANDIDATE KEY Capable of becoming the primary key.
ALTERNATE KEY A candidate key that is not selected as
primary key
 Candidate keys-primary key =
alternate key
FOREIGN KEY Non key attribute whose value is derived
from primary key of another table. Primary
key of some other table having relationship
with current table

DDL – Data Definition Language


 CREATE – CREATE DATABASE / CREATE TABLE
 ALTER – ADD(COL/CONSTRAINT), MODIFY, DROP(COL/CONSTRAINT), CHANGE
 DROP – TABLE / DATABASE
 USE
 SHOW

DML – Data Manipulation Language

 INSERT INTO
 UPDATE
 DELETE

DQL – Data Query Language

 SELECT

TCL – Transaction Control Language

 COMMIT
 ROLLBACK

Referential Integrity – system of rules that a DBMS uses to ensure that relationships
between records in related tables are valid, and that users don’t accidently delete or change
related data

CHECK CONSTRAINT – CREATE TABLE STUDENT ( ROLL INTEGER CHECK(ROLL >0)); # column

CREATE TABLE STUDENT( ROLL INTEGER, SNO INTEGER, CHECK (ROLL <> SNO) # table

 IN -> SELECT * FROM STUDENT WHERE ROLL IN (101,102);


 BETWEEN – lower and upper limits are included
 LIKE -> SELECT * FROM STUDENT WHERE DOB LIKE “2018%”;
 Multiple conditions using OR
ALTER QUERY
1. Adding a new column :

ALTER TABLE STUDENT ADD PHONENO INTEGER;

2. Adding a column with default value

ALTER TABLE STUDENT ADD CITY CHAR(6) DEFAULT “DELHI”;

3. Modifying an existing column

ALTER TABLE STUDENT MODIFY CITY VARCHAR(10);

4. Renaming a column

ALTER TABLE STUDENT CHANGE CITY STATE VARCHAR(10);

5. Removing a column

ALTER TABLE STUDENT DROP STATE;

6. Adding Primary Key Constraint

ALTER TABLE STUDENT ADD PRIMARY KEY(ROLLNO);

7. Deleting Primary Key Constraint

ALTER TABLE EMPLOYEE DROP PRIMARY KEY;

8. Delete a constraint

ALTER TABLE Persons DROP CONSTRAINT UC_Person;

9. Adding a constraint

ALTER TABLE table_name ADD CONSTRAINT New_mail UNIQUE (mail);

PRACTICE QUESTIONS

1. BOOKNO, BOOKNAME, QUANTITY, PRICE, AUTHOR

Write the SQL statement to add a new column REVIEW to store the reviews of the book.

ALTER TABLE BOOKS ADD REVIEW VARCHAR (255);

2. Consider the table Persons whose fields are P_ID, LastName, FirstName, Address,

City. Add a new row but add data only in the P_Id, LastName and FirstName as 5, Peterson,
Kari respectively.

INSERT INTO Persons (P_ID, LastName, FirstName) VALUES (5, 'Peterson', 'Kari')

3. If a database "Employee" exists, which MySQL command helps you to start working in that
database?
USE EMPLOYEE;

4. An attribute A of datatype varchar (20) has the value “Amit”. The Attribute B of datatype
char (20) has value ”Karanita”. How many characters are occupied in attribute A? How many
characters are occupied in attribute B?

4 characters occupied in attribute A. 20 characters in attribute B

5. Ms. Shruti has create a table GAMES having attributes ID, GAME, NO_PLAYERS. Later she
wants to add a new attribute DURATION of integer type in the table. Write the query for it.

ALTER TABLE GAMES ADD DUARTION INTEGER;

6. Benjamin a database administrator created a table with few columns. He wants to stop
duplicating the data in the table. Suggest how he can do so.

He can use primary key for a column

7. Write an SQL command to change the data type of a column named price
to number (10,2) in a table named stationary

Alter table stationary modify price numeric (10,2);

8. Write an SQL command to change the values of all the rows of the column price of table
stationary to Null
Update stationary set price=Null;

9. What constraint should be applied on a column of a table so that it becomes compulsory to


insert the value
NOT NULL / PRIMARY KEY

10. Write an SQL command to assign F_id as primary key in the table named flight

ALTER TABLE flight ADD PRIMARY KEY(F_id);

11. Write an SQL command to remove the column remarks from the table name customer.

ALTER TABLE CUSTOMER DROP REMARKS;

12. What constraint/s should be applied to the column in a table to make it as alternate key?
UNIQUE, NOT NULL

13. What constraint should be applied to a table column to ensure that all values in that
column must be unique and not NULL?
Use the UNIQUE constraint along with the NOT NULL / PRIMARY KEY constraint

14. What constraint should be applied to a table column to ensure that it can have
multiple NULL values but cannot have any duplicate non-NULL values?
Use the UNIQUE constraint alone
15. Write an SQL command to drop the unique constraint named unique_email from a
column named email in a table called Users.
ALTER TABLE Users DROP CONSTRAINT unique_email;

16. Write an SQL command to add a unique constraint to the email column of an existing
table named Users, ensuring that all email addresses are unique.
ALTER TABLE Users ADD CONSTRAINT unique_email UNIQUE (email);

17. What constraint should be applied on a table column so that NULL is not allowed in
that column, but duplicate values are allowed.
NOT NULL

18. What constraint should be applied on a table column so that duplicate values are not
allowed in that column, but NULL is allowed.
UNIQUE

19. Write an SQL command to remove the Primary Key constraint from a table, named
MOBILE. M_ID is the primary key of the table.
ALTER TABLE MOBILE DROP PRIMARY KEY;

20. Write an SQL command to make the column M_ID the Primary Key of an already
existing table, named MOBILE
ALTER TABLE MOBILE ADD PRIMARY KEY (M_ID);

JOINS
 CARTESIAN PRODUCT
o SELECT * FROM T1, T2
o (degree – no. of columns in T1 + no. of columns in T2)
o (cardinality – no. of rows in T1 * no. of rows in T2)
 EQUI JOIN
o SELECT * FROM T1, T2 WHERE T1.NO = T2.NO;
 NATURAL JOIN
o SELECT * FROM T1 NATURAL JOIN T2
o (Degree – no. of column in T1 + no. Of column in T2 – 1)
o (Cardinality – no. of common tuples in T1 and T2)

There is a difference between UNIQUE and PRIMARY KEY constraint. - Unique allows NULL
values whereas PRIMARY KEY does not allow.

 Count (<column name>) does not take NULL values if column is specified.
(Count(rollno))
 Count (*) takes the NULL values also
 Order of flow – where, group by, having, order by

DATE FORMAT

 Select * from t1 where year (DOB) = 2019;


 Select * from t1 where DOB like “2019%”;
 Select * from t1 where DOB BETWEEN 2019-01-01 AND 2020-01-01

You might also like