1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
-
Upload
shonda-hardy -
Category
Documents
-
view
244 -
download
1
Transcript of 1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1
Structured Query Language (SQL)
2
Contents
• SQL – I• SQL – II• SQL – III• SQL – IV
3
SQL- I
• Table Fundamentals– Oracle Data Types
• Basic Data Types
– The CREATE TABLE Command• Rules for Creating Tables• A brief checklist when creating tables
– Inserting data into tables
• Viewing data in the tables– All rows and all columns– Filtering table data
• Selected columns and all rows• Selected rows and all columns• Selected columns and selected rows
• Eliminating duplicate rows when using SELECT statement• Sorting data in a table• Creating a table from a table• Inserting data into a table from another table
– Insertion of a data set into a table from another table
4
• Delete Operations– Removal all rows– Removal of specific row (s)– Removal of specific row (s) based on the data held by the other table
• Updating the contents of a table– Updating all rows– Updating records conditionally
• Modifying the structure of tables– Adding new columns– Dropping a column from a table– Modifying existing columns– Restrictions on the ALTER TABLE
• Renaming tables• Truncating tables• Destroying tables• Examining objects created by a user
5
• Table Fundamentals:A table is database object that holds user data– Oracle Data Types
• Basic Data Typeschar (size) – fixed length string.varchar (size)/varchar2 (size) – variable length stringdate - number (p, s) – precision, scale, eg. number (7, 2) 5 digit before decimal and 2 after.long – variable length stringraw/ long raw – variable length binary string
– The CREATE TABLE Command• Rules for Creating Tables
a name can have maximum up to 30 characters.alphabets from A-Z, a-Z and numbers from 0-9 are allowed.a name should begin with an alphabets.the use of special character like _ is allowedSQL reserved words not allowed
• A brief checklist when creating tablesattributes, data types, primary key, default values
CREATE TABLE <TableName> (<columnname1> <datatype>(<size>), <columnname2> <datatype>(<size>));
6
– Inserting data into tables
• Viewing data in the tablesSELECT– All rows and all columns
SELECT * FROM <tablename>;– Filtering table data
• Selected columns and all rowsSELECT <columnname1>, <columnname2> FROM <tablename>;
• Selected rows and all columnsSELECT * FROM <tablename> WHERE <condition>;
• Selected columns and selected rowsSELECT <column1>, <column2> FROM <table> WHERE <condition>;
INSERT INTO <tablename> (<columnname1>, <columnname2>) VALUES (<expression1>, <expression2>);
7
• Eliminating duplicate rows when using SELECT statementDISTINCTSELECT DISTINCT * FROM <tablename>;
• Sorting data in a tableSELECT * FROM <tablename> ORDER BY <column1>, <column2> <[Sort Order]>;Sort Order: DESC
• Creating a table from a tableCREATE TABLE <tablename1> (<column1>, <column2>) AS SELECT <column1>, <column2> FROM <tablename2>;eg. Create table to retrieve the table structure.
WHERE• Inserting data into a table from another table
INSERT INTO <tablename1> SELECT <column1>, <column2> FROM <tablename2>;– Insertion of a data set into a table from another table
INSERT INTO <tablename1> SELECT <column1>, <column2> FROM <tablename2> WHERE <condition>;
8
• Delete OperationsDELETE– Removal all rows
DELETE FROM <tablename>;– Removal of specific row (s)
DELETE FROM <tablename> WHERE <condition>;– Removal of specific row (s) based on the data held by the other table
EXISTSDELETE FROM table1 WHERE Exists (SELECT fname FROM tabel2 WHERE table2.cust_no = table1. code_no AND table2.fname = “abc”);
• Updating the contents of a tableUPDATE– Updating all rows
UPDATE <tablename> SET <column1> = <expression1>, <column2> = <expression2>;
– Updating records conditionallyUPDATE <tablename> SET <column1> = <expression1>, <column2> = <expression2> WHERE <condition>;
9
• Modifying the structure of tablesALTER TABLE– Adding new columns
ALTER TABLE <tablename> ADD (<newcolumn1> <datatype> (<size>), <newcolumn1> <datatype> (<size>),..);
– Dropping a column from a tableALTER TABLE <tablename> DROP COLUMN <columname>;
– Modifying existing columnsALTER TABLE <tablename> MODIFY (<column1> <newdatatype> (<newsize>));
– Restrictions on the ALTER TABLEChange the name of the tableChange the name of the columnDecrease the size of a column if table data exists9
• Renaming tablesRENAME <tablename> TO <newtablename>;
• Truncating tables: empties a table completely.TRUNCATE TABLE <tablename>;
• Destroying tablesDROP TABLE <tablename>;
10
• Examining objects created by a user– SELECT * FROM TAB;– DESCRIBE <tablename>;