0% found this document useful (0 votes)
2 views

7-seventh chapter

Chapter 7 of the Data Base Course covers SQL, focusing on its basic commands for data administration and manipulation, including creating tables, indexes, and querying databases. It explains the structure of SQL commands, data types, and advanced operations like joins and views. The chapter also details how to insert, update, delete, and retrieve data, along with the use of special operators and the ALTER command for modifying database structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

7-seventh chapter

Chapter 7 of the Data Base Course covers SQL, focusing on its basic commands for data administration and manipulation, including creating tables, indexes, and querying databases. It explains the structure of SQL commands, data types, and advanced operations like joins and views. The chapter also details how to insert, update, delete, and retrieve data, along with the use of special operators and the ALTER command for modifying database structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Data Base Course Chapter 7 (SQL)

In this chapter, we focus on the following points:-


• Explore basic commands and functions of SQL
• How to use SQL for data administration (to create tables, indexes, and views)
• How to use SQL for data manipulation (to add, modify, delete, and retrieve
data)
• How to use SQL to query a database to extract useful information
7.1 Introduction of SQL

SQL is relatively easy to learn, the basic command set has vocabulary of less than 100
words, it is considered nonprocedural language and American National Standards
Institute (ANSI) prescribes a standard SQL. SQL functions fit into two broad
categories:
1- Data definition language
SQL includes commands to:
 Create database objects, such as tables, indexes, and views
 Define access rights to those database objects
2- Data manipulation language
Includes commands to insert, update, delete, and retrieve data within database
tables.

By Asst. Prof. Dr. Ali A. Yassin (1)


Data Base Course Chapter 7 (SQL)

7.2 Creating the Database

Following two tasks must be completed:


1- Create database structure.
2- Create tables that will hold end-user data.
The data base schema is connected by two points:
1- Authentication: process through which DBMS verifies that only registered users
are able to access database. Log on to RDBMS using user ID and password
created by database administrator.
2- Schema: Group of database objects—such as tables and indexes—that are related
to each other.
Each field inside table should be tacked one of the following data types:

By Asst. Prof. Dr. Ali A. Yassin (2)


Data Base Course Chapter 7 (SQL)

Now, when we wish to create table structure, there are many standards that should be
followed:
• Use one line per column (attribute) definition
• Use spaces to line up attribute characteristics and constraints
• Table and attribute names are capitalized
• NOT NULL specification
• UNIQUE specification
• Primary key attributes contain both a NOT NULL and a UNIQUE
specification
• RDBMS will automatically enforce referential integrity for foreign keys
• Command sequence ends with semicolon
• Primary key attributes contain both a NOT NULL and a UNIQUE
specification
• RDBMS will automatically enforce referential integrity for foreign keys
• Command sequence ends with semicolon
7.3 SQL Index
 When primary key is declared, DBMS automatically creates unique index
 Often need additional indexes
 Using CREATE INDEX command, SQL indexes can be created on basis of
any selected attribute
 Composite index
- Index based on two or more attributes
- Often used to prevent data duplication
7.4 Data Manipulation Commands
• Adding table rows
• Saving table changes
• Listing table rows
• Updating table rows
• Restoring table contents
• Deleting table rows
• Inserting table rows with a select subquery
In adding Table Rows,
- INSERT : Used to enter data into table.

By Asst. Prof. Dr. Ali A. Yassin (3)


Data Base Course Chapter 7 (SQL)

The main Syntax as follows:


INSERT INTO columnname, VALUES (value1, value2, … , valuen);
When entering values, notice that:
1- Row contents are entered between parentheses
2- Character and date values are entered between apostrophes
3- Numerical entries are not enclosed in apostrophes
4- Attribute entries are separated by commas
5- A value is required for each column
6- Use NULL for unknown values
Any changes made to table contents are not physically saved on disk until, one of the
following occurs:
1- Database is closed
2- Program is closed
In Listing Table Rows:
SELECT : Used to list contents of table
Syntax: SELECT columnlist FROM tablename;
Columnlist represents one or more attributes, separated by commas.
In Updating Table Rows:
The main functions of UPDATE command as follows:
1- Modify data in a table
2- Syntax:
UPDATE tablename SET columnname = expression [, columname =
expression] [WHERE conditionlist];
* If more than one attribute is to be updated in row, separate corrections with commas.
In Deleting Table Rows:
 Deletes a table row
 Syntax:
