SlideShare a Scribd company logo
UNIT 1
DATABASE DESIGN AND SQL
By Ms. Sonali Sonavane
Introduction
 SQL is a standard language for accessing and
manipulating databases.
 What is SQL?
 SQL stands for Structured Query Language
 SQL lets you access and manipulate databases
 SQL became a standard of the American National
Standards Institute (ANSI) in 1986, and of the International
Organization for Standardization (ISO) in 1987
What Can SQL do?
 SQL can execute queries against a database
 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views
SQL Command Types
DDL
• Data Definition Language
DML • Data Modification Language
DCL
• Data Control Language
TCL
• Transaction Control Language
DQL
• Data Query Language
SQL Data types
SQL Numeric Data Types
SQL Date , Time & character Data Types
SQL Unicode Character and String Data Types
SQL Binary Data Types
SQL other data types
Operators in SQL
Operators
Arithmetic Comparison Logical
Arithmetic Operators
Comparison Operators
Logical Operators
DDL: Data Definition Language
 DDL or Data Definition Language is used to define the
database schema or tables. It simply deals with
descriptions of the database schema and is used to
create and modify the structure of database.
 Commands under DDL are:
 Create
 Drop
 Alter
 Truncate
 Comment
 Rename
DDL: Data Definition Language
 CREATE – to create table or view or index (objects)
in the database
 ALTER – alters the structure of the database
 DROP – delete table/index/view from the database
 TRUNCATE – remove all records from a table,
including all spaces allocated for the records are
removed
 COMMENT – add comments to the data dictionary
 RENAME – rename a table
DML: Data Manipulation Language
The SQL commands that deals with the manipulation of
data present in the database belong to DML or Data
Manipulation Language and this includes most of the
SQL statements.
Examples of DML:
 INSERT – is used to insert data into a table.
 UPDATE – is used to update existing data within a
table.
 DELETE – is used to delete records from a database
table.
DCL: Data Control Language
 DCL includes commands such as GRANT and
REVOKE which mainly deals with the rights,
permissions and other controls of the database
system.
Examples of DCL commands:
 GRANT-gives user’s access privileges to database.
 REVOKE-withdraw user’s access privileges given by
using the GRANT command.
TCL: Transaction Control Language
 : TCL commands deals with the
transaction within the database.
Examples of TCL commands:
 COMMIT– commits a Transaction.
 ROLLBACK– rollbacks a transaction in case of any
error occurs.
 SAVEPOINT–sets a save point within a transaction.
 SET TRANSACTION–specify characteristics for the
transaction.
DQL :Data Query Language
 DML statements are used for performing queries on
the data within schema objects. The purpose of DQL
Command is to get some schema relation based on
the query passed to it.
Example of DQL:
 SELECT – is used to retrieve data from the a
database.
DDL Commands for Database
A database is used to store the collection of records in an organized
form. It allows us to hold the data into tables, rows, columns, and
indexes to find the relevant information frequently. We can access and
manage the records through the database very easily.
Create Database:
Syntax:
Create database database_name;
Example :
Create database student_info ;
Select Database
SELECT Database is used in MySQL to select a
particular database to work with
For selecting database:
Syntax:
Use database_name;
Example :
Use student_info;
Show database
 We can list all the databases available on the
MySQL server using the following command.
 Syntax:
Mysql>show databases;
Drop Database
 We can drop/delete/remove a MySQL database quickly with
the MySQL DROP DATABASE command.
 It will delete the database along with all the tables, indexes,
and constraints permanently.
 Therefore, we should have to be very careful while removing
the database in MySQL because we will lose all the data
available in the database.
 If the database is not available in the MySQL server, the
DROP DATABASE statement throws an error.
 Syntax:
 DROP DATABASE database_name;
 Drop Database student_info;
DDL Commands for table
 Create table
 Alter table
 Describe table
 Drop table
 Truncate table
 Rename table
 Show table
 Copy table
Create table
• Syntax
Create table table_name (Col_name1
data_type(Size) , Col_name2
data_type(Size),…))
• Example
Create table Stud(rno number(4), name
varchar(20), DOB date));
Describe table
 Describe table
• Syntax
desc table_name;
• Example
desc Stud;
Show tables
 Show tables
 This will list the tables in the current database:
• Syntax
show tables;
• Example
mysql> show tables;
stud
Alter table
 Alter table (add or modify)
