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

Assignment On: "Structured Query Language"

This document provides a summary of Structured Query Language (SQL) in 3 paragraphs. It describes SQL as a standard language for querying and modifying data in relational database management systems. It then lists the main characteristics of SQL, including that it is a standard independent language, has cross-platform abilities, and is easy to learn and use. The final paragraph discusses the different data types that can be used in SQL statements such as character, integer, decimal, date, and time.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Assignment On: "Structured Query Language"

This document provides a summary of Structured Query Language (SQL) in 3 paragraphs. It describes SQL as a standard language for querying and modifying data in relational database management systems. It then lists the main characteristics of SQL, including that it is a standard independent language, has cross-platform abilities, and is easy to learn and use. The final paragraph discusses the different data types that can be used in SQL statements such as character, integer, decimal, date, and time.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

Assignment On

STRUCTURED QUERY LANGUAGE

Submitted by: Ravi Kumar Bhardwaj Enroll. No. 09417003910

TECNIA INSTITUTE OF ADVANCED STUDIES

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.

Data Types in SQL Statements


Explicit data type references are made in SQL statements in the creation of domains and base tables and in the alteration of table definitions. The permissible data types and their ranges are:
Data type Description Range

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

Zero or absolute value

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

Zero or absolute value 10 to 10


+308

Zero or absolute value 10 to 10


-308 +308

* 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.

Data Type Abbreviations

The following abbreviations are accepted for data type definitions:


Abbreviation Character

CHAR(n) CHAR CHAR VARYING(n)

CHARACTER(n) CHARACTER CHARACTER VARYING(n) CHARACTER VARYING(n)

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

DATE, TIME and TIMESTAMP

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

01 to 12 01 to 31 (upper limit further constrained by MONTH and YEAR) 00 to 23

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).

Types of SQL Statements


The tables in the following sections provide a functional summary of SQL statements and are divided into these categories:

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

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

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

System Control Statement

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:

SQL Create Table Code


To accomplish this, it is best to first take a look at the entire CREATE TABLE query and then review each line individually.

SQL Create Table Code:


USE mydatabase; CREATE TABLE inventory ( id INT IDENTITY(1,1) PRIMARY KEY, product VARCHAR(50) UNIQUE, quantity INT, price DECIMAL(18,2) );

SQL Insert Into:


USE mydatabase; INSERT INTO inventory VALUES('19" LCD Screen','25','179.99'); INSERT INTO inventory VALUES('HP Printer','9','89.99'); INSERT INTO inventory VALUES('Pen','78','0.99'); INSERT INTO inventory VALUES('Stapler','3','7.99'); INSERT INTO inventory VALUES('Hanging Files','33','14.99'); INSERT INTO inventory VALUES('Laptop','16','499.99');

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 And Code:


USE mydatabase; SELECT * FROM orders WHERE customer = 'Tizag' AND day_of_order = '08/01/08' AND product = 'Pen';

SQL Results:

id customer day_of_order 1 Tizag

product quantity 4

2008-08-01 00:00:00.000 Pen

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.

SQL Select Query Template:


SELECT table_column1, table_column2, table_column3 FROM my_table;

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.

SQL Select Query Code:


USE mydatabase; SELECT id, customer, day_of_order, product, quantity FROM orders;

SQL Orders Table Results:


id customer day_of_order 1 Tizag product quantity 4 2008-08-01 00:00:00.000 Pen

Below, we will manipulate the result output by rearranging the list of table column names inside of the SELECT statement.

SQL Select Query: Rearranged:


USE mydatabase; SELECT day_of_order, customer, product, quantity FROM orders;

SQL Orders Table Results:


day_of_order customer product quantity Pen 4 2008-08-01 00:00:00.000 Tizag

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.

SQL - Select All (*)


"SELECT (*)" is a shortcut that can be used to select all table columns rather than listing each of them by name. Unfortunately, going this route doesn't allow for you to alter the presentation of the results.

SQL Select All Query:


USE mydatabase; SELECT * FROM orders;

SQL Orders Table Results:


id customer day_of_order 1 Tizag product quantity 4 2008-08-01 00:00:00.000 Pen

You might also like