You are on page 1of 19

MKICS,BHARUCH SQL DATABASE

SQL CREATE TABLE Statement


Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

Example
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

SQL DROP TABLE Statement


Syntax
DROP TABLE table_name;

Example
DROP TABLE Shippers;

SQL TRUNCATE TABLE


The TRUNCATE TABLE statement is used to delete the data inside a
table, but not the table itself.

Prepared By Heena Patel (Asst. Prof.) Page 1


MKICS,BHARUCH SQL DATABASE

Syntax
TRUNCATE TABLE table_name;

Example
TRUNCATE TABLE Shippers;

SQL ALTER TABLE Statement


The ALTER TABLE statement is used to add, delete, or modify columns
in an existing table.

The ALTER TABLE statement is also used to add and drop various
constraints on an existing table.

ALTER TABLE - ADD Column


To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype;
The following SQL adds an "Email" column to the "Customers" table:

Example
ALTER TABLE Customers
ADD Email varchar(255);

OR

ALTER TABLE Customers


ADD COLUMN Email varchar(255);

Prepared By Heena Patel (Asst. Prof.) Page 2


MKICS,BHARUCH SQL DATABASE

ALTER TABLE - DROP COLUMN


To delete a column in a table, use the following syntax (notice that
some database systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;
The following SQL deletes the "Email" column from the "Customers"
table:

Example
ALTER TABLE Customers
DROP COLUMN Email;

ALTER TABLE - ALTER/MODIFY


COLUMN
To change the data type of a column in a table, use the following
syntax:

ALTER TABLE table_name


ALTER COLUMN column_name datatype;

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;
ALTER TABLE table_name
MODIFY column_name datatype;

SQL ALTER TABLE Example


Look at the "Persons" table:

Prepared By Heena Patel (Asst. Prof.) Page 3


MKICS,BHARUCH SQL DATABASE

Now we want to add a column named "DateOfBirth" in the "Persons"


table.

We use the following SQL statement:

ALTER TABLE Persons


ADD COLUMN DateOfBirth date;

Change Data Type Example


ALTER TABLE Persons
ALTER COLUMN ID VARCHAR(10);

DROP COLUMN Example


ALTER TABLE Persons
DROP COLUMN DateOfBirth;

Prepared By Heena Patel (Asst. Prof.) Page 4


MKICS,BHARUCH SQL DATABASE

SQL ALTER TABLE RENAME TABEL


This is used to rename an existing table.

ALTER TABLE tablename RENAME TO newtablename;

Example:

ALTER TABLE t1 RENAME TO t2;

SQL Constraints
SQL constraints are used to specify rules for the data in a table.

Constraints are used to limit the type of data that can go into a table.
This ensures the accuracy and reliability of the data in the table. If
there is any violation between the constraint and the data action, the
action is aborted.

Constraints can be column level or table level. Column level


constraints apply to a column, and table level constraints apply to the
whole table.

The following constraints are commonly used in SQL:

Prepared By Heena Patel (Asst. Prof.) Page 5


MKICS,BHARUCH SQL DATABASE

NOT NULL - Ensures that a column cannot have a NULL value


UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
Uniquely identifies each row in a table
FOREIGN KEY - Uniquely identifies a row/record in another
table
CHECK - Ensures that all values in a column satisfies a specific
condition
DEFAULT - Sets a default value for a column when no value is
specified

SQL NOT NULL Constraint


By default, a column can hold NULL values.

The NOT NULL constraint enforces a column to NOT accept NULL


values.

This enforces a field to always contain a value, which means that you
cannot insert a new record, or update a record without adding a value
to this field.

SQL NOT NULL on CREATE TABLE


The following SQL ensures that the "ID", "LastName", and "FirstName"
columns will NOT accept NULL values when the "Persons" table is
created:

Example
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,

Prepared By Heena Patel (Asst. Prof.) Page 6


MKICS,BHARUCH SQL DATABASE

FirstName varchar(255) NOT NULL,


Age int
);

SQL NOT NULL on ALTER TABLE


To create a NOT NULL constraint on the "Age" column when the
"Persons" table is already created, use the following SQL:

ALTER TABLE Persons


MODIFY Age int NOT NULL;

SQL UNIQUE Constraint


The UNIQUE constraint ensures that all values in a column are
different.

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee


for uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint.

