Lesson 16: Table and Integrity Constraints Integrity Constraints are the rules that a database must follow at all times. Various Integrity constraints are as follows:- 1. Not Null: It ensures that we cannot leave a column as null i.e. a value has to be supplied for that column. 2. Unique: Ensures that each row for a column must have a unique value. A column(s) can have null value but the values cannot be duplicated. 3. Primary key: - Primary key is used to identify the record uniquely in the table. A combination of a NOT NULL and UNIQUE means that a column cannot have duplicated values and not even a null value. 4. Default: Specifies a default value for a column. If no value is specified while inserting records then default value will be inserted. 5. Check: Ensures that the value in a column meets a specific condition. It is used to limit the range of values that can be inputted in to a column 6. Foreign Key: Ensure the referential integrity of the data in one table to match values in another table DEFAULT Create Table with Constraints: You can create table with constraints using create table. Constraints can be added at the time of table creation or can be added later on. For example: CREATE TABLE Student ( RollNo Integer Primary Key, Regno Integer Unique, NAME Char(20), Not Null, Gender Char(1), Age Integer Check age>=5, Admfee integer Default 5000 ); Viewing Constraints and their Columns: DESC <TABLENAME>; DESC STUDENT//Shows detailed information about the table STUDENT Alter Table The purpose of alter table are as follows:- 72
1. Adding Column to a table: To add a new column to a table, you can use ALTER TABLE command as per syntax given below:- ADD [COLUMN] <Column data Type> ; ADD MOBILE_NO INTEGER; A new column by the name Mobile_No will be added to the table 2. Deleting Column: To delete a column from the table, the DROP command as per syntax given below:- DROP [COLUMN]<Column Name> ; DROP [COLUMN] MOBILE_NO; It will delete the column MOBILE_NO from the table EMPLOYEE 3. Modifying Data Type(s) of a column(s): To modify a column of a table, you can use MODIFY command as per syntax given below:- MODIFY <Column Name> <Column Definition>; MODIFY SALARY INTEGER(8); It will change the width of a salary field from 5 to 8 To modify a column of a table with constraints: MODIFY <Column Name> <Column Definition> constraints MODIFY EMP_NO INTEGER (10) PRIMARY KEY; It will modify the EMP_NO and EMP_NO is now working as primary key 4. Adding Constraints: You can also use ALTER TABLE command to add constraints to your existing table. ADD <Constraint name>(column name) ADD PRIMARY KEY (EMP_ID); It will define the EMP_ID as primary key Removing Constraints: You can also use ALTER TABLE command to delete or remove constraints from your existing table DROP <Constraint Name> DROP PRIMARY KEY; It will delete the primary key constraint of a table 73
Enabling/Disabling Constraints: You can enable or disable constraints, but only primary key constraint cannot be disabled, it can only be deleted. Other constraints like Foreign Key, Not Null etc. can de enabled or disabled. Syntax TO ENABLE: SET FOREIGN_KEY_CHECKS=1; Syntax TO DISABLE: SET FOREIGN_KEY_CHECKS=0; Dropping Tables: To delete or drop tables from database, you can use DROP TABLE COMMAND. With Drop Table command, My SQL also removes the description of the table along with all constraints. DROP TABLE TABLE NAME DROP TABLE EMPLOYEE; It will DROP table employee from the database Very Short Answer Type question (1 Marks) 1. What is the use of ALTER TABLE Command? 2. What are integrity constraints? Write the names of the integrity constraints. 3. What is Primary key Constraint? 4. What is Foreign key constraint? 5. What is difference between Unique and Primary Key? 6. Write SQL command to view the constraints of emp table. 7. What is NULL? 8. What is the significance of NOT NULL constraint? 9. Write a query to add new column aadharno in a table student. 10. Write a query to modify data type (char to int) of the existing column emp_id of emp table. 11. Write a query to delete a column pincode form a table employee. 12. Can constraints be added in an existing table? How? 13. Write a statement to enable and disable the constraints of table. 14. When a Primary key constraint is included in a table, what other constraints does this imply? Short Answer Type questions (2 Marks) 1 Write SQL command to create table Coach the following table structure. Field Type Constraint PCode Integer PRIMARY KEY Name Varchar(20) NOT NULL ACode Integer FOREIGN KEY which refer the Acode in Table Activity City Varchar(20) Default = delhi 2 Write a command to create following table with P_ID as foreign key from person table. The "Orders" table is: O_Id OrderNo P_Id 1 77895 3 2 44678 3 74
3. What are different constraints? Explain any two with example. 4. Ms. Shilpa created two tables with Deptno as Primary key in Table1 and Foreign Key in Table2, while inserting a row in Table2, Ms. Shilpa is not able to enter a value in the column Deptno. What could be the possible reason there for it? 5. Write a MySQL command for creating a table "CLUB" whose structure is given below: Field Name Datatype Size Constraint MEMBER_No Integer 10 Primary key Member_Name Varchar 20 Join_Date Date Member_Type char 1 Not Null Charges Decimal 10,2 75
6. Answer the question based on the table VOTER given below: Column Name Data type Size Constraints Description V_id INT 8 Primary key Voter identification Vname VARCHAR 25 Not null Name of the voter Age INT 3 Check>17 Age should not be less than 17 Address VARCHAR 30 Address of voter Phone VARCHAR 10 Phone number of the voter REVISION: Complete the following crossword puzzle using RDBMS (MySQL) concept and commands:- 4 1 2 7 3 6 8 5 10 9 1. Logical unit of work that must succeed or fail entirely. (Across) 2. Number of attributes in the table. (Down) 3. Special features that specify rules for the data in a table (Down) 4. Statement to undo work done in the current transaction(down) 5. Statement that adds one or more records to any single table in a relational database.(across) 6. Combines records from two tables (Down) 7. Returns the number of rows returned by the query. (Down) 8. Statement to save changes made by a transaction.(across) 9. Statement which is used to pull information from a table.(across) 10. Operator to define the range of values. (Down) 76