SQL-DDL and DML
SQL-DDL and DML
Language)
What is SQL?
CREATE
1 Creates a new Database , table, a view of a table, or other object in the
database.
ALTER
2 Modifies an existing database object, such as a table.
DROP
3 Deletes an entire database or table, a view of a table or other objects in the
database.
Truncate
4 the purpose of deleting all records, but not the table structure
SQL commands…..
The INSERT INTO statement is used to insert new rows into a table.
Syntax
Syntax
SELECT * FROM table_name ;
e.g. select * from person;
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
LIKE Search for a pattern
Example
This is wrong:
Category
SELECT
SELECT DISTINCT
DISTINCT category
category Gadgets
FROM
FROM Product
Product Photography
Household
Compare to:
Category
Gadgets
SELECT
SELECT Gadgets
category
category Photography
FROM
FROM Product
Product Household
We want to add a first name to the person with a last name of "Rasmussen":
UPDATE Person SET FirstName = 'Nina‘ WHERE LastName = 'Rasmussen'
SQL DELETE Statement
SELECT <columns>
The ORDER BY clause FROM <tables>
sorts the results of a
WHERE <condition>
query
You can sort in ORDER BY <cols>
ascending (default) or [ASCENDING |
descending order
DESCENDING|
ASC | DESC ]
ORDER BY Example
Drop database
You can delete an existing DB, Table with the
DROP Database statement.
To delete a Database
Syntax
DROP DATABASE database_name ;
E.g. drop database Employee;
ALTER Command
The Alter command is used to modify the structure of the table. The
command helps to
For the purpose of deleting all records, but not the table
structure, we would prefer to use the TRUNCATE
Command.
Syntax:
TRUNCATE TABLE <table_name>
Example:
Q7> Truncate table Employee;
SQL AND & OR AND & OR
Example
Use AND to display each person with the first name
equal to "Tove", and the last name equal to "Svendson":
Example
Use OR to display each person with the first name equal
to "Tove", or the last name equal to "Svendson":
Result:
SQL AND & OR AND & OR…
Example
You can also combine AND and OR (use parentheses to form
complex expressions):
With SQL, aliases can be used for column names and table
names.
Column Name Alias
The syntax is:
SELECT column AS column_alias FROM table
Example
Who has ordered a
product, and what did they
order?
Foreign key