However, you can have many UNIQUE constraints per table, but only
one PRIMARY KEY constraint per table.

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID)
);

Prepared By Heena Patel (Asst. Prof.) Page 7


MKICS,BHARUCH SQL DATABASE

SQL UNIQUE Constraint on ALTER


TABLE
To create a UNIQUE constraint on the "ID" column when the table is
already created, use the following SQL:

ALTER TABLE Persons


ADD UNIQUE (ID);

To name a UNIQUE constraint, and to define a UNIQUE constraint on


multiple columns, use the following SQL syntax:

ALTER TABLE Persons


ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);

DROP a UNIQUE Constraint


To drop a UNIQUE constraint, use the following SQL:

ALTER TABLE Persons


DROP CONSTRAINT UC_Person;

SQL PRIMARY KEY Constraint


The PRIMARY KEY constraint uniquely identifies each record in a table.

Primary keys must contain UNIQUE values, and cannot contain NULL
values.

A table can have only ONE primary key; and in the table, this primary
key can consist of single or multiple columns (fields).

Prepared By Heena Patel (Asst. Prof.) Page 8


MKICS,BHARUCH SQL DATABASE
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);

CREATE TABLE Persons (


ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);

To allow naming of a PRIMARY KEY constraint, and for defining a


PRIMARY KEY constraint on multiple columns, use the following
SQL syntax:

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
);

SQL PRIMARY KEY on ALTER TABLE


To create a PRIMARY KEY constraint on the "ID" column when the
table is already created, use the following SQL:

ALTER TABLE Persons


ADD PRIMARY KEY (ID);

To allow naming of a PRIMARY KEY constraint, and for defining a


PRIMARY KEY constraint on multiple columns, use the following
SQL syntax:

Prepared By Heena Patel (Asst. Prof.) Page 9


MKICS,BHARUCH SQL DATABASE
ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);

DROP a PRIMARY KEY Constraint


ALTER TABLE Persons
DROP PRIMARY KEY;

SQL FOREIGN KEY Constraint


A FOREIGN KEY is a key used to link two tables together.

A FOREIGN KEY is a field (or collection of fields) in one table that


refers to the PRIMARY KEY in another table.

The table containing the foreign key is called the child table, and the
table containing the candidate key is called the referenced or parent
table.

Look at the following two tables:

Prepared By Heena Patel (Asst. Prof.) Page 10


MKICS,BHARUCH SQL DATABASE

Prepared By Heena Patel (Asst. Prof.) Page 11


MKICS,BHARUCH SQL DATABASE

SQL FOREIGN KEY on CREATE TABLE


The following SQL creates a FOREIGN KEY on the "PersonID" column when the
"Orders" table is created:

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

To allow naming of a FOREIGN KEY constraint, and for defining a


FOREIGN KEY constraint on multiple columns, use the following SQL
syntax:

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
CONSTRAINT PK_1 PRIMARY KEY (OrderID),
CONSTRAINT FK_P1 FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)
);

SQL FOREIGN KEY on ALTER TABLE


ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

To allow naming of a FOREIGN KEY constraint, and for defining a


FOREIGN KEY constraint on multiple columns, use the following SQL
syntax:

Prepared By Heena Patel (Asst. Prof.) Page 12


MKICS,BHARUCH SQL DATABASE
ALTER TABLE Orders
ADD CONSTRAINT FK_P1
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

DROP a FOREIGN KEY Constraint


to drop a FOREIGN KEY constraint, use the following SQL:

ALTER TABLE Orders


DROP CONSTRAINT FK_P1;

SQL CHECK Constraint


The CHECK constraint is used to limit the value range that can be
placed in a column.

If you define a CHECK constraint on a single column it allows only


certain values for this column.

If you define a CHECK constraint on a table it can limit the values in


certain columns based on values in other columns in the row.

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
To allow naming of a CHECK constraint, and for defining a
CHECK constraint on multiple columns, use the following SQL
syntax:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,

Prepared By Heena Patel (Asst. Prof.) Page 13


MKICS,BHARUCH SQL DATABASE
City varchar(255),
CONSTRAINT CHK_P1 CHECK (Age>=18 AND City='Sandnes')
);

SQL CHECK on ALTER TABLE


