0% found this document useful (0 votes)
12 views15 pages

Week 8-9 DDL Queries

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)
12 views15 pages

Week 8-9 DDL Queries

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/ 15

Week 8-9

DDL Queries
By shafiullah Durrani
Lecturer CS/IT
Agricultural university Peshawar
Data Definition Language(DDL)
•In order to make/perform changes on the physical structure of any table
residing inside a database, DDL is used. These commands when executed
are auto commit in nature and all the changes in the table are reflected
and saved immediately. DDL commands includes :
Creating Databases Code

1) The general command for creating a database:


CREATE DATABASE database_name;

A specific example:
CREATE DATABASE IT_students;

2) To drop a database:
DROP DATABASE database_name;
For Example:
DROP DATABASE It_students;
Remember to be careful with this command! Once you drop a database, it's gone!
3) Use a Database command
USE <database name>;
-- example:
USE IT_students;

4) Rename A Database Command

EXEC sp_renamedb 'Test', 'Test2‘

Note: Test is the name of database you want to change


and Test2 is the new name for the database
Tables in SQL
Microsoft SQL Server is a relational database management systems (RDBMS)
that, at its fundamental level, stores the data in tables. The tables are the database
objects that behave as containers for the data, in which the data will be logically
organized in rows and columns format

Person ID LastName First Name Address City


1 Nasir Khan xyz Peshawar
SQL DATA Types
SQL Numeric Data Types SQL Character and String Data Types SQL Date and Time Data Types

Datatype Datatype Description Datatype Description

Fixed length with Stores date in the format YYYY-


bit DATE
MM-DD
CHAR maximum length of 8,000
tinyint characters Stores time in the format
TIME
HH:MI:SS
smallint Variable length storage
int VARCHAR with maximum length of Stores date and time
DATETIM
8,000 characters E
information in the format YYYY-
MM-DD HH:MI:SS
bigint
Variable length storage
VARCHAR with provided max Stores number of seconds
TIMESTA
decimal (max) characters, not supported MP
passed since the Unix epoch
(‘1970-01-01 00:00:00’ UTC)
numeric in MySQL
Stores year in 2 digit or 4 digit
float Variable length storage format. Range 1901 to 2155 in
TEXT with maximum size of YEAR
4-digit format. Range 70 to 69,
real 2GB data representing 1970 to 2069.
1) The SQL CREATE TABLE Statement
The CREATE TABLE statement is used to create a new table in a database.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

Example
CREATE TABLE student (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
2) The SQL SHOW TABLE COLUMN STATEMENT

EXEC sp_columns 'Your Table Name‘

If I want to get all columns in Student table of database then it would be


EXEC sp_columns ‘student'

3)ALTER TABLE - ADD Column


To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;

Example
ALTER TABLE Student
ADD Email varchar(255);
4) 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;

Example
ALTER TABLE Student
DROP COLUMN Email;

5) ALTER TABLE - ALTER/MODIFY


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

SQL Server ALTER TABLE table_name


ALTER COLUMN column_name datatype;
5) Rename a Table in SQL
EXEC sp_rename cs_std2, students;

6) Rename a column name in SQL

'students.name', 'StudentName' ,
EXEC sp_rename
'COLUMN' ;
SQL Constraints
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);

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:

•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

•INDEX - Used to create and retrieve data from the database very quickly
1)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 Student (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);

2)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
ALTER Column Age int NOT NULL;
3) 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:
SQL Server
ALTER TABLE Persons
ADD UNIQUE (ID);

4 ) sQL CHECK Constraint


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)
);
5) SQL CHECK on ALTER TABLE
To create a CHECK constraint on the "Age" column when the table is already created,
use the following SQL:
ALTER TABLE Persons
ADD CHECK (Age>=18);

6) SQL DROP TABLE Statement


The DROP TABLE statement is used to drop an existing table
in a database.
Syntax
DROP TABLE table_name;

Example
DROP TABLE Student;

7) SQL TRUNCATE TABLE


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

You might also like