Docsity

SQL Oracle Short Book, Schemes and Mind Maps of Database Management Systems (DBMS)

Toggle sidebar
bg1
Table of Content
SQL Overview........................................................................
13
What is SQL?
...........................................................................................13
Why SQL? ................................................................................................ 15
History: ..................................................................................................... 16
SQL Process: ........................................................................................... 16
SQL Commands: ...................................................................................... 17
DDL - Data Definition Language: ............................................................. 17
DML - Data Manipulation Language: ........................................................ 17
DCL - Data Control Language: ................................................................. 17
DQL - Data Query Language: .................................................................. 17
SQL RDBMS Concepts .......................................................... 18
What is field? ............................................................................................ 19
What is record, or row? ............................................................................ 19
What is column? ....................................................................................... 19
What is NULL value?................................................................................ 19
SQL Constraints: ...................................................................................... 20
NOT NULL Constraint: ............................................................................. 20
DEFAULT Constraint:............................................................................... 21
Example: .................................................................................................. 21
Drop Default Constraint: ........................................................................... 21
UNIQUE Constraint: ................................................................................. 21
Example: .................................................................................................. 21
DROP a UNIQUE Constraint: ................................................................... 22
PRIMARY Key: ......................................................................................... 22
Create Primary Key: ................................................................................. 22
Delete Primary Key: ................................................................................. 23
FOREIGN Key: ......................................................................................... 24
Example: .................................................................................................. 24
DROP a FOREIGN KEY Constraint: ........................................................ 25
CHECK Constraint: .................................................................................. 25
Example: .................................................................................................. 25
DROP a CHECK Constraint: .................................................................... 25
INDEX: ..................................................................................................... 26
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download SQL Oracle Short Book and more Schemes and Mind Maps Database Management Systems (DBMS) in PDF only on Docsity!

