7-seventh chapter
7-seventh chapter
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.
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.
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 ] ;
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.
• 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: