Week13 Database Commands
Week13 Database Commands
SQL Statements
Objectives
2
CREATE Command
3
CREATE DATABASE
• A database is defined as a structured set of data.
• To create a database in RDBMS, create command is used.
Syntax : CREATE DATABASE <Database_Name> ;
4
CREATE TABLE
• Create command can also be used to create tables.
• Specify the details of the columns of the tables.
• Specify the names and datatypes of various columns.
5
Example:
Creating a TABLE without table structure
6
CREATE TABLE
7
Example:
Creating a TABLE with table structure
8
Data Types
9
DROP TABLE
• It will destroy the table and all data which will be recorded in it.
10
Example: Drop a table in a database
1 List of tables in Database
3
Updated list of tables in Database
2 DROP Command
11
“ALTER TABLE” Statement
• This statement is used to add, modify or delete
constraints or columns.
Syntax : ALTER TABLE <Table_Name> Action ;
Keyword Keyword Name of the Specify the Every SQL Query ends
Table to be action to be with a semicolon
altered/ modify done on
Action table
➢ Add a column
➢ Drop a column ➢ Set a default value for the column.
➢ Change the data type of a column ➢ Add a constraint to a column.
➢ Rename a column ➢ Rename a table
12
Example: Add a column to a Table
Syntax : ALTER TABLE table_name ADD COLUMN
new_column_name data_type constraint;
Table : Department
13
TRUNCATE TABLE Statement
• To remove all data from a table, you use the DELETE statement.
• However, when you use the DELETE statement to delete all data
from a table that has a lot of data, it is not efficient.
• Therefore, you need to use the TRUNCATE TABLE statement.
Syntax : TRUNCATE TABLE <Table_Name> ;
14
TRUNCATE :
Remove all data from multiple tables
• To remove all data from multiple tables at once, you separate
each table by a comma (,) .
Syntax :
TRUNCATE TABLE <Table_Name1>, ;
<Table_Name2>, …
Keyword Keyword Every SQL Query ends
with a semicolon
Names of the Table
to delete the data
15
RENAME Command
Syntax :
RENAME TABLE <Old_Table_Name> TO <New_Table_Name> ;
16
Demo
17
Demo
18
“INSERT INTO” Statement
19
INSERT INTO : Only Values
• Specify only the value of data to be inserted without the
column names.
• Only values : Takes the advantage of the order of the columns
when the table was created.
Syntax:
20
Example
1
Database Table :
Student_Details
2 INSERT INTO :
only values
21
INSERT INTO :
Column names and Values (both)
• Specify both the columns which we want to fill and their corresponding
values.
• The number of columns and values must be the same.
• If a column is not specified, the default value for the column is used.
• The values specified must satisfy all the applicable constraints such
as primary keys.
• If a syntax error occurs or if any constraints are violated, the new row is not
added to the table and an error is returned instead.
22
INSERT INTO :
Column names and Values (both)
Every SQL Query
Syntax: ends with a
semicolon
( value1, ;
INSERT INTO Table_Name ( column1, VALUES value2,
column2, …. ) …. )
Keyword Name of the Table Keyword
23
Example :
INSERT INTO column names and values
Query :
Database Table:
Student_details
24
Database Table : Student_details
1
2
3
Database Table:
Student_details
25
Insert into specific columns in a table
Database Table :
Department
26
Insert multiple rows at a time in a single
SQL statement
name of first column, second
Syntax: Name of the Table column,…
( value1A, value1B, …. ),
(value2A, value2B, ….), ;
(value3A, value3B, ….)
Every SQL Query
…… ends with a
semicolon
multi-rows record values
27
Database table “Department” with
no records
28
Insert multiple rows at a time into a Table
INSERT INTO
Database table
after inserting
29
“SELECT” Statement
• Data Manipulation Language (DML) SQL command.
• Used to retrieve records from one or more tables.
30
SELECT all the columns from one table
Syntax : SELECT * FROM Table_Name ;
Keyword “all columns” Keyword Name of the Table Every SQL Query ends with
a semicolon
31
SELECT multiple columns from one table
Syntax :
SELECT Column1, column2 ,… FROM Table_Name ;
Keyword Selected columns Keyword Name of the Table Every SQL Query
ends with a
semicolon
Example 2:
Display multiple columns
in a table
32
SELECT Statement:
Filter rows using WHERE clause.
• WHERE clause is used to filter the results from a SELECT, INSERT,
UPDATE, or DELETE statement.
• Any row that does not satisfy this condition will be eliminated from the
output.
33
Select Statement with Where Clause
Example 3:
Example 4:
34
Select data based on comparison operator
Example 5:
35
SELECT distinct rows using DISTINCT
operator.
• Used to remove duplicates from the result set.
• The DISTINCT clause can only be used with SELECT statements.
Syntax :
36
Select Statement with DISTINCT Clause
Example :
Example :
37
SELECT Statement : Sort rows using
ORDER BY clause.
38
Database Table : Books
39
Examples
Example :
Example :
40
Syntax: UPDATE Statement
• Specify both the columns which we want to fill and
their corresponding values.
Condition to select the rows
for which the values of
Keyword Name of the Table Keyword Keyword columns needs to be updated
41
Database Table : Student_Details
42
Example 1: Update the details of Student_ID = 102
43
Example 2: Update the details of Student_ID = 103
44
Example 3: Update the already existing data in a record
45
Omitting WHERE clause:
46
Update Statement: Return Clause
• UPDATE statement returns the following command tag:
• UPDATE count
• Count is the number of rows updated including rows whose values did not
change.
• The UPDATE statement has an optional RETURNING clause that returns the
updated rows.
Syntax:
UPDATE table_name SET column1=value1, column2=value2, ...
WHERE condition RETURNING * | output_expression AS
output_name;
47
Update Statement: Return Clause
Update a row and return the updated row
Example: Modify the published_date of the course to 2020-07-01 and returns
the updated course.
Query: UPDATE course SET published_date = '2020-07-01‘ WHERE
course_id = 2 RETURNING *;
Output Result:
48
DELETE Statement
• Data Manipulation Language (DML) SQL Command.
49
Example: Database Table
Table: Employee
50
Example 1: Delete one row in a table
51
Example 2: Delete all rows in a table
52
Delete Statement: Return Clause
• By using the RETURNING clause, you can return the deleted rows to
client as follows:
▪ DELETE FROM Table_Name WHERE condition
RETURNING (select_list | *);
• The asterisk (*) allows you to return all columns of the deleted row.
• To return specific columns, you can specify them after the
RETURNING keyword.
• DELETE statement only removes data from a table.
• It doesn’t modify the structure of the table.
53
Delete Statement: Return Clause
54
SQL : Aggregate Functions
55
Aggregate Functions
56
SQL COUNT
57
Database Table
Table : Books
Table :
Employee
58
SQL COUNT
Example 1
Example 2
59
SQL SUM
• Totals the values in a given column.
• Can only use SUM on columns containing numerical values.
Example 3:
60
SQL MIN & MAX
• MIN and MAX are SQL aggregation functions that return the
lowest and highest values in a particular column.
❑ Syntax :
SELECT MIN(<column_name>) FROM <Table_Name>;
❑ Syntax :
SELECT MAX(<column_name>) FROM <Table_Name>;
61
Example: SQL MIN / MAX
Example 4 :
Example 5 :
62
SQL AVG
• Calculates the average of a selected group of values.
• It's very useful, but has some limitations.
▪ First, it can only be used on numerical columns.
▪ Second, it ignores null values completely.
Example 6 :
63
Summary
• Discussed SQL Statements.
64
Reference
• Modern database management / Jeffrey A. Hoffer, V. Ramesh,
Heikki Topi. — 10th edition. Pearson Publication.
65
66