0% found this document useful (0 votes)
25 views19 pages

Unit 2

Uploaded by

Praveena G
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)
25 views19 pages

Unit 2

Uploaded by

Praveena G
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/ 19

CREATE DATABASE databasename;

DROP DATABASE databasename;


CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
• CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
• Try it Yourself
PersonID LastName FirstName Address City
• INSERT INTO

INSERT INTO table_name (column1, column2, column3,...)


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

• INSERT INTO table_name


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

• Eg:
• INSERT INTO Customers (CustomerName, City, Country)
VALUES (‘Cardio', 'Stavanger', 'Norway');
DROP TABLE statement is used to drop an
existing table in a database.

• DROP TABLE table_name;

• DROP TABLE persons;


• Try it Yourself
TRUNCATE TABLE
statement is used to delete the data inside a
table, but not the table itself.

TRUNCATE TABLE table_name;


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

ALTER TABLE table_name


ADD column_name datatype;

ALTER TABLE Customers


ADD Email varchar(255);
• NOT NULL on CREATE TABLE

• CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
• UNIQUE Constraint

• CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);
• PRIMARY KEY on CREATE TABLE

• 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
);

• CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
);
• FOREIGN KEY Constraint
• Persons table
PersonID LastName FirstName Age

1 Hansen Ola 30

2 Svendson Tove 23

3 Pettersen Kari 20
OrderID OrderNumber PersonID

1 77895 3

2 44678 3

3 22456 2

4 24562 1
• CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(Perso
nID)
);
• CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID
REFERENCES Persons(PersonID)
);
• CHECK Constraint

• CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
• DEFAULT Constraint

• CREATE TABLE Orders (


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

You might also like