casharia
Labaad
SQL Course
Semester Four
Chapter TWO
DDL & DML
SQL Commands
SQL Commands are instructions that are used by the user to communicate with the
database, to perform specific tasks, functions and queries of data
Koowaad
Qaybta
Section ONE
Data Definition Language
Data Definition Language update
DDL is used to define and modify database structures.
CREATE Command
The CREATE statement in SQL is used to create new databases, tables, indexes, or other
database objects. The most commonly used form is CREATE TABLE, which defines a
new table and its columns.
Create Object Object Name
• Table
• Database
• Views
CREATE Command
Before creating tables, we first need to create a database where all the tables will be
stored.
command
object obsactama
a
Create Database School
Database Is fixed when
Keyword Database Name
we creating database
Using the Database
Once the database is created, we need to select it before creating tables.
command
tama
keyword object
USE School
Keyword Database Name
Show All Databases in SQL Server
SELECT * FROM sys.databases
command
CREATE Command - Tables
To create table in database
Create Table Students
Table Key word Is fixed when
Keyword Table Name
we creating tables
Create Table Syntax
CREATE TABLE table_name (
column_name1 data_type Constraints,
column_name2 data_type [NULL | NOT NULL],
column_name3 data_type [NULL | NOT NULL],
...,
);
SQL Data Types
one
chapter
Aktin
Fadlan Horay ayong
Kaan
Cashar
usoo goadanay
SQL Constraints
Create Table Students
CREATE TABLE Students (
STUDENT_ID INT NOT NULL Primary Key,
StudentName Varchar(100),
Age INT CHECK (Age >0),
Enrollment_Date DATETIME DEFAULT,
Class Varchar(10) ,
);
Create Table Grades
CREATE TABLE Grades (
STUDENT_ID INT,
Subject Varchar(25),
Score INT CHECK (SCORE Between 0 and 100),
Exam_DATE DATETIME DEFAULT,
FOREIGN KEY (STUDENT_ID) REFERENCES
Students(STUDENT_ID)
);
View Table Structure
EXEC sp_help Students ;
ALTER
I
badalis
ka
wax
modify update
ALTER
The ALTER statement is used to modify an existing table structure without deleting or
recreating it. It allows us to add, delete, or modify columns and constraints
in a table
ALTER TABLE table_name
ADD | DROP | MODIFY | ALTER COLUMN column_name data_type;
ALTER
The ALTER statement is used to modify an existing table structure without deleting or
recreating it. It allows us to add, delete, or modify columns and constraints
in a table
ALTER TABLE table_name
ADD | DROP | MODIFY | ALTER COLUMN column_name data_type;
ALTER
Adding new column with an existing table using Alter
ALTER TABLE students ADD Age int;
Now, the "Students" table has an extra column named "Age"
Modifying an Existing Column
Changing the data type or constraints of a column
ALTER TABLE students ALTER COLUMN age BIGINT;
Deleting a Column
Removing an unnecessary column
ALTER TABLE students Drop COLUMN age;
! Be careful! Dropping a column permanently deletes its data
⚠
Adding a Constraint
Adding a NOT NULL constraint to a column
ALTER TABLE students Alter COLUMN FirstName Varchar(50) NOT NULL;
DROP
DROP
The DROP statement is used to remove objects like databases, tables, columns,
or constraints permanently from the database.
Drop Object Object Name
Database, Table Object Name
Keyword
DROP
Dropping a Database
Drop Database school_db
Dropping a Table
Drop Table students
Labaad
Qoybta
Section Two
Data Manipulation Language
Data Manipulation Language
• DML is a subset of SQL used for managing and manipulating data in
a database
• It allows you to retrieve, insert, update, and delete records in the
database tables.
DML
inaad xog 109
9
1 tirtat
I Insert Update Delete
Overview of DML
Command Purpose Example
SELECT Retrieves data from one or SELECT * FROM Students
more tables.
INSERT Adds new rows to a table. INSERT INTO Students (ID,
Name) VALUES (1, Ahmed')
UPDATE Modifies existing records. UPDATE Students SET Name =
Ali' WHERE ID = 1
DELETE Removes records from a table. DELETE FROM Students
WHERE ID = 1
INSERT Command
The INSERT command adds new rows of data to a table.
INSERT INTO TABLE table_name (column1 ,column2 , column3 )
Values (‘value’, ‘value’, ‘value’ )
INSERT Command
The INSERT command adds new rows of data to a table.
UPDATE Command
The UPDATE command modifies existing records in a table based on a condition.
Update TABLE table_name SET column = value , column = value
Where condition;
UPDATE Command
The UPDATE command modifies existing records in a table based on a condition.
DELETE Command
The DELETE command removes records from a table based on a condition
Delete FROM table_name Where condition;
SELECT Command
The SELECT command is used to query the database and retrieve data from one or
more tables
SELECT column1, column2, ... FROM table_name WHERE
condition
SELECT All Columns
If you want to select all columns from a table, use the asterisk * symbol:
SELECT * FROM Students ;
SELECT Specific Columns
To retrieve specific columns, specify the column names
This will retrieve the StudentName, Age, and Class columns from the Students table.
SELECT with WHERE Clause (Filtering Data)
The WHERE clause is used to filter records based on a condition. For example, to
select students who are 20 years old or older
This will retrieve the StudentName and Age of students who are 20 years old or older
saddataad
Qousta
Section Three
ALIAS
I
myonal
Magac
Nama
Fake
ALIAS
• An alias is a temporary name assigned to a table or a column in a
query. It does not change the actual table or column name but
makes the output more readable.
Aliases
Column Table
Column ALIAS
• Used to rename column names in the output
• The AS keyword is used
Formatting Column Names with out AS
Table Alias
• Shortens table names for better readability in complex queries
• Mostly used when working with joins
Aliases
Column Table
four
r
Section SQL Operators
AFaraad
Qaybta
Operators in SQL
• Operators are used to specify conditions in an SQL statement and to
serve as conjunctions for multiple conditions in a statement.
Operators
Arithmetic Comparison Logical
operators operators operators
SQL Arithmetic Operators
• Operators are used to specify conditions in an SQL statement and to
serve as conjunctions for multiple conditions in a statement.
SQL Arithmetic Operators
Examples
SQL Comparison Operators
Examples
SQL Comparison Operators
Consider the CUSTOMERS table having the following records:
Retrieving High- Earning Customers
This query selects all customers whose salary is greater than 5000 from the
CUSTOMERS table
Retrieving Customers with a Salary of 2000
This query selects all records from the CUSTOMERS table where the salary is
exactly 2000.
Retrieving Customers with a Salary Not Equal to 2000
This query selects all records from the CUSTOMERS table where the salary is
NOT 2000.
SQL Logical Operators
• Here is a list of all the logical operators available in SQL
SQL Comparison Operators
Consider the CUSTOMERS table having the following records:
Retrieving Customers with a Non-Null Age
This query selects all records from the CUSTOMERS table where the age is not
NULL
Retrieving Customers Whose Names Start with 'Ko'
This query selects all customers from the CUSTOMERS table whose name starts with 'Ko'
using the LIKE operator. The % wildcard allows for any characters to follow the 'Ko'
Retrieving Customers Aged Between 25 and 27
This query selects all records from the CUSTOMERS table where the age is between 25
and 27, inclusive, using the BETWEEN operator
isatto Xig to waxaa
leadihiin Quiz Assignant
Done for Chapter Two
Nasiib vacancy