Syntax
alter table table_name add/modify/drop column name
data_type(size) [first/after column name]
Example
• Alter table stud add age int(3);
• Alter table stud modify name varchar(30);
• Alter table stud modify name char(20);
• Alter table stud drop column age;
•
Truncate Table
 Truncate Table
used to delete complete data/all rows from an
existing table.
• Syntax
TRUNCATE TABLE table_name;
• Example
mysql> TRUNCATE TABLE stud;
Rename Table
 Rename Table
used to rename table.
• Syntax
RENAME TABLE tbl_name TO new_tbl_name
• Example
RENAME TABLE stud TO student ;
Drop table
 Drop table
uses a Drop Table statement to delete the existing table.
This statement removes the complete data of a table along
with the whole structure or definition permanently from the
database
•Syntax
Drop table table_name;
•Example
Drop table stud;
Copy table
Syntax:
CREATE TABLE new_table_name
SELECT column1, column2, column3
FROM existing_table_name;
Example:
create table s1 select rno, name from stud;
Cont…
• Syntax
ALTER TABLE <table name> ADD CONSTRAINT
<constraint name> PRIMARY KEY(<attribute list>);
Example
ALTER TABLE STUDENT ADD CONSTRAINT NOT
NULL PRIMARY KEY (StudID);
Constraints
 Constraints are the set of rules defined on tables to
ensure data integrity.
• Primary key
• Not null
• Default
• Unique
• Check
• Foreign key/reference key
Primary Key constraints
 Each table must normally contain a column or set of columns that uniquely
identifies rows of data that are stored in the table. This column or set of
columns is referred to as the primary key.
• A table can have only one primary key.
• It is unique and cant have NULL values.
Syntax
CREATE TABLE table_name ( Col_name Data_type(size)
CONSTRAINT constraint_name PRIMARY KEY, ... );
Example
 Create table stud (rollno number(4),name varchar(20),constraint
pk1 primary key(rno))
 Create table stud (rollno number(4) primary key, name ….)
Constraints-Not Null
 • A NOT NULL constraint means that a data row
must have a value for the column specified as NOT
NULL.
• Syntax
CREATE TABLE table_name (Col_name
Data_type(size)not null, ... );
• Example
Create table stud (rollno number(4) ,name
varchar2(20)not null);
Constraints- Default
 In a MySQL table, while inserting data into a table, if
no value is supplied to a column, then the column gets
the value set as DEFAULT.
Syntax
CREATE TABLE table_name (col_name data_type (size)
DEFAULT ‘default_value’ );
Example
CREATE TABLE Stud (rno number(4) ,name varchar2(20),
addr varchar(30) DEFAULT’ ‘Chandwad );
Constraints- Unique
 The UNIQUE constraint in MySQL does not allow to
insert a duplicate value in a column.
Syntax
CREATE TABLE table_name (col_name data_type
(size) Unique);
Example
CREATE TABLE Stud (Rno number(4) Unique);
Constraints- Check
 In a MySQL table, A CHECK constraint controls the values
in the associated column. The CHECK constraint determines
whether the value is valid or not.
Syntax
CREATE TABLE table_name (col_name data_type(size)
Check (condition) );
Example
 CREATE TABLE Stud (rno number(4) CHECK (rollno
BETWEEN 1 AND 60));
 CREATE TABLE Stud (age number(4) CHECK (age>18));
Constraints- FOREIGN KEY
 A FOREIGN KEY in MySQL creates a link between two
tables by one specific column of both tables. The
specified column in one table must be a PRIMARY KEY
and referred by the column of another table known as
FOREIGN KEY.
Syntax
Create table table_name(col_name data_type
(size)references table_name(col_name));
Example
Create table stud1 (rollno number(4) references
stud(rno));
Constraints- after table creation and drop constraint
 You can also add constraint after table creation
using alter table option
Example
 Alter table stud add constraint prk1 primary key(rollno);
 ALTER TABLE emp ADD PRIMARY KEY (EID)
 You can also drop constraint using Drop command &
name of constraint
Example
 alter table emp Drop constraint pk1;
 ALTER TABLE emp DROP PRIMARY KEY;
AUTO_INCREMENT Constraint
 This constraint automatically generates a unique number
whenever we insert a new record into the table.
Syntax:
CREATE TABLE Animals( id int NOT NULL AUTO_INCREMEN
T, name CHAR(30) NOT NULL, PRIMARY KEY (id));
Example:
INSERT INTO Animals (name) VALUES
('Tiger'),('Dog'),('Penguin'),
('Camel'),('Cat'),('Ostrich');
DDL Commands for Index
 An index is a data structure that allows us to add indexes in the
