0% found this document useful (0 votes)
45 views10 pages

Data Definition Language (DDL) (Slides)

Uploaded by

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

Data Definition Language (DDL) (Slides)

Uploaded by

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

SQL basics

Data definition language (DDL)


Please do not copy without permission. © ExploreAI 2023.
SQL basics
Data definition language

The sublanguage responsible for defining how


data are structured in a database in SQL is
called the data definition language (DDL).

The commands that are used to build, amend,


or remove SQL tables are contained in the data
definition language.

These commands include CREATE TABLE,


ALTER TABLE, TRUNCATE TABLE, and DROP
TABLE.

2
SQL basics
Database schemas and tables
A database schema is a logical container that
Tables are the fundamental building blocks of a
houses these tables and provides a framework for
database schema and store data in rows and
classifying, ordering, and arranging them in
columns.
relation to one another.

TABLE A TABLE B
col1 col2 col3 col1 col2 col3
x 34 s x 34 s
y 73 m y 73 m
z 22 l z 22 l
w 12 m w 12 m

3
SQL basics
Database schemas and DDL

A SQL sublanguage known as data definition


language, or DDL, is used to create, modify, or
remove SQL tables from the database schema.

4
SQL basics
CREATE DATABASE

| The CREATE DATABASE statement is used to create a new SQL database.

Syntax Example

CREATE DATABASE database_name;


USE database_name;

1. Creates a database named united_nations.


Creating a database typically requires appropriate
permissions or privileges depending on the 2. Selects the united_nations database.
database management system we are working with. All subsequent SQL operations will be
performed inside this database.

5
SQL basics
CREATE TABLE

| The CREATE TABLE statement is used to create new tables. It specifies the structure of the
table, defining the columns and their data types.

Syntax Example

CREATE TABLE table_name (


column1 datatype,
Column2 datatype [Constraint],
....
);

1. Creates a table inside the united_nations database


named Access_to_Basic_Services.

2. If the "USE database_name" function wasn't


executed, the database name is entered before
the table name.
4. After the data type, we can insert an optional
3. Inside the brackets, it defines the name of each constraint that allows us to enforce rules on the
column and its data type separated by a comma. type of data the column can have, e.g. NOT NULL.
6
SQL basics
Constraints

| When creating a table in SQL, we can apply various constraints to columns to enforce data integrity and
define rules for the values stored in those columns. Here are some commonly used constraints in SQL:

NOT NULL UNIQUE


This constraint ensures that the values in a column (or a
This constraint ensures that a column cannot contain combination of columns) are unique across the table. It
NULL values. It enforces the requirement for the column prevents duplicate values from being inserted into the
to have a non-null value for each row. column(s).

PRIMARY KEY FOREIGN KEY


The PRIMARY KEY constraint uniquely identifies each This constraint establishes a relationship between two
row in a column combining the NOT NULL and UNIQUE tables based on a column. It ensures that the values in the
constraints. The primary key column values are unique primary key column in the first table correspond to the
and cannot be null. values in the foreign key column in the second table.
7
SQL basics
ALTER TABLE

| The ALTER TABLE statement is used to modify the structure of an existing database object,
such as adding, modifying, or deleting columns in a table.

Syntax Examples
_To add a column_
ALTER TABLE table_name
ADD column_name datatype;

_To delete a column_


ALTER TABLE table_name
DROP COLUMN column_name;

_To rename a column_


ALTER TABLE table_name
RENAME COLUMN old_name to new_name;

_To change the data type of a column_


ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

8
SQL basics
TRUNCATE TABLE

| The TRUNCATE TABLE statement is used to remove all data from a table, effectively resetting it
to an empty state. This operation is faster than deleting individual rows.

Syntax Example

TRUNCATE TABLE table_name;

*If the "USE database_name" function wasn't executed, the database


name is entered before the table name.

As soon as the TRUNCATE TABLE statement is


executed, the data are permanently wiped from the Removes all the content of the
table and cannot be recovered, hence it is Access_to_Basic_Services table without deleting
important to use this command with caution. the table itself.
Appropriate backups of the data are required.

9
SQL basics
DROP TABLE and DROP DATABASE

| The DROP statements are used to remove entire database objects, such as tables or schemas,
from the database.

Syntax Example

DROP TABLE table_name;

DROP DATABASE database_name;

It is important to exercise caution when using the


DROP TABLE or DROP DATABASE statements as they Deletes the Access_to_Basic_Services table and
permanently delete the table or database, and they then deletes the united_nations database as well.
cannot be recovered.

10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy