0% found this document useful (0 votes)
36 views26 pages

SQL CH 5

Structured Query Language (SQL) is a query language used to manage data in relational database management systems (RDBMS). SQL allows users to access and manipulate data in databases. It allows users to specify conditions to retrieve data from one or more tables in a database. Some key SQL commands include SELECT to retrieve data, INSERT to add new data, UPDATE to modify existing data, DELETE to remove data, and CREATE TABLE to make a new table. SQL also uses constraints like PRIMARY KEY, FOREIGN KEY, and NOT NULL to enforce data integrity.

Uploaded by

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

SQL CH 5

Structured Query Language (SQL) is a query language used to manage data in relational database management systems (RDBMS). SQL allows users to access and manipulate data in databases. It allows users to specify conditions to retrieve data from one or more tables in a database. Some key SQL commands include SELECT to retrieve data, INSERT to add new data, UPDATE to modify existing data, DELETE to remove data, and CREATE TABLE to make a new table. SQL also uses constraints like PRIMARY KEY, FOREIGN KEY, and NOT NULL to enforce data integrity.

Uploaded by

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

Structured Query Language

By Animut B.
1 Introduction to SQL
What is SQL?
– When a user wants to get some information
from a database file, he can issue a query.
– A query is a user–request to retrieve data
or information with a certain condition.
– SQL is a query language that allows user to
specify the conditions. (instead of algorithms)
1 Introduction to SQL
Concept of SQL

– The user specifies a certain condition.