DELETE FROM tablename [WHERE conditionlist ];
WHERE condition is optional. If WHERE condition is not specified, all rows from
specified table will be deleted.
7.4.1 Compound Conditions
1- Inserting Table Rows with a Select Subquery:
 Inserts multiple rows from another table (source)

By Asst. Prof. Dr. Ali A. Yassin (4)


Data Base Course Chapter 7 (SQL)

 Uses SELECT subquery: Query that is embedded (or nested) inside another
query and executed first
 Syntax:
INSERT INTO tablename SELECT columnlist FROM tablename;
2- Selecting Rows with Conditional Restrictions
 Select partial table contents by placing restrictions on rows to be included in
output and Add conditional restrictions to SELECT statement, using WHERE
clause
• Syntax:
SELECT columnlist FROM tablelist [ WHERE conditionlist ] ;

By Asst. Prof. Dr. Ali A. Yassin (5)


Data Base Course Chapter 7 (SQL)

3- Arithmetic Operators:
The Rule of Precedence as follows:
• Perform operations within parentheses
• Perform power operations
• Perform multiplications and divisions
• Perform additions and subtractions

4- Special Operators
• BETWEEN
Used to check whether attribute value is within a range.
• IS NULL
Used to check whether attribute value is null.
• LIKE
Used to check whether attribute value matches given string pattern.

By Asst. Prof. Dr. Ali A. Yassin (6)


Data Base Course Chapter 7 (SQL)

• IN
Used to check whether attribute value matches any value within a value list.
• EXISTS
Used to check if subquery returns any rows.
7.4.2 Advanced Data Definition Commands
• All changes in table structure are made by using ALTER command
- Followed by keyword that produces specific change
- Following three options are available:
* ADD
* MODIFY
* DROP
• ALTER can be used to change data type, some RDBMSs (such as Oracle) do
not permit changes to data types unless column to be changed is empty.
• Use ALTER to change data characteristics
• If column to be changed already contains data, changes in column’s
characteristics are permitted if those changes do not alter the data type
The ALTER Command can use as follows:-
1- Adding a Column:
Use ALTER to add column that do not include the NOT NULL clause for new
column.
2- Dropping a Column
Use ALTER to drop column where some RDBMSs impose restrictions on the deletion
of an attribute.
3- Copying Parts of Tables
• SQL permits copying contents of selected table columns so that the data need
not be reentered manually into newly created table(s).
• First create the PART table structure.
Next add rows to new PART table using PRODUCT table rows.
4- Adding Primary and Foreign Key Designations
• When table is copied, integrity rules do not copy, so primary and foreign keys
need to be manually defined on new table
• User ALTER TABLE command
– Syntax:

By Asst. Prof. Dr. Ali A. Yassin (7)


Data Base Course Chapter 7 (SQL)

 ALTER TABLE tablename ADD PRIMARY KEY(fieldname);


 For foreign key, use FOREIGN KEY in place of PRIMARY KEY.
5- Deleting a Table from the Database
• DROP
- Deletes table from database
- Syntax: DROP TABLE tablename;
7.4.3 Advanced Select Queries
SQL provides useful functions that can:
- Count.
- Find minimum and maximum values.
- Calculate averages.
- SQL allows user to limit queries to only those entries having no duplicates
or entries whose duplicates may be grouped.

7.4 Virtual Tables: Creating a View


• View is virtual table based on SELECT query
– Can contain columns, computed columns, aliases, and aggregate
functions from one or more tables
• Base tables are tables on which view is based
• Create view by using CREATE VIEW command

By Asst. Prof. Dr. Ali A. Yassin (8)


Data Base Course Chapter 7 (SQL)

7.5 Joining Database Tables


• Ability to combine (join) tables on common attributes is most important
distinction between relational database and other databases.
• Join is performed when data are retrieved from more than one table at a time.
• Join is generally composed of an equality comparison between foreign key
and primary key of related tables.
• Alias can be used to identify source table
• Any legal table name can be used as alias
• Add alias after table name in FROM clause
FROM tablename alias

By Asst. Prof. Dr. Ali A. Yassin (9)

You might also like