Database-chap 8 Flashcards
SQL (31 cards)
SQL functions fit into two broad categories
Data definition language
Data manipulation language
Data definition language
SQL includes commands to:
-Create database objects, such as tables, indexes, and views
-Define access rights to those database objects
Data manipulation language
Includes commands to insert, update, delete, and retrieve data within database tables
Data Types
Data type selection is usually dictated by nature of data and by intended use
SQL Constraints
-NOT NULL constraint
Ensures that column does not accept nulls
-UNIQUE constraint
Ensures that all values in column are unique
-DEFAULT constraint
Assigns value to attribute when a new row is added to table
-CHECK constraint
Validates data when attribute value is entered e.g min order
SQL Indexes
When primary key is declared, DBMS automatically creates unique index
Create index
Using CREATE INDEX command, SQL indexes can be created on basis of any selected attribute
CREATE UNIQUE INDEX indexname
ON tablename (column1);
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
Adding Table Rows notice that:
-Row contents are entered between parentheses
-Character and date values are entered between apostrophes
-Numerical entries are not enclosed in apostrophes
-Attribute entries are separated by commas
-A value is required for each column
-Use NULL for unknown values
Insert
Used to enter data into table
Syntax:
INSERT INTO TABLE VALUES (value1, value2, … , valuen);
Commit
Will permanently save any changes made to any table in the database
COMMIT[WORK];
Changes made to table contents are not physically saved on disk until, one of the following occurs:
-Database is closed
-Program is closed
-COMMIT command is used
SELECT
-Used to list contents of table
Syntax:
SELECT columnlist FROM tablename;
UPDATE
Syntax:
UPDATE tablename SET columnname = expression columname = expression columnname WHERE conditionlist];
ROLLBACK
-Used to restore database to its previous condition
-Only applicable if COMMIT command has not been used to permanently store changes in database
Syntax:
ROLLBACK;
COMMIT and ROLLBACK only work with data manipulation commands that are used to add, modify, or delete table rows
Delete
Deletes a table row
Syntax:
DELETE FROM tablename[WHERE conditionlist ];
Inserting Table Rows with a Select Subquery
INSERT
-Inserts multiple rows from another table (source)
-The values returned by the SELECT subquery must match the attributes and data types of the table in the INSERT statement
-Uses SELECT subquery
Query that is embedded (or nested) inside another query
Executed first
Selecting Rows with Conditional Restrictions
Syntax:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
Selecting Rows with Conditional Restrictions
Syntax:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ]
Arithmetic Operators: The Rule of Precedence
-Perform operations within parentheses
-Perform power operations
-Perform multiplications and divisions
-Perform additions and subtractions
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
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 TABLE PRODUCT
{ADD PROD_NAME};
Changing a Column’s Data Type
ALTER can be used to change data type
ALTER TABLE PRODUCT
MODIFY (V_CODE CHAR(5));
Dropping a Column
Use ALTER to drop column
-Some RDBMSs impose restrictions on the deletion of an attribute
E.G. ALTER TABLE VENDOR
DROP COLUMN V_ORDER;