Uploaded by Abbiha Mustafa

Lab 4 - DDL - Constraints with Create

advertisement
Data Definition Language (DDL)
DDL statements create, modify, and remove database objects such as tables, indexes, and users.
Common DDL statements are CREATE, ALTER, and DROP.
DDL commands are as follows,
1. CREATE
2. DROP
3. ALTER
5. TRUNCATE


These commands can be used to add, remove or modify tables within a database.
DDL has pre-defined syntax for describing the data.
1. CREATE COMMAND


CREATE command is used for creating objects in the database.
It creates a new table or Database.
Syntax:
CREATE TABLE <table_name>
( column_name1 datatype,
column_name2 datatype,
program varchar(10) default ‘BSCS’
column_name_n datatype
);
I.
Identity is used in Microsoft SQL Server to automatically insert numerical primary key
values to a table as new data is inserted.
CREATE TABLE TABLE_NAME
(PRIMARY_KEY_COLUMN INT PRIMARY KEY IDENTITY ( [Initial_Value], [Interval] ),
...)
II.
Constraint (a limitation or restriction.) A constraint is a property assigned to a column or the
set of columns in a table that prevents certain types of inconsistent data values from being
placed in the column(s). Constraints are used to enforce data integrity. This ensures the
accuracy and reliability of the data in the database.
Six types of constraints can be placed when creating a table:
 NOT NULL Constraint: Ensures that a column cannot have NULL value.
 DEFAULT Constraint: Provides a default value for a column when none is specified.
 UNIQUE Constraint: Ensures that all values in a column are different.
 CHECK Constraint: Makes sure that all values in a column satisfy certain criteria.
 Primary Key Constraint: Used to uniquely identify a row in the table.
 Foreign Key Constraint: Used to ensure referential integrity of the data.
CREATE TABLE tableName(
id number(12) primary key,
-- Primary Key
CH_a number(12) FOREIGN KEY REFERENCES abcTable2(P_a),
-- FOREIGN KEY
First_Name char(50) ,
Address char(50) UNIQUE,
-- UNIQUE
City char(50) NOT NULL,
-- Not Null
Country char(25) default 'Pakistan',
-- Default
age number check (age>2000)
-- Check\
);
2. DROP COMMAND



DROP command allows removing entire database objects from the database.
It removes entire data structure from the database.
It deletes a table, index or view.
Syntax:
DROP TABLE <table_name>;
OR
DROP DATABASE <database_name>;

If you want to remove individual records, then use DELETE command of the DML
statement.
3. ALTER COMMAND



An ALTER command allows to alter or modify the structure of the database.
It modifies an existing database object.
Using this command, you can add additional column, drop existing column and even change
the data type of columns.
Syntax:
--To add a column in a table
ALTER TABLE table_name
ADD column_name datatype;
--To delete a column in a table
ALTER TABLE table_name
DROP COLUMN column_name;
--To change the data type of a column in a table
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
5. TRUNCATE COMMAND



TRUNCATE command is used to delete all the rows from the table permanently.
It removes all the records from a table, including all spaces allocated for the records.
This command is the same as the DELETE command, but the TRUNCATE command does
not generate any rollback data.
Syntax:
TRUNCATE TABLE <table_name>;
Example:
TRUNCATE TABLE employee;
Download