– The program will go through all the records
in the database file and select those records
that satisfy the condition.(searching).
– Statistical information of the data.
– The result of the query will then be stored
in form of a table.
1 Introduction to SQL
How to involve SQL in FoxPro
– Before using SQL, the tables should be
opened.
– The SQL command can be entered directly
in the Command Window
SQL
• Two sublanguages:
• Data Definition Language (DDL)
– Create/alter/delete tables and their attributes
• Data Manipulation Language (DML)
– Query one or more tables – discussed next !
– Insert/delete/modify tuples in tables
Some of The Most Important SQL
Commands
SQL is NOT case sensitive: SELECT is the same as select
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
• CREATE INDEX - creates an index (search key)
• DROP INDEX - deletes an index
The SQL CREATE DATABASE Statement
• The CREATE DATABASE statement is used to
create a database.
• Syntax
– CREATE DATABASE dbname;
Example
• The following SQL statement creates a
database called "my_db":
CREATE DATABASE my_db;
The SQL CREATE TABLE Statement
• The CREATE TABLE statement is used to create a table in a database.
• Tables are organized into rows and columns; and each table must have a
name.
Syntax
• CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
• The column_name parameters specify the names of the columns of the
table.
• The data_type parameter specifies what type of data the column can hold
(e.g. varchar, integer, decimal, date, etc.).
• The size parameter specifies the maximum length of the column of the
table.
Example
• Now we want to create a table called "Persons" that contains five
columns: PersonID, LastName, FirstName, Address, and City.
• We use the following CREATE TABLE statement:
• Example
• CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
SQL Constraints
• SQL constraints are used to specify rules for the data in a table.
• If there is any violation between the constraint and the data action, the
action is aborted by the constraint.
• Constraints can be specified when the table is created (inside the CREATE
TABLE statement) or after the table is created (inside the ALTER TABLE
statement).
• SQL CREATE TABLE + CONSTRAINT Syntax
• CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);
• In SQL, we have the following constraints:
• NOT NULL - Indicates that a column cannot store NULL value
• UNIQUE - Ensures that each row for a column must have a unique value
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures
that a column (or combination of two or more columns) have an unique
identity which helps to find a particular record in a table more easily
and quickly
• FOREIGN KEY - Ensure the referential integrity of the data in one table
to match values in another table
• CHECK - Ensures that the value in a column meets a specific condition
• DEFAULT - Specifies a default value when specified none for this column
SQL NOT NULL Constraint
• The NOT NULL constraint enforces a column to NOT accept NULL values.
• The NOT NULL constraint enforces a field to always contain a value. This
means that you cannot insert a new record, or update a record without
adding a value to this field.
• The following SQL enforces the "P_Id" column and the "LastName" column to
not accept NULL values:
• Example
• CREATE TABLE PersonsNotNull
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
SQL PRIMARY KEY Constraint
• The PRIMARY KEY constraint uniquely identifies each record in a database table.
• Primary keys must contain unique values.
• A primary key column cannot contain NULL values.
• Each table should have a primary key, and each table can have only ONE
primary key.
• SQL PRIMARY KEY Constraint on CREATE TABLE
• The following SQL creates a PRIMARY KEY on the "P_Id" column when the
"Persons" table is created:
• CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
SQL FOREIGN KEY Constraint on CREATE
TABLE
• The following SQL creates a FOREIGN KEY on the
"P_Id" column when the "Orders" table is
created:
• CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)
The SQL SELECT Statement
• The SELECT statement is used to select data
from a database.
• The result is stored in a result table, called the
result-set.
Syntax
– SELECT column_name,column_name
FROM table_name;
And
– SELECT * FROM table_name;
SELECT Column Example
• The following SQL statement selects the
"CustomerName" and "City" columns from the
"Customers" table:
• SELECT CustomerName,City FROM Customers;
SELECT * Example
• The following SQL statement selects all the
columns from the "Customers" table:
• SELECT * FROM Customers;
The SQL WHERE Clause
• The WHERE clause is used to extract only those
records that fulfill a specified criterion.
Syntax
• SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Example
• SELECT * FROM Customers
WHERE Country='Mexico';
The SQL AND & OR Operators
• The AND operator displays a record if both the first
condition AND the second condition are true.
• The OR operator displays a record if either the first
condition OR the second condition is true.
• Example
1. SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin';
2. SELECT * FROM Customers
WHERE City='Berlin'
OR City='München';
The SQL ORDER BY Keyword
• The ORDER BY keyword is used to sort the result-set by one or more
columns.
• The ORDER BY keyword sorts the records in ascending order by
default. To sort the records in a descending order, you can use the
DESC keyword.
Syntax
• SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
Example SELECT * FROM Customers
ORDER BY Country;
• SELECT * FROM Customers
ORDER BY Country DESC;
The SQL INSERT INTO Statement
• The INSERT INTO statement is used to insert new records in a
table.
Syntax
• It is possible to write the INSERT INTO statement in two forms.
1. The first form does not specify the column names where the data
will be inserted, only their values:
• INSERT INTO table_name
VALUES (value1,value2,value3,...);
2. The second form specifies both the column names and the values
to be inserted:
• INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
Example
• Assume we wish to insert a new row in the
"Customers" table.
• We can use the following SQL statement:
• Example
• INSERT INTO Customers (CustomerName,
ContactName, Address, City, PostalCode,
Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen
21','Stavanger','4006','Norway');
The SQL UPDATE Statement
• The UPDATE statement is used to update existing
records in a table.
Syntax
• UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
Example
• UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
The SQL DELETE Statement
• The DELETE statement is used to delete rows in a table.
Syntax
• DELETE FROM table_name
WHERE some_column=some_value;
• Example
• Assume we wish to delete the customer "Alfreds Futterkiste"
from the "Customers" table.
• We use the following SQL statement:

• DELETE FROM Customers


WHERE CustomerName='Alfreds Futterkiste' AND
ContactName='Maria Anders';
The SQL LIKE Operator
• The LIKE operator is used to search for a specified pattern in a
column.
• SQL LIKE Syntax
• SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
• Example
1. SELECT * FROM Customers
WHERE City LIKE 's%';
2. SELECT * FROM Customers
WHERE City LIKE '%s';
3. SELECT * FROM Customers
WHERE Country LIKE '%land%';
4. SELECT * FROM Customers
SQL JOIN
• An SQL JOIN clause is used to combine rows from two or
more tables, based on a common field between them.
• The most common type of join is: SQL INNER JOIN (simple
join). An SQL INNER JOIN return all rows from multiple
tables where the join condition is met.
Example
• SELECT Orders.OrderID, Customers.CustomerName,
Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
• The DROP TABLE Statement
• The DROP TABLE statement is used to delete a table.
– DROP TABLE table_name
• The DROP DATABASE Statement
• The DROP DATABASE statement is used to delete a database.
– DROP DATABASE database_name
• SQL ALTER TABLE Statement
Syntax
• To add a column in a table, use the following syntax:
– ALTER TABLE table_name
ADD column_name datatype
• To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):
– ALTER TABLE table_name
DROP COLUMN column_name
Example
• ALTER TABLE Persons
ADD DateOfBirth date
• ALTER TABLE Persons
DROP COLUMN DateOfBirth

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