Relational Database Management System(RDBMS) Structured Query Language(SQL)
Database A database is an organized collection of data. It is the collection of schemes, tables, queries, reports, views and other objects. Data is stored in a table. A table is a collection of related data held in a structured format within a database. It consists of fields (columns), and rows.
Structured Query Language(SQL) SQL is a special-purpose programming language designed for managing data held in a relational database management system (RDBMS) SQL commands are instructions, which are used to communicate with the database to perform specific tasks, work, functions and queries with data. SQL consists of 1. Data Definition Language 2. Data Manipulation Language 3. Data Control Language
Data Definition Language(DDL) DDL SQL commands are used for creating, modifying, and dropping the structure of database objects. DDL contains the following commands(statements) - CREATE - ALTER - DROP - RENAME - TRUNCATE
CONSTRAINTS SQL constraints are used to specify rules for the data in a table. Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the table is created (inside the ALTER TABLE statement). In SQL, we have the following constraints: NOT NULL - Indicates that a column cannot store NULL value UNIQUE - Ensures that each row for a column must have a unique value PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have a unique identity which helps to find a particular record in a table more easily and quickly FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table CHECK - Ensures that the value in a column meets a specific condition DEFAULT - Specifies a default value for a column
DATA TYPES Each column in a database table is required to have a name and a data type. The data type is a guideline for SQL to understand what type of data is expected inside of each column. Different types of datatypes in SQL are: CHAR VARCHAR2 LONG(2gb) CLOB,NCLOB,BLOB,BFILE NUMBER DATE LONG,LONGRAW ROWID,UROWID
CREATE The CREATE command is used to establish a new database, table, index, or stored procedure. The CREATE statement in SQL creates an object in a relational database management system (RDBMS). The format for creating a table is: CREATE TABLE TableName ( ColumnName1 DataType Constraint, ColumnName2 DataType Constraint, …….., …….. ); Example for creating a table: CREATE TABLE food ( Id number PRIMARY KEY, Food_Name char(20) not null, Manufacturer varchar2(50), Expiry date not null, );
In Oracle db, a table can be created using a subquery Example CREATE TABLE table_name AS SELECT * FROM table2
ALTER ALTER TABLE command is used to modify the definition (structure) of a table by modifying the definition of its columns(Fields). The ALTER command is used to perform the following functions. Add, drop, modify table columns (data types and data sizes) Add and drop constraints Enable and Disable constraints Syntax to add a column ALTER TABLE table_name ADD column_name datatype constraint; Example: 1.ALTER TABLE food ADD quantity number; 2.ALTER TABLE food ADD (quantity number,owner varchar2(20));
Syntax to drop a column ALTER TABLE table_name DROP column_name; Example: ALTER TABLE food DROP owner_name; Syntax to modify a column ALTER TABLE table_name MODIFY column_name Example ALTER TABLE food MODIFY quantity numeric(5);* changing datatype(size) Syntax to RENAME a column ALTER TABLE table_name RENAME COLUMN column_name TO newCN Example: ALTER TABLE food RENAME expiry TO expiry_date;
RENAME The SQL RENAME command is used to change the name of the table or a database object. Syntax to rename a table RENAME table_name TO new_tablename;
TRUNCATE The SQL TRUNCATE TABLE command is used to delete complete data from an existing table but retains the table structure. Syntax: TRUNCATE TABLE table_name; Example TRUNCATE TABLE food;
DROP DROP command is used to delete the table SYNTAX: DROP TABLE table_name; EXAMPLE: DROP TABLE foods;
DATA MANIPULATION LANGUAGE(DDL) A data manipulation language (DML) is a set of commands(queries) used for selecting, inserting, deleting and updating data in a database. DML can modify stored data but not the schema or database objects. DML contains following commands: -INSERT -UPDATE -SELECT -DELETE
INSERT The INSERT Statement is used to add new rows of data to a table. We can insert data to a table in following ways, 1.INSERT INTO table_name (col1,col2,coln) VALUES (val1,val2,valn); 2.INSERT INTO table_name VALUES (VAL1,VAL2,VALN); 3.INSERT INTO table_name VALUES(&val1,&val2,&valn); 4.Using SELECT statement INSERT INTO table_name1 SELECT * FROM table_name2;
UPDATE The UPDATE Statement is used to modify the existing rows in a table. SYNTAX for UPDATE UPDATE table_name SET column_name1=newval1,column_name2=newval2.. [condition]*** Example: UPDATE food SET manufacturer=‘parle’ WHERE serial_no=3;
DELETE This command removes one or more records from a table according to specified conditions. SYNTAX for DELETE: DELETE FROM table_name WHERE condition; Example: DELETE FROM food WHERE serial_no=2; DELETE FROM food WHERE serial_no=2 AND manufacturer=‘itc’; We can also delete rows using sub queries DELETE all the ROWS from a table but retains TABLE structure SYNTAX: DELETE * FROM table_name Example: DELETE * FROM food;
SELECT SQL SELECT statement is used to query or retrieve data from a table in the database. A query may retrieve information from specified columns or from all of the columns in the table. Syntax of SQL SELECT Statement: SELECT * FROM table_name; SELECT column_name FROM table_name [condition]; SELECT query can have optional clauses like WHERE, ORDER BY, GROUP BY, HAVING and AS.
TRANSACTION CONTROL LANGUAGE(TCL) A transaction is the propagation of one or more changes to the database. For example, if you are creating a record or updating a record or deleting a record from the table, then you are performing transaction on the table. It is important to control transactions to ensure data integrity and to handle database errors. Transaction Control: Following commands used to control transactions: COMMIT ROLLBACK SAVEPOINT Transactional control commands are only used with the DML commands INSERT, UPDATE and DELETE only.
COMMIT The COMMIT command is the transactional command used to save changes invoked by a transaction to the database. The COMMIT command saves all transactions to the database since the last COMMIT or ROLLBACK command. SYNTAX: COMMIT; AutoCommit: Transaction is committed automatically when us User closes the rdms.
ROLLBACK The ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database. The ROLLBACK command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued. SYNTAX: ROLLBACK;
SAVEPOINT A SAVEPOINT is a point in a transaction when you can roll the transaction back to a certain point without rolling back the entire transaction. SYNTAX: SAVEPOINT savepoint_name; Example: SAVEPOINT a; RELEASING SAVEPOINT: REALEASE SAVEPOINT savepoint_name;