existing table.
 It enables you to improve the faster retrieval of records on a
database table.
 When a table is created with a primary key or unique key, it
automatically creates a special index named PRIMARY.
We called this index as a clustered index. All indexes other
than PRIMARY indexes are known as a non-clustered index or
secondary index.
 Create Index
 Show Index
 Drop Index
Create Index
Syntax
1. Create Index index_name on
table_name (column_name)
2. Alter table table_name add index
index_name (column_name)
Example
1. Create Index n1 on Stud(Name)
2. Alter table Stud add Index n1 (name)
Show Index
Syntax
Show Index from table_name
Example
Show Index from Stud;
Drop Index
Syntax
 DROP INDEX index_name ON table_na
me
 Alter table table_name drop Index
index_name
Example
 Drop index city1 on stud;
 Alter table Stud drop Index n1;
DDL Commands for View
 In MySQL, the View is a virtual table created by a
query by joining one or more tables.
 It is operated similarly to the base table but does
not contain any data of its own.
 The View and table have one main difference that
the views are definitions built on top of other tables
(or views).
 If any changes occur in the underlying table, the
same changes reflected in the View also.
Create View
Syntax
Create View view_name as select
col_name1,col_name2 from table_name
[where ]
Example
1.Create view v1 as select name from
stud; 2.Create view v2 as select name
from stud where addr=‘Nashik’;
Show View
Syntax
Select col_name1,.. from View_name
[where condition]
Example
Select * from v1;
Alter View
 The ALTER VIEW statement is used to modify or
update the already created VIEW without
dropping it.
Syntax:
ALTER VIEW view_name AS SELECT columns
FROM table WHERE conditions;
Drop View
Syntax
Drop View view_name
Example
Drop View v1;
Assignment
 Create one table employee with fields
Eno –primary key and apply sequence starts with 101
Ename –not null
Address –default ‘Nashik’
Joindate
Post
Salary –check > 5000
 Create another table emp_proj with fields
Eno- forign key
Project_name
Loc
 Create Index on Ename field of employee table
 Create View on employee table to show only Employee name, it’s post and
salary.

More Related Content

PPTX
RELATIONSHIP IN DBMS.pptx
PPTX
Null / Not Null value
PPT
Introduction to-sql
PPT
Retrieving data using the sql select statement
PPTX
DATABASE CONSTRAINTS
PDF
Sql exposición
PPTX
Aggregate functions in SQL.pptx
PPT
Advanced Sql Training
RELATIONSHIP IN DBMS.pptx
Null / Not Null value
Introduction to-sql
Retrieving data using the sql select statement
DATABASE CONSTRAINTS
Sql exposición
Aggregate functions in SQL.pptx
Advanced Sql Training

What's hot (20)

DOC
Sql task answers
PPT
Advanced sql
PDF
101 dicas excel
PPTX
PL/SQL - CURSORS
PPTX
Microsoft Access
PDF
Almost everything-you-wanted-to-know-about-pto
ODP
Oracle SQL Advanced
PPT
Introduction to sql
PPT
Les01 (retrieving data using the sql select statement)
PDF
Sql commands
PDF
Alter table command
PPTX
PPTX
Procedimientos almacenados en Postgres
PDF
Document of Record
PPT
SQL subquery
PDF
PPT
SQL DDL
DOCX
Joins in dbms and types
PPT
Sql joins
PPTX
Sql and Sql commands
Sql task answers
Advanced sql
101 dicas excel
PL/SQL - CURSORS
Microsoft Access
Almost everything-you-wanted-to-know-about-pto
Oracle SQL Advanced
Introduction to sql
Les01 (retrieving data using the sql select statement)
Sql commands
Alter table command
Procedimientos almacenados en Postgres
Document of Record
SQL subquery
SQL DDL
Joins in dbms and types
Sql joins
Sql and Sql commands
Ad

Similar to SQL: Data Definition Language(DDL) command (20)

