Knowledge in plsql

SQL Aliases

SQL Aliases SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of the query. Alias Column Syntax SELECT column_name AS alias_name FROM table_name; Alias Table Syntax SELECT column_name(s) FROM table_name AS alias_name

SQL FOREIGN KEY Constraint

SQL FOREIGN KEY Constraint A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.

Introduction to SQL

SQL is a standard language for accessing and manipulating databases.What is SQL?SQL stands for Structured Query LanguageSQL lets you access and manipulate databasesSQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987What Can SQL do?SQL can execute queries against a databaseSQL can retrieve data from a databaseSQL can insert records in a databaseSQL can update records in a databaseSQL can delete records from a databaseSQL can create new databasesSQL can create new tables in a databaseSQL can create stored procedures in a databaseSQL can create views in a databaseSQL can set permissions on tables, procedures, and viewsSQL is a Standard - BUT....Although SQL is an ANSI/ISO standard, there are different versions of the SQL language.However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.

RDBMS

RDBMS stands for Relational Database Management System.RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows.Look at the "Customers" table:ExampleSELECT * FROM Customers;

SQL SELECT Statement

SQL SELECT StatementThe SQL SELECT StatementThe SELECT statement is used to select data from a database.The data returned is stored in a result table, called the result-set.SELECT SyntaxSELECT column1, column2, ...FROM table_name;Here, column1, column2, ... are the field names of the table you want to select data from. If you want to select all the fields available in the table, use the following syntax:SELECT * FROM table_name;

SQL SELECT DISTINCT Statement

The SQL SELECT DISTINCT StatementThe SELECT DISTINCT statement is used to return only distinct (different) values.Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.SELECT DISTINCT Syntax SELECT DISTINCT column1, column2, ... FROM table_name; SELECT Example Without DISTINCTThe following SQL statement selects ALL (including the duplicates) values from the "Country" column in the "Customers" table:Example SELECT Country FROM Customers;

SELECT DISTINCT Examples

SELECT DISTINCT ExamplesThe following SQL statement selects only the DISTINCT values from the "Country" column in the "Customers" table:Example SELECT DISTINCT Country FROM Customers; The following SQL statement lists the number of different (distinct) customer countries:Example SELECT COUNT(DISTINCT Country) FROM Customers;

SQL WHERE Clause

The SQL WHERE ClauseThe WHERE clause is used to filter records.The WHERE clause is used to extract only those records that fulfill a specified condition.WHERE Syntax SELECT column1, column2, ... FROM table_name WHERE condition; WHERE Clause ExampleThe following SQL statement selects all the customers from the country "Mexico", in the "Customers" table:Example SELECT * FROM Customers WHERE Country='Mexico'; 

SQL AND, OR and NOT Operators

SQL AND, OR and NOT OperatorsThe WHERE clause can be combined with AND, OR, and NOT operators.The AND and OR operators are used to filter records based on more than one condition:The AND operator displays a record if all the conditions separated by AND are TRUE.The OR operator displays a record if any of the conditions separated by OR is TRUE.The NOT operator displays a record if the condition(s) is NOT TRUE.AND Syntax SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND condition3 ...; OR Syntax SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ...; NOT Syntax SELECT column1, column2, ... FROM table_name WHERE NOT condition;

Example

AND ExampleThe following SQL statement selects all fields from "Customers" where country is "Germany" AND city is "Berlin":Example SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin'; OR ExampleThe following SQL statement selects all fields from "Customers" where city is "Berlin" OR "München":Example SELECT * FROM Customers WHERE City='Berlin' OR City='München'; The following SQL statement selects all fields from "Customers" where country is "Germany" OR "Spain":Example SELECT * FROM Customers WHERE Country='Germany' OR Country='Spain'; NOT ExampleThe following SQL statement selects all fields from "Customers" where country is NOT "Germany":Example SELECT * FROM Customers WHERE NOT Country='Germany'; Combining AND, OR and NOTYou can also combine the AND, OR and NOT operators.The following SQL statement selects all fields from "Customers" where country is "Germany" AND city must be "Berlin" OR "München" (use parenthesis to form complex expressions):Example SELECT * FROM Customers WHERE Country='Germany' AND (City='Berlin' OR City='München'); The following SQL statement selects all fields from "Customers" where country is NOT "Germany" and NOT "USA":Example SELECT * FROM Customers WHERE NOT Country='Germany' AND NOT Country='USA'; 

SQL ORDER BY Keyword

The SQL ORDER BY KeywordThe ORDER BY keyword is used to sort the result-set in ascending or descending order.The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.ORDER BY Syntax SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC; 

SQL INSERT INTO Statement

The SQL INSERT INTO StatementThe INSERT INTO statement is used to insert new records in a table.INSERT INTO SyntaxIt is possible to write the INSERT INTO statement in two ways.The first way specifies both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);