Assignment On: "Structured Query Language"
Assignment On: "Structured Query Language"
Approved by AICTE, Ministry of HRD, Govt. of India Affiliated To Guru Gobind Singh Indraprastha University, Delhi INSTITUTIONAL AREA, MADHUBAN CHOWK, ROHINI, DELHI- 110085 E-Mail: [email protected], Website: www.tecnia.in
SQL
SQL (Structured Query Language) is a database sublanguage for querying and modifying relational databasesSQL (Structured Query Language) is a database sublanguage for querying and modifying relational databasesSQL (Structured Query Language) is a database sublanguage for querying and modifying relational databases. SQL is a database computer language designed for managing data in relational database management systems(RDBMS), and originally based upon relational algebra. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control. SQL was one of the first languages for Edgar F. Codd's relational model in his influential 1970 paper, "A Relational Model of Data for Large Shared Data Banks"] and became the most widely used language for relational databases. CHARACTERISTICS OF SQL SQL experienced and accurate over many years, is the customary way to communicate with a database. It is unanimously understood by the both front ends and back ends. The combination of SQL language and relational database systems is one of the most esteemed achievements of the IT field. Over the last few decades, database management segment has grown-up terrifically and this segment only worth tens of billions of dollars, and SQL stands today as a significant computer technology and an authoritative market force. Some of the primary merits of SQL are briefed below. Standard independent language: The widespread rules of the SQL have been recognized by ANSI and ISO. Therefore the SQL is an open language, involving it is not owned or controlled by a single company. Nowadays, SQL is offered by all the foremost DBMS vendors. SQL is not just for an exacting product; it also works with oracle, Microsoft SQL server and Sybase, and lot more. Almost all the data base vendors may have proprietary extensions to SQL, but the fundamentals of SQL are almost equal across all the data base vendors. Cross-platform abilities: The prevalent adversity of using a programming language to access database is that it infrequently produces an accurate cross platform application. SQL may be moderately innovative to some of the for the most part of popular programming languages like COBOL or C. But it has been in use on dissimilar hardware platforms for years. Majority of the cases, the same SQL statement can be used on a mainframe, desktop and a server. Easy to learn and use: SQL statements look like very simple English sentences that are easy to understand. SQL has been created such that it is instinctive, easy, and maps humans cognitive model. While it is non-procedural in nature, the user host has to type SQL declaration and afterwards it has to be handed over to the DBMS. Then it is executed by the DBMS, internal code and returns a set, which is a logically defined group of data.
Speed: For the past ten years, SQL data engines have been focusing the powerful effort to get better performance. The passionate competition among data base vendors has resulted in quicker, more vigorous database management systems that work at lower costs.
CHARACTER(n) CHARACTER VARYING(n) or VARCHAR(n) BINARY(n) BINARY VARYING(n) or VARBINARY(n) INTEGER(p) SMALLINT INTEGER BIGINT DECIMAL(p, s)
Character string, fixed length n. See Variable length character string, maximum length n. Fixed length binary string, maximum length n. Variable length binary string, maximum length n. Integer numerical, precision p. Integer numerical precision 5. Integer numerical, precision 10. Integer numerical, precision 19. Exact numerical, precision p, scale s. Exact numerical, precision p, scale s. (Same as DECIMAL - see below). Approximate numerical, mantissa precision p. Approximate numerical
1 n 15000
1 n 15000
1 n 15000
1 n 15000 1 p 45 -32768 through 32767 -2,147,483,648 through 2,147,483,647 -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807 1 p 45 0 s p 1 p 45 0 s p 1 p 45 Zero or absolute value 10 to 10
-999 +999
NUMERIC(p, s)
FLOAT(p) REAL
mantissa precision 7. FLOAT DOUBLE PRECISION DATE TIME TIMESTAMP Approximate numerical mantissa precision 16. Approximate numerical mantissa precision 16. Composed of a number of integer fields, represents an absolute point in time, depending on sub-type.
10 to 10
-38 -308
+38
* Refer to DATE, TIME and TIMESTAMP for a complete explanation of this data type.
INTERVAL
Composed of a number of integer fields, represents a * Refer to Interval for a complete period of time, depending on the explanation of this data type. type of interval.
VARCHAR(n)
VARBINARY(n) BINARY VARYING(n) INT(p) INT DEC(p, s) DEC INTEGER(p) INTEGER DECIMAL(p, s) DECIMAL
NUM(p, s) NUM
NUMERIC(p, s) NUMERIC
The DATE or TIMESTAMP data type represents an absolute position on the timeline and the TIME data type represents an absolute time of day. "DATETIME" is a term used to collectively refer to the data types DATE, TIME and TIMESTAMP. A DATETIME contains some or all of the fields YEAR, MONTH, DAY, HOUR, MINUTE and SECOND. These fields always occur in the order listed, which is from the most significant to least significant. Each of the fields is an integer value, except that the SECOND field may have an additional integer component to represent the fractional seconds. For a DATETIME value with a SECOND component, it is possible to specify an optional seconds precision which is the number of significant digits in the fractional part of the SECOND value. This must be a value between 0 and 9. If a seconds precision is not specified, the default is 0 for TIME and 6 for TIMESTAMP. DATE values are represented according to the Gregorian calendar and TIME values are represented according to the 24 hour clock. The inclusive value limits for the DATETIME fields are as follows:
Field Inclusive value limit
YEAR MONT H
0001 to 9999
DAY HOUR
MINUT 00 to 59
E SECON 00 to 59.999999999 D
The three DATETIME data types are: DATE, TIME(s) and TIMESTAMP(s).
DATE
This describes a date using the fields YEAR, MONTH and DAY in the format YYYY-MM-DD. The length is 10.
TIME(s)
This describes a time in an unspecified day, with seconds precision s, using the fields HOUR, MINUTE and SECOND in the format HH:MM:SS[.sF] where F is the fractional part of the SECOND value. If a seconds precision is not specified, s defaults to 0. The length is 8 (or 9+s, if s > 0).
TIMESTAMP(s)
This describes both a date and time, with seconds precision s, using the fields YEAR, MONTH, DAY, HOUR, MINUTE and SECOND in the format YYYY-MM-DD HH:MM:SS[.sF] where F is the fractional part of the SECOND value. If a seconds precision is not specified, s defaults to 6. The length is 26 (or 19, if s = 0 or 20+s, if s > 0).
Data Definition Language (DDL) Statements Data Manipulation Language (DML) Statements Transaction Control Statements Session Control Statements System Control Statement Embedded SQL Statements
Data definition language (DDL) statements let you to perform these tasks:
Create, alter, and drop schema objects Grant and revoke privileges and roles Analyze information on a table, index, or cluster Establish auditing options Add comments to the data dictionary
The CREATE, ALTER, and DROP commands require exclusive access to the specified object. For example, an ALTER TABLE statement fails if another user has an open transaction on the specified table. The GRANT, REVOKE, ANALYZE, AUDIT, and COMMENT commands do not require exclusive access to the specified object. For example, you can analyze a table while other users are updating the table. Oracle Database implicitly commits the current transaction before and after every DDL statement. The DDL statements are:
ALTER ... (All statements beginning with ALTER) ANALYZE ASSOCIATE STATISTICS AUDIT COMMENT CREATE ... (All statements beginning with CREATE) DISASSOCIATE STATISTICS DROP ... (All statements beginning with DROP) FLASHBACK ... (All statements beginning with FLASHBACK) GRANT NOAUDIT
PURGE
RENAME REVOKE TRUNCATE UNDROP
Data manipulation language (DML) statements access and manipulate data in existing schema objects. These statements do not implicitly commit the current transaction. The data manipulation language statements are:
CALL DELETE EXPLAIN PLAN INSERT LOCK TABLE MERGE SELECT UPDATE
The SELECT statement is a limited form of DML statement in that it can only access data in the database. It cannot manipulate data in the database, although it can operate on the accessed data before returning the results of the query. The CALL and EXPLAIN PLAN statements are supported in PL/SQL only when executed dynamically. All other DML statements are fully supported in PL/SQL.
Transaction Control Statements
Transaction control statements manage changes made by DML statements. The transaction control statements are:
COMMIT ROLLBACK SAVEPOINT SET TRANSACTION
All transaction control statements, except certain forms of the COMMIT and ROLLBACK commands, are supported in PL/SQL. For information on the restrictions, see COMMIT and ROLLBACK .
Session Control Statements
Session control statements dynamically manage the properties of a user session. These statements do not implicitly commit the current transaction. PL/SQL does not support session control statements. The session control statements are:
ALTER SESSION SET ROLE
The single system control statement, ALTER SYSTEM, dynamically manages the properties of an Oracle Database instance. This statement does not implicitly commit the current transaction and is not supported in PL/SQL.
Embedded SQL Statements
Embedded SQL statements place DDL, DML, and transaction control statements within a procedural language program. Embedded SQL is supported by the Oracle precompilers
Example:
Successful execution of the above query should yield messages indicating that the queries have run successfully.
SQL Results:
(1 row(s) affected) (1 row(s) affected) (1 row(s) affected) (1 row(s) affected) (1 row(s) affected) (1 row(s) affected) We can double-check the results by running a SELECT (*) query and doing so will retrieve all records SQL has stored inside the inventory table.
SQL Code:
USE mydatabase; SELECT * FROM inventory;
SQL Results:
id product 2 HP Printer 3 Pen 4 Stapler 5 Hanging Files 6 Laptop quantity price 179.99 89.99 0.99 7.99 14.99 499.99 9 78 3 33 16 1 19" LCD Screen 25
In creating this new table with data that relates to data inside the orders table, you have unknowingly created a relational database. We can now take a list of items ordered by our customers and verify that these items are in stock as purchases continue to flow in, so long as we maintain an up-to-date inventory table.
SQL Results:
product quantity 4
This example illustrates how SQL AND combines multiple conditional statements (3 total now) into a single condition with multiple circumstances (filters). Each filter removes rows from the result set that doesn't meet the condition.
SQL - Or
SQL OR also applies logic to help filter results. The difference is that instead of linking together conditional statements, an OR condition just asks SQL to look for 2 separate conditions within the same query and return any records/rows matching either of the conditions.
SQL Or Code:
USE mydatabase; SELECT * FROM orders WHERE product = 'Pen' OR product = '19" LCD Screen';
SQL Results:
id customer 1 Tizag day_of_order product quantity 4 2008-08-01 00:00:00.000 Pen
4 Gerald Garner 2008-08-15 00:00:00.000 19" LCD Screen 3 5 Tizag 2008-07-25 00:00:00.000 19" LCD Screen 3
The first record returned matches the first condition since the product = 'Pen'. The two records after that match the other condition; the product in each of those orders is the '19" LCD Screen'. This type of logic allows the developer to filter results based on one or more conditions.
Select queries require two essential parts. The first part is the "WHAT", which determines what we want SQL to go and fetch. The second part of any SELECT command is the "FROM WHERE". It identifies where to fetch the data from, which may be from a SQL table, a SQL view, or some other SQL data object. Now we would like SQL to go and fetch some data for us from the orders table that was created in the previous lesson. How do we translate this request into SQL code so that the database application does all the work for us? Simple! We just need to tell SQL what we want to select and from where to select the data, by following the schema outlined below.
Below, we will manipulate the result output by rearranging the list of table column names inside of the SELECT statement.
By rearranging the table column list inside the SELECT statement, we altered the appearance of the result set. Also, by not including the id column in the list of table columns, SQL did not fetch any column data for this column because we didn't ask SQL to do so.