PPTX
Introduction to database and sql fir beginers
DOCX
COMPUTERS SQL
PPTX
DBMS: Week 05 - Introduction to SQL Query
PPTX
Lab2 ddl commands
PPTX
SQL - DML and DDL Commands
DOCX
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
PPTX
MySQL Essential Training
PPT
Creating a database
PDF
Chapter 4 Structured Query Language
DOC
Module 3
PPTX
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
PDF
DBMS.pdf
PDF
Sql smart reference_by_prasad
PDF
Sql smart reference_by_prasad
PPT
Sql intro & ddl 1
PPT
Sql intro & ddl 1
PDF
Introduction to SQL..pdf
PPT
Sql basics and DDL statements
PPTX
SQl data base management and design
Introduction to database and sql fir beginers
COMPUTERS SQL
DBMS: Week 05 - Introduction to SQL Query
Lab2 ddl commands
SQL - DML and DDL Commands
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
MySQL Essential Training
Creating a database
Chapter 4 Structured Query Language
Module 3
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
DBMS.pdf
Sql smart reference_by_prasad
Sql smart reference_by_prasad
Sql intro & ddl 1
Sql intro & ddl 1
Introduction to SQL..pdf
Sql basics and DDL statements
SQl data base management and design
Ad

More from sonali sonavane (11)

PPTX
Introduction To Pandas:Basics with syntax and examples.pptx
PPTX
Understanding_Copyright_Presentation.pptx
PPTX
Python chart plotting using Matplotlib.pptx
PPTX
SQL Data Manipulation language and DQL commands
PPTX
Random Normal distribution using python programming
PPTX
program to create bell curve of a random normal distribution
PPTX
Data Preprocessing: One Hot Encoding Method
PPTX
Data Preprocessing Introduction for Machine Learning
PPTX
Data Preprocessing:Feature scaling methods
PPTX
Data Preprocessing:Perform categorization of data
PPTX
NBA Subject Presentation08 march 24_A Y 2023-24.pptx
Introduction To Pandas:Basics with syntax and examples.pptx
Understanding_Copyright_Presentation.pptx
Python chart plotting using Matplotlib.pptx
SQL Data Manipulation language and DQL commands
Random Normal distribution using python programming
program to create bell curve of a random normal distribution
Data Preprocessing: One Hot Encoding Method
Data Preprocessing Introduction for Machine Learning
Data Preprocessing:Feature scaling methods
Data Preprocessing:Perform categorization of data
NBA Subject Presentation08 march 24_A Y 2023-24.pptx

Recently uploaded (20)

PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
ETO & MEO Certificate of Competency Questions and Answers
PDF
Well-logging-methods_new................
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPT
Drone Technology Electronics components_1
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Digital Logic Computer Design lecture notes
PDF
composite construction of structures.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
bas. eng. economics group 4 presentation 1.pptx
ETO & MEO Certificate of Competency Questions and Answers
Well-logging-methods_new................
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Drone Technology Electronics components_1
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Construction Project Organization Group 2.pptx
Lesson 3_Tessellation.pptx finite Mathematics
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Sustainable Sites - Green Building Construction
UNIT-1 - COAL BASED THERMAL POWER PLANTS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Digital Logic Computer Design lecture notes
composite construction of structures.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx

SQL: Data Definition Language(DDL) command

  • 1. UNIT 1 DATABASE DESIGN AND SQL By Ms. Sonali Sonavane
  • 2. Introduction  SQL is a standard language for accessing and manipulating databases.  What is SQL?  SQL stands for Structured Query Language  SQL lets you access and manipulate databases  SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987
  • 3. What Can SQL do?  SQL can execute queries against a database  SQL can retrieve data from a database  SQL can insert records in a database  SQL can update records in a database  SQL can delete records from a database  SQL can create new databases  SQL can create new tables in a database  SQL can create stored procedures in a database  SQL can create views in a database  SQL can set permissions on tables, procedures, and views
  • 4. SQL Command Types DDL • Data Definition Language DML • Data Modification Language DCL • Data Control Language TCL • Transaction Control Language DQL • Data Query Language
  • 7. SQL Date , Time & character Data Types
  • 8. SQL Unicode Character and String Data Types SQL Binary Data Types
  • 14. DDL: Data Definition Language  DDL or Data Definition Language is used to define the database schema or tables. It simply deals with descriptions of the database schema and is used to create and modify the structure of database.  Commands under DDL are:  Create  Drop  Alter  Truncate  Comment  Rename
  • 15. DDL: Data Definition Language  CREATE – to create table or view or index (objects) in the database  ALTER – alters the structure of the database  DROP – delete table/index/view from the database  TRUNCATE – remove all records from a table, including all spaces allocated for the records are removed  COMMENT – add comments to the data dictionary  RENAME – rename a table
  • 16. DML: Data Manipulation Language The SQL commands that deals with the manipulation of data present in the database belong to DML or Data Manipulation Language and this includes most of the SQL statements. Examples of DML:  INSERT – is used to insert data into a table.  UPDATE – is used to update existing data within a table.  DELETE – is used to delete records from a database table.
  • 17. DCL: Data Control Language  DCL includes commands such as GRANT and REVOKE which mainly deals with the rights, permissions and other controls of the database system. Examples of DCL commands:  GRANT-gives user’s access privileges to database.  REVOKE-withdraw user’s access privileges given by using the GRANT command.
  • 18. TCL: Transaction Control Language  : TCL commands deals with the transaction within the database. Examples of TCL commands:  COMMIT– commits a Transaction.  ROLLBACK– rollbacks a transaction in case of any error occurs.  SAVEPOINT–sets a save point within a transaction.  SET TRANSACTION–specify characteristics for the transaction.
  • 19. DQL :Data Query Language  DML statements are used for performing queries on the data within schema objects. The purpose of DQL Command is to get some schema relation based on the query passed to it. Example of DQL:  SELECT – is used to retrieve data from the a database.
  • 20. DDL Commands for Database A database is used to store the collection of records in an organized form. It allows us to hold the data into tables, rows, columns, and indexes to find the relevant information frequently. We can access and manage the records through the database very easily. Create Database: Syntax: Create database database_name; Example : Create database student_info ;
  • 21. Select Database SELECT Database is used in MySQL to select a particular database to work with For selecting database: Syntax: Use database_name; Example : Use student_info;
  • 22. Show database  We can list all the databases available on the MySQL server using the following command.  Syntax: Mysql>show databases;
  • 23. Drop Database  We can drop/delete/remove a MySQL database quickly with the MySQL DROP DATABASE command.  It will delete the database along with all the tables, indexes, and constraints permanently.  Therefore, we should have to be very careful while removing the database in MySQL because we will lose all the data available in the database.  If the database is not available in the MySQL server, the DROP DATABASE statement throws an error.  Syntax:  DROP DATABASE database_name;  Drop Database student_info;
  • 24. DDL Commands for table  Create table  Alter table  Describe table  Drop table  Truncate table  Rename table  Show table  Copy table
  • 25. Create table • Syntax Create table table_name (Col_name1 data_type(Size) , Col_name2 data_type(Size),…)) • Example Create table Stud(rno number(4), name varchar(20), DOB date));
  • 26. Describe table  Describe table • Syntax desc table_name; • Example desc Stud;
  • 27. Show tables  Show tables  This will list the tables in the current database: • Syntax show tables; • Example mysql> show tables; stud
  • 28. Alter table  Alter table (add or modify) Syntax alter table table_name add/modify/drop column name data_type(size) [first/after column name] Example • Alter table stud add age int(3); • Alter table stud modify name varchar(30); • Alter table stud modify name char(20); • Alter table stud drop column age; •
  • 29. Truncate Table  Truncate Table used to delete complete data/all rows from an existing table. • Syntax TRUNCATE TABLE table_name; • Example mysql> TRUNCATE TABLE stud;
  • 30. Rename Table  Rename Table used to rename table. • Syntax RENAME TABLE tbl_name TO new_tbl_name • Example RENAME TABLE stud TO student ;
  • 31. Drop table  Drop table uses a Drop Table statement to delete the existing table. This statement removes the complete data of a table along with the whole structure or definition permanently from the database •Syntax Drop table table_name; •Example Drop table stud;
  • 32. Copy table Syntax: CREATE TABLE new_table_name SELECT column1, column2, column3 FROM existing_table_name; Example: create table s1 select rno, name from stud;
  • 33. Cont… • Syntax ALTER TABLE <table name> ADD CONSTRAINT <constraint name> PRIMARY KEY(<attribute list>); Example ALTER TABLE STUDENT ADD CONSTRAINT NOT NULL PRIMARY KEY (StudID);
  • 34. Constraints  Constraints are the set of rules defined on tables to ensure data integrity. • Primary key • Not null • Default • Unique • Check • Foreign key/reference key
  • 35. Primary Key constraints  Each table must normally contain a column or set of columns that uniquely identifies rows of data that are stored in the table. This column or set of columns is referred to as the primary key. • A table can have only one primary key. • It is unique and cant have NULL values. Syntax CREATE TABLE table_name ( Col_name Data_type(size) CONSTRAINT constraint_name PRIMARY KEY, ... ); Example  Create table stud (rollno number(4),name varchar(20),constraint pk1 primary key(rno))  Create table stud (rollno number(4) primary key, name ….)
  • 36. Constraints-Not Null  • A NOT NULL constraint means that a data row must have a value for the column specified as NOT NULL. • Syntax CREATE TABLE table_name (Col_name Data_type(size)not null, ... ); • Example Create table stud (rollno number(4) ,name varchar2(20)not null);
  • 37. Constraints- Default  In a MySQL table, while inserting data into a table, if no value is supplied to a column, then the column gets the value set as DEFAULT. Syntax CREATE TABLE table_name (col_name data_type (size) DEFAULT ‘default_value’ ); Example CREATE TABLE Stud (rno number(4) ,name varchar2(20), addr varchar(30) DEFAULT’ ‘Chandwad );
  • 38. Constraints- Unique  The UNIQUE constraint in MySQL does not allow to insert a duplicate value in a column. Syntax CREATE TABLE table_name (col_name data_type (size) Unique); Example CREATE TABLE Stud (Rno number(4) Unique);
  • 39. Constraints- Check  In a MySQL table, A CHECK constraint controls the values in the associated column. The CHECK constraint determines whether the value is valid or not. Syntax CREATE TABLE table_name (col_name data_type(size) Check (condition) ); Example  CREATE TABLE Stud (rno number(4) CHECK (rollno BETWEEN 1 AND 60));  CREATE TABLE Stud (age number(4) CHECK (age>18));
  • 40. Constraints- FOREIGN KEY  A FOREIGN KEY in MySQL creates a link between two tables by one specific column of both tables. The specified column in one table must be a PRIMARY KEY and referred by the column of another table known as FOREIGN KEY. Syntax Create table table_name(col_name data_type (size)references table_name(col_name)); Example Create table stud1 (rollno number(4) references stud(rno));
  • 41. Constraints- after table creation and drop constraint  You can also add constraint after table creation using alter table option Example  Alter table stud add constraint prk1 primary key(rollno);  ALTER TABLE emp ADD PRIMARY KEY (EID)  You can also drop constraint using Drop command & name of constraint Example  alter table emp Drop constraint pk1;  ALTER TABLE emp DROP PRIMARY KEY;
  • 42. AUTO_INCREMENT Constraint  This constraint automatically generates a unique number whenever we insert a new record into the table. Syntax: CREATE TABLE Animals( id int NOT NULL AUTO_INCREMEN T, name CHAR(30) NOT NULL, PRIMARY KEY (id)); Example: INSERT INTO Animals (name) VALUES ('Tiger'),('Dog'),('Penguin'), ('Camel'),('Cat'),('Ostrich');
  • 43. DDL Commands for Index  An index is a data structure that allows us to add indexes in the existing table.  It enables you to improve the faster retrieval of records on a database table.  When a table is created with a primary key or unique key, it automatically creates a special index named PRIMARY. We called this index as a clustered index. All indexes other than PRIMARY indexes are known as a non-clustered index or secondary index.  Create Index  Show Index  Drop Index
  • 44. Create Index Syntax 1. Create Index index_name on table_name (column_name) 2. Alter table table_name add index index_name (column_name) Example 1. Create Index n1 on Stud(Name) 2. Alter table Stud add Index n1 (name)
  • 45. Show Index Syntax Show Index from table_name Example Show Index from Stud;
  • 46. Drop Index Syntax  DROP INDEX index_name ON table_na me  Alter table table_name drop Index index_name Example  Drop index city1 on stud;  Alter table Stud drop Index n1;
  • 47. DDL Commands for View  In MySQL, the View is a virtual table created by a query by joining one or more tables.  It is operated similarly to the base table but does not contain any data of its own.  The View and table have one main difference that the views are definitions built on top of other tables (or views).  If any changes occur in the underlying table, the same changes reflected in the View also.
  • 48. Create View Syntax Create View view_name as select col_name1,col_name2 from table_name [where ] Example 1.Create view v1 as select name from stud; 2.Create view v2 as select name from stud where addr=‘Nashik’;
  • 49. Show View Syntax Select col_name1,.. from View_name [where condition] Example Select * from v1;
  • 50. Alter View  The ALTER VIEW statement is used to modify or update the already created VIEW without dropping it. Syntax: ALTER VIEW view_name AS SELECT columns FROM table WHERE conditions;
  • 51. Drop View Syntax Drop View view_name Example Drop View v1;
  • 52. Assignment  Create one table employee with fields Eno –primary key and apply sequence starts with 101 Ename –not null Address –default ‘Nashik’ Joindate Post Salary –check > 5000  Create another table emp_proj with fields Eno- forign key Project_name Loc  Create Index on Ename field of employee table  Create View on employee table to show only Employee name, it’s post and salary.