Sunteți pe pagina 1din 6

ALTER TABLE (add column) ALTER TABLE (drop column) AS (alias for column) AS (alias for table) CREATE

DATABASE CREATE INDEX CREATE Sequence syntax: CREATE UNIQUE INDEX CREATE VIEW Data Definition Language (DDL) Statements DDL Delete From Statement Difference between TRUNCATE, DELETE and DROP commands DML DROP DROP DATABASE DROP INDEX DROP SEQUENCE syntax: GROUP BY HAVING Inner Join Order By Outer Join (Left Join, Right Join) Package Scalar functions Sequence SQL - Data Types SQL - Distinct TCL Transaction Control Statements Trigger TRUNCATE Types of SQL Statements Union View What are the difference between DDL, DML and DCL commands?

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. Many DDL statements may cause Oracle Database to recompile or reauthorize schema objects. For information on how Oracle Database recompiles and reauthorizes schema objects and the circumstances under which a DDL statement would cause this, see Oracle Database Concepts. DDL statements are supported by PL/SQL with the use of the DBMS_SQL package. The DDL statements are: ALTER ... (All statements beginning with ALTER)

Types of SQL Statements

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
Data Manipulation Language (DML) Statements

The lists in the following sections provide a functional summary of SQL statements and are divided into these categories: 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

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.

Where
SELECT "column_name" FROM "table_name" WHERE "condition"

And/Or
SELECT "column_name" FROM "table_name" WHERE "simple condition" {[AND|OR] "simple condition"}

In
SELECT "column_name" FROM "table_name" WHERE "column_name" IN ('value1', value2 ...)

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

Between
SELECT "column_name" FROM "table_name" WHERE "column_name" BETWEEN 'value1' AND 'value2'

Like
SELECT "column_name" FROM "table_name" WHERE "column_name" LIKE {PATTERN}

Order By
SELECT "column_name" FROM "table_name" [WHERE "condition"] ORDER BY "column_name" [ASC, DESC]

Count
SELECT COUNT ("column_name") FROM "table_name"

Group By
SELECT "column_name1", SUM("column_name2") FROM "table_name" GROUP BY "column_name1"

Having
SELECT "column_name1", SUM ("column_name2") FROM "table_name" GROUP BY "column_name1" HAVING (arithmetic function condition)

Create Table Statement