Table of Content

  • SQL Overview........................................................................
  • What is SQL? ...........................................................................................
  • Why SQL?
  • History:
  • SQL Process:
  • SQL Commands:
  • DDL - Data Definition Language:
  • DML - Data Manipulation Language:
  • DCL - Data Control Language:
  • DQL - Data Query Language:
  • SQL RDBMS Concepts
  • What is field?............................................................................................
  • What is record, or row?
  • What is column?
  • What is NULL value?................................................................................
  • SQL Constraints:
  • NOT NULL Constraint:
  • DEFAULT Constraint:...............................................................................
  • Example:
  • Drop Default Constraint:
  • UNIQUE Constraint:
  • Example:
  • DROP a UNIQUE Constraint:...................................................................
  • PRIMARY Key:
  • Create Primary Key:
  • Delete Primary Key:
  • FOREIGN Key:
  • Example:
  • DROP a FOREIGN KEY Constraint:
  • CHECK Constraint:
  • Example:
  • DROP a CHECK Constraint:
  • INDEX:
  • Example:
  • DROP a INDEX Constraint:......................................................................
  • Data Integrity:
  • Database Normalization
  • Third Rule of 1NF:
  • SQL RDBMS Databases
  • MySQL
  • History:
  • Features:
  • MS SQL Server
  • History:
  • Features:
  • ORACLE
  • History:
  • Features:
  • MS- ACCESS
  • Features:
  • SQL Syntax
  • SQL SELECT Statement:
  • SQL DISTINCT Clause:
  • SQL WHERE Clause:...............................................................................
  • SQL AND/OR Clause:
  • SQL IN Clause:
  • SQL BETWEEN Clause:
  • SQL Like Clause:
  • SQL ORDER BY Clause:
  • SQL GROUP BY Clause:
  • SQL COUNT Clause:
  • SQL HAVING Clause:
  • SQL CREATE TABLE Statement:
  • SQL DROP TABLE Statement:
  • SQL CREATE INDEX Statement:
  • SQL DROP INDEX Statement:
  • SQL DESC Statement:
  • SQL TRUNCATE TABLE Statement:
  • SQL ALTER TABLE Statement:
  • SQL ALTER TABLE Statement (Rename):
  • SQL INSERT INTO Statement:
  • SQL UPDATE Statement:
  • SQL DELETE Statement:
  • SQL CREATE DATABASE Statement:
  • SQL DROP DATABASE Statement:
  • SQL USE Statement:
  • SQL COMMIT Statement:
  • SQL ROLLBACK Statement:....................................................................
  • SQL Data Types
  • Exact Numeric Data Types:
  • Approximate Numeric Data Types:
  • Date and Time Data Types:
  • Character Strings Data Types:
  • Unicode Character Strings Data Types:
  • Binary Data Types:
  • Misc Data Types:
  • SQL Operators
  • What is an Operator in SQL?
  • SQL Arithmetic Operators:
  • SQL Comparison Operators:
  • SQL Logical Operators:
  • SQL Expressions
  • Syntax:
  • SQL - Boolean Expressions:
  • SQL - Numeric Expression:
  • SQL - Date Expressions:
  • SQL CREATE Database
  • Syntax:
  • Example:
  • DROP or DELETE Database
  • Syntax:
  • Example:
  • SQL SELECT Database.........................................................
  • Syntax:
  • Example:
  • SQL CREATE Table
  • Syntax:
  • Syntax:
  • Example:
  • Example:
  • SQL DROP or DELETE Table................................................
  • Syntax:
  • Example:
  • SQL INSERT Query
  • Syntax:
  • Example:
  • Populate one table using another table:
  • SQL SELECT Query
  • Syntax:
  • Example:
  • SQL WHERE Clause
  • Syntax:
  • Example:
  • SQL AND and OR Operators
  • The AND Operator:
  • Syntax:
  • Example:
  • The OR Operator:
  • Syntax:
  • Example:
  • SQL UPDATE Query..............................................................
  • Syntax:
  • Example:
  • SQL DELETE Query
  • Syntax:
  • Example:
  • SQL LIKE Clause
  • Syntax:
  • Example:
  • SQL TOP Clause
  • Syntax:
  • Example:
  • SQL ORDER BY Clause
  • Syntax:
  • Example:
  • SQL Group By........................................................................
  • Syntax:
  • Example:
  • SQL Distinct Keyword
  • Syntax:
  • Example:
  • SQL SORTING Results..........................................................
  • Syntax:
  • Example:
  • SQL Constraints.....................................................................
  • NOT NULL Constraint:
  • DEFAULT Constraint:...............................................................................
  • Example:
  • Drop Default Constraint:
  • UNIQUE Constraint:
  • Example:
  • DROP a UNIQUE Constraint:...................................................................
  • PRIMARY Key:
  • Create Primary Key:
  • Delete Primary Key:
  • FOREIGN Key:
  • Example:
  • DROP a FOREIGN KEY Constraint:
  • CHECK Constraint:
  • Example:
  • DROP a CHECK Constraint:
  • INDEX:
  • Example:
  • DROP a INDEX Constraint:......................................................................
  • Dropping Constraints:...............................................................................
  • Integrity Constraints:
  • SQL Joins
  • SQL Join Types:
  • INNER JOIN
  • Syntax:
  • Example:
  • LEFT JOIN
  • Syntax:
  • Example:
  • RIGHT JOIN
  • Syntax:
  • Example:
  • FULL JOIN
  • Syntax:
  • Example:
  • SELF JOIN
  • Syntax:
  • Example:
  • CARTESIAN JOIN
  • Syntax:
  • Example:
  • SQL Unions Clause
  • Syntax:
  • Example:
  • The UNION ALL Clause:
  • Syntax:
  • Example:
  • Example:
  • SQL NULL Values
  • Syntax:
  • Example:
  • SQL Alias Syntax
  • Syntax:
  • Example:
  • SQL Indexes
  • The CREATE INDEX Command:
  • Single-Column Indexes:
  • Unique Indexes:
  • Composite Indexes:
  • Implicit Indexes:
  • The DROP INDEX Command:
  • When should indexes be avoided?
  • SQL ALTER TABLE Command............................................
  • Syntax:
  • Example:
  • SQL TRUNCATE TABLE
  • Syntax:
  • Example:
  • SQL - Using Views
  • Creating Views:
  • Example:
  • The WITH CHECK OPTION:..................................................................
  • Updating a View:
  • Inserting Rows into a View:
  • Deleting Rows into a View:
  • Dropping Views:
  • SQL HAVING CLAUSE
  • Syntax:
  • Example:

SQL Overview

SQL^ tutorial^ gives^ unique learning on^ Structured Query Language^ and it helps to make practice on SQL

commands which provides immediate results. SQL is a language of database, it includes database creation, deletion, fetching rows and modifying rows etc. SQL is an ANSI (American National Standards Institute) standard, but there are many different versions of the SQL language.

