0% found this document useful (0 votes)
3 views

Lecture 5 DDL (1)

DATABASE

Uploaded by

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

Lecture 5 DDL (1)

DATABASE

Uploaded by

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

Basic SQL

(Tool: MYSQL Workbench)


SQL
Data Definition Langage
Data Manipulation Language
Main Commands for DDL
Using DDL commands we can make changes in the structure of the database.

Main Commands:
◦ Create – used to create a new table in the database
◦ Alter – used to make changes in the already created tables
◦ Drop – used to delete an existing table
Getting Started …
Before creating a table in the database, we need to create a database itself:

◦ Create database databaseName;

◦ Example:
◦ Create database Company;

◦ SQL is not case sensitive.


◦ Every command needs to end with a semicolon.
Getting Started …
We can delete a database :

Drop database DatabaseName;


Example:

Drop database Company;


Getting Started …
Before creating a new table or viewing already constructed tables we need to make sure we are in scope of the
database or we are present in a database.

Use databaseName ;
Example:
Use Company;

We can see tables present in an existing database:


Example:
Show tables;
How to create a table
Datatypes & Constraints
Datatypes
Numeric values Character String Some more
INT (Size: 4 Bytes) Fixed length: CHAR(5) important datatypes
CAT_ _
NUMERIC(I, j) 888.88 Boolean: TRUE, FALSE, NULL
Non-fixed length:
Fixed point number, with user-specified VARCHAR(n) CAT DATE : yyyy-mm-dd
precision of p digits, with d digits to the right TIME: hh:mm:ss
of decimal point
TIMESTAMP: DATE(DD-MM-
YYYY) & TIME (HH:MM:SS)
Key words for Constraints
NOT NULL – used for required fields
UNIQUE – used for attributes that cannot have a duplicate value (other than
pk)
DEFAULT – used to assign some default value to an attribute
CHECK(Dnumber > 0 AND Dnumber < 21) -- used to set a condition on the
input value for an attribute.
Primary key – used to make an attribute primary key of the table
Foreign Key (A) references s(B) -- used to make an attribute foreign key.
Create Table
CREATE TABLE tablename
(
AttributeName Datatype Constraint,
…....
);
Employee
CREATE TABLE Employee
(
Fname,
Minit,
Lname,
Ssn,
Bdat,
Address,
Gender,
Salary,
Super_ssn ,
Dno ,
CONSTRAINT PK_Employee PRIMARY KEY (attributes),
CONSTRAINT FK_EmpDep FOREIGN KEY (attribute)
REFERENCES table name(attribute) );
Employee
CREATE TABLE Employee
(
Fname VARCHAR(15) NOT NULL,
Minit CHAR(1),
Lname VARCHAR(15) NOT NULL,
Ssn CHAR (9) NOT NULL,
Bdat DATE,
Address VARCHAR(30) DEFAULT 'UCP',
Gender Char(1),
Salary int,
Super_ssn CHAR(9),
Dno INT ,
Primary key (ssn),
CONSTRAINT FK_EmpDep FOREIGN KEY (Dno)
REFERENCES DEPARTMENT(Dnumber) );
Solution of Foreign
key violation
We can specify RESTRICT, CASCADE, SET NULL or
SET DEFAULT on referential integrity constraints
(foreign keys)

◦ On delete / Update Cascade /Restrict

Automatically delete / update data


◦ On delete / update Set NULL / Default

Automatically set Null.

On delete no action is performed by default


Employee
CREATE TABLE Employee
(
Fname VARCHAR(15) NOT NULL,
Minit CHAR(1),
Lname VARCHAR(15) NOT NULL,
Ssn CHAR (9) NOT NULL,
Bdat DATE,
Address VARCHAR(30) DEFAULT 'UCP',
Gender Char(1),
Salary int,
Super_ssn CHAR(9),
Dno INT DEFAULT 1,
Primary key (ssn),
CONSTRAINT FK_EmpDep FOREIGN KEY (Dno)
REFERENCES DEPARTMENT(Dnumber) ON DELETE SET
DEFAULT ON UPDATE CASCADE
);
Department
CREATE TABLE DEPTARTMENT
( Dname VARCHAR(15) NOT NULL ,
Dnumber INT CHECK (Dnumber > 0 AND Dnumber<21),
Mgr_ssn CHAR(9),
Mgr_start_date DATE,
PRIMARY KEY (Dnumber),
UNIQUE (Dname),
Constraint DepMgrFK FOREIGN KEY (Mgr_ssn) REFERENCES
Employee(Ssn) ON DELETE SET NULL ON UPDATE CASCADE
);
Works_on
CREATE TABLE WORKS_ON
(
Essn CHAR(9),
Pno INT,
Hours DECIMAL (3,1),
Primary key(Essn, Pno),
Constraint essnfk Foreign key (ESSN) References EMPLOYEE (ssn)
ON DELETE SET NULL ON UPDATE CASCADE
);
Class Activity
Create table of
DEPT_LOCATION
PROJECT
DEPENDENT
DROP TABLE
Used to remove a relation (table) and its definition
The relation can no longer be used in queries, updates, or any other commands since its
description no longer exists
Example:

DROP TABLE DEPENDENT;


ALTER TABLE
Used to add an attribute to one of the tables
Used to drop an existing attribute of the table
Used to change the datatype of an existing attribute of the table
Used to change the name of an existing attribute of the table
Used to add/drop constraints
Add/Drop attribute
The new attribute will have NULLs in all the tuples of the relation right after the command is
executed; hence, the NOT NULL constraint is not allowed for such an attribute
Example:

ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12);

ALTER TABLE EMPLOYEE DROP JOB ;


Setting position for a new
attribute
By default attribute is added at the end of the table
student
Sid Name Address
ALTER TABLE student ADD Email varchar(50) FIRST;
ALTER TABLE student DROP Email;
ALTER TABLE student ADD Email varchar(50) AFTER Sid;
Changing a Column Datatype or
Name
student
ALTER TABLE student Modify Email varchar(100); Sid Name Address

ALTER TABLE student Change Email Email_Address varchar(100);


Change command can change both datatype and name.

ALTER TABLE student Change Email Email_Address varchar(50);


Adding Not Null and Default Value of
Attributes
student
Sid Name Address

ALTER TABLE student MODIFY Address Text NOT NULL DEFAULT ‘LAHORE’;

TO change Default value:


ALTER TABLE student ALTER Address SET DEFAULT ’KARACHI’;

You can remove default constraint from any column:


ALTER TABLE student ALTER Address drop default;
Rename a table

ALTER TABLE Student RENAME TO UnderGrad_student;


Drop/ add primary key
Alter table employee
Drop primary key;

Alter table employee


Add primary key (ssn);
Drop/add foreign key
Alter table employee
Add constraint fk_1 foreign key (dno) references department (dnumber);

Alter table employee


Drop foreign key fk_1;

You might also like