CREATE TABLE "table_name" ("column 1" "data_type_for_column_1", "column 2" "data_type_for_column_2 ...)

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. BASIC SQL COMMANDS AND DEFINITIONS Select Statement SELECT "column_name" FROM "table_name" Select * Statement SELECT * FROM table_name

Drop Table Statement


DROP TABLE "table_name"

Truncate Table Statement


TRUNCATE TABLE "table_name"

Insert Into Statement


INSERT INTO "table_name" ("column1", "column2 ...) VALUES ("value1", "value2 ...)

Update Statement
UPDATE "table_name" SET "column_1" = [new value] WHERE {condition}

Distinct
SELECT DISTINCT "column_name" FROM "table_name"

ALTER TABLE (add column)


ALTER TABLE table_name ADD column_name datatype

ALTER TABLE (drop column)


ALTER TABLE table_name DROP COLUMN column_name

AS (alias for column)


SELECT column_name AS column_alias FROM table_name

AS (alias for table)


SELECT column_name FROM table_name AS table_alias

There are several basic types and categories of functions in SQL. The basic types of functions are: Aggregate Functions Scalar functions Aggregate functions Aggregate functions operate against a collection of values, but return a single value. Aggregate functions (like SUM) often need an added GROUP BY functionality.

Delete From Statement


DELETE FROM "table_name" WHERE {condition} DELETE FROM table_name (Note: Deletes the entire table!!)

GROUP BY
GROUP BY was added to SQL because aggregate functions (like SUM) return the aggregate of all column values every time they are called, and without the GROUP BY function it was impossible to find the sum for each individual group of column values. Syntax: SELECT column, SUM(column) FROM table GROUP BY column

CREATE VIEW
CREATE VIEW view_name AS SELECT column_name FROM table_name WHERE condition

CREATE INDEX
CREATE INDEX index_name ON table_name (column_name)

HAVING
Having was added to SQL because the WHERE keyword could not be used against aggregate functions (like SUM), and without having it would be impossible to test for result conditions. Syntax: SELECT column, SUM(column) FROM table GROUP BY column HAVING SUM(column) condition value

CREATE UNIQUE INDEX


CREATE UNIQUE (column_name) INDEX index_name ON table_name

DROP INDEX
DROP INDEX table_name.index_name

Scalar functions CREATE DATABASE


CREATE DATABASE database_name Scalar functions operate against a single value, and return a single value based on the input value.

DROP DATABASE
DROP DATABASE database_name Procedure A procedure is a program that executes one or more statements. Its called as a standalone statement. Syntax: CREATE PROCEDURE NAME (parameter , parameter...) [RETURNS data_type] { statement ... } parameter: parameter_type name data_type opt_default parameter_type: IN | OUT | INOUT opt_default: | DEFAULT literal | := literal Function A function is a program that executes one or more statements and returns a value. Its called with in an expression. Syntax: SELECT function(column) FROM table

Trigger
A trigger is a program whose execution is triggered by some events, usually a SQL operation on a table or column with in a table.

Package
Packages are fundamental building blocks of any well-designed application built in the Oracle PL/SQL. A package contains 2 elements, the specification and body. The specification the user what she can do with the package, what programs can be called and what data structures and user-defined types can be referenced. The package body implements and programs in the package specification, it also can contain private program units.

Inner Join
The INNER JOIN returns all rows from both tables where there is a match. If there are rows in Employees that do not have matches in Orders, those rows will not be listed.

Outer Join (Left Join, Right Join)


Left Join The LEFT JOIN returns all the rows from the first table, even if there are no matches in the second table. If there are rows in Employees that do not have matches in Orders, those rows also will be listed. Right Join The RIGHT JOIN returns all the rows from the second table, even if there are no matches in the first table. If there had been any rows in Orders that did not have matches in Employees, those rows also would have been listed. The SELECT INTO statement is most often used to create backup copies of tables or for archiving records. Syntax: SELECT column_name(s) INTO newtable [IN externaldatabase] FROM source

Syntax: SELECT Company, Order Number FROM Orders ORDER BY Company Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time. For example, you can use cursor to include a list of all user databases and make multiple operations against each database by passing each database name as a variable. Before using cursor, you first must declare the cursor. Once a cursor has been declared, you can open it and fetch from it. You can fetch row by row and make multiple operations on the currently active row in the cursor. When you have finished working with a cursor, you should close cursor and deal locates it to release SQL Server resources.

View
A VIEW is a virtual table based on the result-set of a SELECT statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from a single table. Keep in mind that Views cannot store data (except for Indexed Views); rather they only refer to data present in tables. Syntax: CREATE VIEW <View_Name> AS <SELECT Statement> GO CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition

Sequence
Sequence generator can be used to generate an iterative sequence of values. Sequence generators have a number of uses including the creation of primary keys for a table. The INCREMENT, MINVALUE, MAXVALUE, START, and CACHE values are all optional.

CREATE Sequence syntax:


CREATE SEQUENCE name [ INCREMENT increment_value ] [ MINVALUE minimum_value ] [ MAXVALUE maximum_value ] [ START start_value ] [ CACHE cache_value ] [ CYCLE ]

Union
The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the UNION command all selected columns need to be of the same data type. Note: With UNION, only distinct values are selected. Syntax: SQL Statement 1 UNION SQL Statement 2

DROP SEQUENCE syntax:


DROP SEQUENCE name Index Syntax: CREATE INDEX "INDEX_NAME" ON "TABLE_NAME" (COLUMN_NAME) What are the difference between DDL, DML and DCL commands?

SQL - Distinct
Distinct is used to retrieve rows of your database that have unique values for a given column. For example say we have a table of employees and in this table of employees we have several job titles from janitors to CEOs. We would like to know just how many distinct job titles we have. Syntax: SELECT DISTINCT "column_name" FROM "table_name

DDL
Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples: o CREATE - to create objects in the database o ALTER - alters the structure of the database o DROP - delete objects from the database o TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed o COMMENT - add comments to the data dictionary o RENAME - rename an object

Order By
The ORDER BY keyword is used to sort the result. The ORDER BY clause is used to sort the rows.

DML
Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples: o SELECT - retrieve data from the a database o INSERT - insert data into a table o UPDATE - updates existing data within a table o DELETE - deletes all records from a table, the space for the records remain o MERGE - UPSERT operation (insert or update) o CALL - call a PL/SQL or Java subprogram o EXPLAIN PLAN - explain access path to data o LOCK TABLE - control concurrency DCL Data Control Language (DCL) statements. Some examples: o GRANT - gives user's access privileges to database o REVOKE - withdraw access privileges given with the GRANT command

2.

Truncate is much faster than delete.

Reason: When you type delete all the data get copied into the Rollback Tablespace first. Then delete operation get performed. Thats why when you type rollback after deleting a table, you can get back the data (The system gets it for you from the Rollback Tablespace).All this process takes time. But when you type truncate, it removes data directly without copying it into the Rollback Tablespace. Thats why Truncate is faster than Delete. Once you Truncate you cant get back the data. 3. 4. You cant rollback in truncate but in delete you can rollback. Truncate removes the record permanently. In case of truncate, Trigger doesn't get fired. But in DML commands like delete. Trigger gets fired. You cant use conditions (where clause) in truncate. But in delete you can write conditions using where clause

5.

TCL
Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions. o COMMIT - save work done o SAVEPOINT - identify a point in a transaction to which you can later roll back o ROLLBACK - restore database to original since the last COMMIT o SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use

Drop <table> drops the table, including the table definition and the associated objects (rules, indexes, constraints, triggers, primary key and so on). Obviously once a table is dropped; all the data rows contained in the table are also removed. A truncate <table> command removes all rows from a table. The table structure and all the indexes continue to exist until you issue a drop table command (as discussed above). The rules, defaults and constraints that are bound to the columns remain bound, and triggers remain in effect.

SQL - Data Types


SQL recognizes 4 general types of data. As the database designer you will be selecting which type of data that can be placed in each table column. Before we look at each type of table column we will elaborate on specific data types and how they are handled in SQL. Character Strings - ('Words or numbers') Numbers - (3, 3.423, -17) Booleans - (True / False) Nulls - (empty fields)

Difference between TRUNCATE, DELETE and DROP commands


The delete command is used to remove rows from a table. A where clause can be used to only remove some rows. If no where condition is specified, all rows will be removed. After performing a delete operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire.

TRUNCATE
TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as a DELETE.

DROP
The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back. Drop and truncate are DDL commands, whereas delete is a DML command. Therefore delete operations can be rolled back (undone), while drop and truncate operations cannot be rolled back. 1. Truncate is a DDL command whereas delete is a DML command.

Types of SQL Statements .............................................1 Data Definition Language (DDL) Statements ..............1 Transaction Control Statements ....................................2 ALTER TABLE (add column)......................................2 ALTER TABLE (drop column).....................................3 AS (alias for column).....................................................3 AS (alias for table).........................................................3 CREATE VIEW.............................................................3 CREATE INDEX...........................................................3 CREATE UNIQUE INDEX..........................................3 DROP INDEX...............................................................3 CREATE DATABASE..................................................3 DROP DATABASE.......................................................3 GROUP BY...................................................................3 HAVING........................................................................3 Scalar functions..............................................................3 Trigger............................................................................3

Package..........................................................................3 Inner Join.......................................................................3 Outer Join (Left Join, Right Join)..................................4 View...............................................................................4 Union..............................................................................4 SQL - Distinct................................................................4 Order By.........................................................................4 Sequence........................................................................4 CREATE Sequence syntax: ..........................................4 DROP SEQUENCE syntax:..........................................4 What are the difference between DDL, DML and DCL commands?.....................................................................4 DDL...............................................................................4 DML...............................................................................5 TCL................................................................................5 Difference between TRUNCATE, DELETE and DROP commands......................................................................5 TRUNCATE..................................................................5 DROP.............................................................................5 SQL - Data Types..........................................................5

S-ar putea să vă placă și