What is SQL?

SQL is Structured Query Language, which is a computer language for storing, manipulating and retrieving data stored in relational database. SQL is the standard language for Relation Database System. All relational database management systems like MySQL, MS Access, Oracle, Sybase, Informix, postgres and SQL Server use SQL as standard database language. Also, they are using different dialects, such as:  MS SQL Server using T-SQL,  Oracle using PL/SQL,  MS Access version of SQL is called JET SQL (native format) etc.

Why SQL?

 Allows users to access data in relational database management systems.  Allows users to describe the data.  Allows users to define the data in database and manipulate that data.  Allows to embed within other languages using SQL modules, libraries & pre-compilers.  Allows users to create and drop databases and tables.

CHAPTER

SQL Commands:

The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be classified into groups based on their nature:

DDL - Data Definition Language:

Command Description CREATE Creates a new table, a view of a table, or other object in database ALTER Modifies an existing database object, such as a table. DROP Deletes an entire table, a view of a table or other object in the database.

DML - Data Manipulation Language:

Command Description INSERT Creates a record UPDATE Modifies records DELETE Deletes records

DCL - Data Control Language:

Command Description GRANT Gives a privilege to user REVOKE Takes back privileges granted from user

DQL - Data Query Language:

Command Description SELECT Retrieves certain records from one or more tables

SQL RDBMS Concepts

What is RDBMS?

RDBMS^ stands for^ R elational^ D atabase^ M anagement^ S ystem. RDBMS is the basis for SQL^ and for all

modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. A Relational database management system (RDBMS) is a database management system (DBMS) that is based on the relational model as introduced by E. F. Codd.

What is table?

The data 0 in RDBMS is stored in database objects called tables. The table is a collection of related data entries and it consists of columns and rows. Remember, a table is the most common and simplest form of data storage in a relational database. Following is the example of a CUSTOMERS table: +----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+

CHAPTER

SQL Constraints:

Constraints are the rules enforced on data columns on table. These are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the database. Constraints could be column level or table level. Column level constraints are applied only to one column, whereas table level constraints are applied to the whole table. Following are commonly used constraints available in SQL:  NOT NULL Constraint: Ensures that a column cannot have NULL value.  DEFAULT Constraint: Provides a default value for a column when none is specified.  UNIQUE Constraint: Ensures that all values in a column are different.  PRIMARY Key: Uniquely identified each rows/records in a database table.  FOREIGN Key: Uniquely identified a rows/records in any another database table.  CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy certain conditions.  INDEX: Use to create and retrieve data from the database very quickly.

NOT NULL Constraint:

By default, a column can hold NULL values. If you do not want a column to have a NULL value, then you need to define such constraint on this column specifying that NULL is now not allowed for that column. A NULL is not the same as no data, rather, it represents unknown data. Example: For example, the following SQL creates a new table called CUSTOMERS and adds five columns, three of which, ID and NAME and AGE, specify not to accept NULLs: CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) ); If CUSTOMERS table has already been created, then to add a NOT NULL constraint to SALARY column in Oracle and MySQL, you would write a statement similar to the following: ALTER TABLE CUSTOMERS MODIFY SALARY DECIMAL (18, 2) NOT NULL;

DEFAULT Constraint:

The DEFAULT constraint provides a default value to a column when the INSERT INTO statement does not provide a specific value. Example: For example, the following SQL creates a new table called CUSTOMERS and adds five columns. Here, SALARY column is set to 5000.00 by default, so in case INSERT INTO statement does not provide a value for this column. then by default this column would be set to 5000.00. CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2) DEFAULT 5000.00, PRIMARY KEY (ID) ); If CUSTOMERS table has already been created, then to add a DFAULT constraint to SALARY column, you would write a statement similar to the following: ALTER TABLE CUSTOMERS MODIFY SALARY DECIMAL (18, 2) DEFAULT 5000.00; Drop Default Constraint: To drop a DEFAULT constraint, use the following SQL: ALTER TABLE CUSTOMERS ALTER COLUMN SALARY DROP DEFAULT;

UNIQUE Constraint:

The UNIQUE Constraint prevents two records from having identical values in a particular column. In the CUSTOMERS table, for example, you might want to prevent two or more people from having identical age. Example: For example, the following SQL creates a new table called CUSTOMERS and adds five columns. Here, AGE column is set to UNIQUE, so that you can not have two records with same age: CREATE TABLE CUSTOMERS( ID INT NOT NULL,

CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) ); To create a PRIMARY KEY constraint on the "ID" column when CUSTOMERS table already exists, use the following SQL syntax: ALTER TABLE CUSTOMER ADD PRIMARY KEY (ID); NOTE: If you use the ALTER TABLE statement to add a primary key, the primary key column(s) must already have been declared to not contain NULL values (when the table was first created). For defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax: CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID, NAME) ); To create a PRIMARY KEY constraint on the "ID" and "NAMES" columns when CUSTOMERS table already exists, use the following SQL syntax: ALTER TABLE CUSTOMERS ADD CONSTRAINT PK_CUSTID PRIMARY KEY (ID, NAME); Delete Primary Key: You can clear the primary key constraints from the table, Use Syntax: ALTER TABLE CUSTOMERS DROP PRIMARY KEY ;

FOREIGN Key:

A foreign key is a key used to link two tables together. This is sometimes called a referencing key. Foreign Key is a column or a combination of columns whose values match a Primary Key in a different table. The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign Key in the second table. If a table has a primary key defined on any field(s), then you can not have two records having the same value of that field(s).

Example:

Example:

Consider the structure of the two tables as follows: CUSTOMERS table: CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) ); ORDERS table: CREATE TABLE ORDERS ( ID INT NOT NULL, DATE DATETIME, CUSTOMER_ID INT references CUSTOMERS(ID), AMOUNT double, PRIMARY KEY (ID) ); If ORDERS table has already been created, and the foreign key has not yet been set, use the syntax for specifying a foreign key by altering a table. ALTER TABLE ORDERS ADD FOREIGN KEY (Customer_ID) REFERENCES CUSTOMERS (ID);

INDEX:

The INDEX is used to create and retrieve data from the database very quickly. Index can be created by using single or group of columns in a table. When index is created, it is assigned a ROWID for each row before it sorts out the data. Proper indexes are good for performance in large databases, but you need to be careful while creating index. Selection of fields depends on what you are using in your SQL queries. Example: For example, the following SQL creates a new table called CUSTOMERS and adds five columns: CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) ); Now, you can create index on single or multiple columns using the following syntax: CREATE INDEX index_name ON table_name ( column1, column2.....); To create an INDEX on AGE column, to optimize the search on customers for a particular age, following is the SQL syntax: CREATE INDEX idx_age ON CUSTOMERS ( AGE ); DROP an INDEX Constraint: To drop an INDEX constraint, use the following SQL: ALTER TABLE CUSTOMERS DROP INDEX idx_age;

Data Integrity:

The following categories of the data integrity exist with each RDBMS:  Entity Integrity : There are no duplicate rows in a table.

Domain Integrity : Enforces valid entries for a given column by restricting the type, the format, or the range of values.  Referential Integrity : Rows cannot be deleted which are used by other records.  User-Defined Integrity : Enforces some specific business rules that do not fall into entity, domain, or referential integrity.

Database Normalization

Database normalization is the process of efficiently organizing data in a database. There are two reasons of the normalization process:  Eliminating redundant data, for example, storing the same data in more than one table.  Ensuring data dependencies make sense. Both of these are worthy goals as they reduce the amount of space a database consumes and ensure that data is logically stored. Normalization consists of a series of guidelines that help guide you in creating a good database structure. Normalization guidelines are divided into normal forms; think of form as the format or the way a database structure is laid out. The aim of normal forms is to organize the database structure so that it complies with the rules of first normal form, then second normal form, and finally third normal form. It's your choice to take it further and go to fourth normal form, fifth normal form, and so on, but generally speaking, third normal form is enough.  First Normal Form (1NF)  Second Normal Form (2NF)  Third Normal Form (3NF)

First Normal Form

First normal form (1NF) sets the very basic rules for an organized database:  Define the data items required, because they become the columns in a table. Place related data items in a table.  Ensure that there are no repeating groups of data.  Ensure that there is a primary key. First Rule of 1NF: You must define the data items. This means looking at the data to be stored, organizing the data into columns, defining what type of data each column contains, and finally putting related columns into their own table. For example, you put all the columns relating to locations of meetings in the Location table, those relating to members in the MemberDetails table, and so on. Second Rule of 1NF: The next step is ensuring that there are no repeating groups of data. Consider we have the following table: