Chapter 6 Structured Query Language (SQL)
Chapter 6 Structured Query Language (SQL)
(SQL)
Structured Query Languages (SQL)
7
SQL Language (DDL, DML, TCL and DCL)
An SQL data type refers to the type of data which can be stored
in a column of a database table. For example integer data,
character data, monetary data, date and time data, binary
strings, and so on.
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 are the rules that we can apply on the type of data in a
table. That is, we can specify the limit on the type of data that can
o As we can see clearly that the field C_ID in Orders table is the
primary key in Customers table, i.e. it uniquely identifies each row
in the Customers table. Therefore, it is a Foreign Key in Orders
table.
Syntax:
CREATE TABLE Orders
(
O_ID int NOT NULL,
ORDER_NO int NOT NULL,
C_ID int, PRIMARY KEY (O_ID),
FOREIGN KEY (C_ID) REFERENCES Customers(C_ID)
); 20
SQL - CHECK Constraint
); 21
SQL - CHECK Constraint Cont’d….
Example 1
For example, the following program creates a new table called CUSTOMERS and
adds five columns. Here, we add a CHECK with AGE column, so that you cannot
have any CUSTOMER who is below 18 years.
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL CHECK (AGE >= 18),
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
22
PRIMARY KEY (ID) );
SQL - CHECK Constraint Cont’d….
Example
create table customer
(
CID varchar(15) primary key,
CName varchar(15) not null,
Age int constraint C1 check(Age>=18),
Salary decimal(5,2)
); 24
SQL - CHECK Constraint Cont’d….
Example
Example
For example, the following SQL creates a new table called CUSTOMERS
and adds five columns. Here, the SALARY column is set to 5000.00 by
default, so in case the INSERT INTO statement does not provide a value
for this column, then by default this column would be set to 5000.00. 26
SQL - Default Constraint Cont’d….
schema)
28
DDL-Creating a Database
Syntax:
Example:
Syntax:
Example:
use DatabaseName;
Example:
31
use DB;
DDL- Show a Database
Syntax o Example
create table student
CREATE TABLE table_name (
SID varchar(23),
(Column_name datatype[(size)], Fname varchar(17),
Mname varchar(15),
Column_name datatype[(size)] Lname varchar(14),
D_Birth date,
) Age int,
Grade float
33
);
DDL-Drop table
Syntax
Syntax
36
ALTER TABLE table_name DROP COLUMN column_name;
DDL-Truncate table
Remove all records from a table, including all spaces allocated for
the records are removed.
Syntax:
37
DDL-Rename table
Syntax:
Example:
EXEC sp_rename ’st', ’stud’ 38
Exercise
Syntax
SELECT column FROM table WHERE column operator value
Example
select * from New_student where Grade =3.95;
select SID, D_Birth,Grade from New_student where Fname ='Meron';
Using the WHERE Clause
• Note that we have used single quotes around the conditional values
in the examples.
• SQL uses single quotes around text values (most database systems
will also accept double quotes). Numeric values should not be
enclosed in quotes.
For text values:
This is correct:
select * from New_student where Fname ='Meron';
This is wrong:
select * from New_student where Fname ='Meron';
The LIKE Condition
• The following SQL statement will return Students with first names
that start with an ‘a':
• The following SQL statement will return students with first names
that end with an 'a':
You can also specify the columns for which you want to insert data:
Example 1:
insert into stud values ('IS/00/14','Abel','Tamirat','Yonas',27,3.56);
Example 2:
insert into stud values
('IS/007/14','A','B','C',27,3.00),
('IS/008/14/','AA','BB','CC',25,4.00),
('IS/009/14','D','E','F',22,3.99);
Example 3:
insert into stud (SID,Fname,Mname,Lname,AGE,Grade)
values ('IS/005/14','Abebe','Sofi','Yishak',27,3.56);
The Update Statement
Syntax
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value
Update one Column in a Row
Example
update Stud set Grade = 3.80
where Fname = 'Abel';
Update several Columns in a Row
Syntax
UPDATE table_name,
SET column_name1 = new_value, column_name2 = new_value, …
column_namen = new_value
WHERE column_name = some_value
Example:
Syntax
example:
Projection
display all columns, "*" must be used. Column names are inserted next
Selection
▪ Syntax:
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SQL Aggregate Functions-MIN() and MAX() FUNCTION
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
▪ Syntax:
• SELECT MIN(column_name)
FROM table_name
WHERE condition;
• SELECT MAX(column_name)
FROM table_name
WHERE condition;
SQL - Group By Clause
o The SQL GROUP BY clause groups rows of data with the same values in the
specified columns. This clause is most commonly used with SQL aggregate functions
to compute statistics (such as a count of certain values, sum, average, and the
minimum/maximum value in a set) for a group of rows.
o It is used to arrange similar data into groups. The GROUP BY clause follows the
WHERE clause comes before the ORDER BY clause.
SQL - Group By Clause
o Syntax:
SELECT column1, column 2… FROM table_name WHERE [condition] GROUP
BY column1, column2 ORDER BY column1, column2;
o Example:
o If we want to know the total salary of each Employe, then GROUP BY is used as
follows:
• SELECT FName, Sum(salary) FROM EMP GROUP BY FName;
• SELECT FName, count(salary) FROM EMP GROUP BY FName;
SQL - Order By Clause