ALTER TABLE Persons
ADD CHECK (Age>=18);
To allow naming of a CHECK constraint, and for defining a
CHECK constraint on multiple columns, use the following SQL
syntax:
ALTER TABLE Persons
ADD CONSTRAINT CHK_P1 CHECK (Age>=18 AND City='Sandnes');

DROP a CHECK Constraint


ALTER TABLE Persons
DROP CONSTRAINT CHK_P1;

SQL DEFAULT Constraint


The DEFAULT constraint is used to provide a default value for a
column.

The default value will be added to all new records IF no other value is
specified.

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'BHARUCH'
);

Prepared By Heena Patel (Asst. Prof.) Page 14


MKICS,BHARUCH SQL DATABASE
The DEFAULT constraint can also be used to insert system values,
by using functions like GETDATE():

CREATE TABLE Orders (


ID int NOT NULL,
OrderNumber int NOT NULL,
OrderDate date DEFAULT GETDATE()
);

SQL DEFAULT on ALTER TABLE


To create a DEFAULT constraint on the "City" column when the table is
already created, use the following SQL:

ALTER TABLE Persons


ALTER City SET DEFAULT 'BHARUCH';

DROP a DEFAULT Constraint


To drop a DEFAULT constraint, use the following SQL:

ALTER TABLE Persons


ALTER City DROP DEFAULT;

ALTER TABLE Persons


ALTER COLUMN City DROP DEFAULT;

EXAMPLES
1. create table employee1
(
eid integer primary key,
ename varchar(30),

Prepared By Heena Patel (Asst. Prof.) Page 15


MKICS,BHARUCH SQL DATABASE
salary integer ,
deptid integer ,
foreign key(deptid) references dept(deptid),
check(salary > 0)
);

2. create table doctormaster (


doctorno varchar(50) primary key,
name varchar(50),
address varchar(70),
type varchar(2));

3. create table patientmaster (


patientno integer ,
name varchar(50),
address varchar(90),
contact integer,
constraint p1 primary key(patientno)
);

4. create table treatement


(
doctorno varchar(50) ,
patientno integer ,
admitdate date,
dischargedate date ,
constraint fk11 foreign key(doctorno) references doctormaster
(doctorno) ,
constraint fk22 foreign key ( patientno ) references patientmaster
(patientno) ,
constraint pk33 primary key (doctorno,patientno,admitdate));
5. create table employee
(
eid integer primary key,
ename varchar(30),
salary integer ,
deptid integer ,
constraint f1 foreign key(deptid) references dept(deptid),
constraint c1 check(salary > 0)
);

Prepared By Heena Patel (Asst. Prof.) Page 16


MKICS,BHARUCH SQL DATABASE

SQL INSERT INTO Statement


The INSERT INTO statement is used to insert new records in a table.

Syntax
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

If you are adding values for all the columns of the table, you do not
need to specify the column names in the SQL query.

INSERT INTO table_name


VALUES (value1, value2, value3, ...);

Example

INSERT INTO Customers (customerid,CustomerName,city,country)


VALUES (1, 'Tom','surat',’india’);

Insert Data Only in Specified Columns


INSERT INTO Customers(customerid,CustomerName,City)
VALUES (2,'Cardinal','baroda');

SQL UPDATE Statement


The UPDATE statement is used to modify the existing records in a
table.

Prepared By Heena Patel (Asst. Prof.) Page 17


MKICS,BHARUCH SQL DATABASE

Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Example
The following SQL statement updates the first customer (CustomerID
= 1) with a new contact person and a new city.

UPDATE Customers
SET ContactName = 'Schin', City= 'surat'
WHERE CustomerID = 1;

UPDATE Multiple Records


The following SQL statement will update the city to "bombay" for all
records where country is "india":

UPDATE Customers
SET city='bombay'
WHERE Country='india';

SQL DELETE Statement


The DELETE statement is used to delete existing records in a table.

Syntax
DELETE FROM table_name WHERE condition;

Prepared By Heena Patel (Asst. Prof.) Page 18


MKICS,BHARUCH SQL DATABASE

Example
The following SQL statement deletes the customer "Alfreds Futterkiste"
from the "Customers" table:

DELETE FROM Customers WHERE CustomerName='Ali’;

Delete All Records


DELETE FROM table_name;

Example
DELETE FROM Customers;

Prepared By Heena Patel (Asst. Prof.) Page 19

You might also like