Structured Query Language (Sql)
Total Page:16
File Type:pdf, Size:1020Kb
STRUCTURED QUERY LANGUAGE (SQL) Structured Query Language is a language that provides an interface to Relational Database Management System.SQL was developed by IBM in 1970s for the use of data in big organisation properly. SQL consists of several commands for communication with Oracle server. SQL is a non-procedural language, which has an English like structure and allow the user to specify what is wanted rather than how it should be done. SQL supports many statements that directly implement the corresponding relational algebra operation.SQL is the Standard Database Language supported by every RDBMS. SQL is used to communicate with a database. According to ANSI (American National Standards Institute), it is the standard language for relational database management systems. SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. Some common relational database management systems that use SQL are: Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc. Although most database systems use SQL, most of them also have their own additional proprietary extensions that are usually only used on their system. However, the standard SQL commands such as "Select", "Insert", "Update", "Delete", "Create", and "Drop" can be used to accomplish almost everything that one needs to do with a database. This tutorial will provide you with the instruction on the basics of each of these commands as well as allow you to put them to practice using the SQL Interpreter. Features of SQL. • It is non-procedural or set oriented language. • Recovery and concurrency. • Integrity constraints. • The security can be maintained by view mechanism. • It is English like language. • Time efficient. Classification of SQL statements. ➢ Data Definition Language(DDL). It is a set of SQL command used to define the data in the data-base. This definition includes all the entity sets and their associated attributes as well as the relationship among the entity sets.(Used to define the objects in a relational database).e.g.CREATE,ALTER and DROP. ➢ Data Manipulation Language(DML). It is a set of SQL commands used to manipulate the data in the data-base. Data Manipualtion involves retrieval of data from data-base, insertion of new data into the data-base and deletion or modification of existing data.(Used for query,addition,deletion and updation of data stored in the database).e.g.SELECT,UPDATE and DELETE. ➢ Data Control Language(DCL). ): It is a set of SQL commands used to control the access of data from data-base.(Used for controlling data stored in the database).e.g.GRANT,REVOKE. ➢ Tranaction Control Language(TCL). It is a set of SQL commands used to control the transaction activities done on the data-base. (Used to save the changes made in the table as well as to ignore the changes).e.g.ROLLBACK,SAVEPOINT and COMMITE. ➢ Data Query Language(DQL). It is a component of SQL statement that allow to get data from data-base..(Used to query data from the table).e.g.SELECT. S.NO SQL COMMANDS DESCRIPTION COMPONENTS CREATE To create the schema object (table). To alter the schema object. ALTER 1. DDL DROP To delete the schema object. RENAME To rename the schema object. INSERT To insert data in the table. UPDATE To update existing data with in a table. To delete all the records from the 2. DML table or to remove rows from the DELETE table. GRANT To give permission and take away parbaligious to access the data. 3. DCL REVOKE To give permission and take away parbaligious to access the data. COMMIT To make a transaction permanent. 4. TCL SAVEPOINT To set a point to which transaction can be rollback. To undo changes in the ROLLBACK transaction. 5. DQL SELECT To retrive data from one or more table. SQL DATATYPES When we create a table, we need to specify the datatype of each column.Datatypes are declared to identify the type of data that will be held or stored for a particular column or variable.The datatypes may be followed by one or more number of parentheses which give information about the column’s width.The table given below list important datatypes. S.NO DATATYPES DESCRIPTION 1. CHAR(255) This data type is used to store character strings value of fixed length. The size in bracket determine the number of characters the cell can hold. The maximum number of characters this data type can hold is 255 characters. 2. VARCHAR/ This data type is used to store the variable length alpha VARCHAR2 numeric data. It is more flexible form than the char data type. (4000) The maximum number of characters it can hold is 4000 characters. 3. This data type is used to represent date and time. The DATE standard format is DD-MM-YY i.e. 22-jan-13. To enter date other than the standard format we can use appropriate function. 4. NUMBER(P,S) This data type is used to store number either fixed or floating (38) points. It can be used to store either zero, positive and negative numbers. The precision (P) determine the maximum length of the data, and the scale (S) determine the number of places to the right of the decimal. If scale is omitted then, by default it is a zero value. The maximum value of precision value is upto 38 digits. 5. LONG(2GB) This data type is used to store variable length character strengths containing upto 2 GB. 6. RAW/LONG This data type are used to store binary data such as digitised RAW(2GB) picture or image. Raw data type can have a maximum length of 255 bytes. Long raw data type can contain upto 2 GB. Commands used in Data Definition Language (DDL) 1. CREATE TABLE Command. We use CREATE TABLE command to create a new database.With this command we can specify a name,type,precision and scale for each field in the data base to be created. Syntax:- CREATE TABLE <table_name> (column_name1 type(size), column_name2 type(size), . column_name n type(size)); Example:-To create a table STUDENT holding the SNO, ROLLNO, NAME, BRANCH, SEM, MARKS the following command has to be writen CREATE TABLE STUDENT (SNO NUMBER(5), ROLLNO NUMBER(10), NAME CHAR(20), BRANCH CHAR(10), SEM VARCHAR2(10), MARKS NUMBER(10)); 2. ALTER TABLE Command. This command is used to change the table.We can add new column,change the datatype of columns or drop any constraints using ALTER TABLE command. • The ADD Clause. Using ADD clause we can add new column into the table. Syntax:- ALTER TABLE<table name> ADD(column_name type(size)); Example:- If we have to add new column for PN_NO into a table STUDENT then we have to write: ALTER TABLE STUDENT ADD(PH_NO NUMBER(10)); • The MODIFY Clause. Use the MODIFY keyword to modify the definition of an existing column. Syntax:- ALTER TABLE<table_name> MODIFY(Column_name type(size)); Exmple:-If we have to change the size of column PN_NO into the table STUDENT,then we have to write: ALTER TABLE STUDENT MODIFY(PH_NO NUMBER(15)); • Dropping a column. Use to remove a column which is no longer required for your table. Syntax:- ALTER TABLE<table_name> DROP COLUMN<column_name>; Example:- If we have to remove the column PN_NO from the table STUDENT,then we have to write: ALTER TABLE STUDENT DROP COLUMN PH_NO; 3.DROP TABLE Command. This command is use to remove the table physically. Syntax:- DROP TABLE<table_name> Example:- If we have to drop the table than,we have to write: DROP TABLE STUDENT; 4.RENAME Command. This command is use to rename the existing table. Syntax:- RENAME<old_tablt_name>TO<new_table_name> Example:-If we have to rename the existing table STUDENT than, we have to write: RENAME STUDENT TO STUDENT1; Commands used in Data Manipulation Language(DML) 1.The INSERT INTO Command. This commands adds a new record to the existing database.The new record includes data specified with the INSERT INTO command. Syntax:- INSERT INTO<table_name> VALUES(<expression1.<expression2>, . <expression_n>); Example:- If we have to enter the new student record into table STUDENT,than we have to write: INSERT INTO STUDENT VALUES(1,1100606,’BHUPI’,’BCA’,4,400) 2.The DELETE Command. This command is use to delete the existing rows of a table. • Delete all the rows from table. It is use to delete all the rows from the table. Syntax:- DELETE from <table_name>; Example:-If we have to remove all the rows from the table STUDENT than,we have to write: DELETE FROM STUDENT; • Delete the specific rows. It is use to delete the specific row from the table. Syntax:- DELETE from<table_name> WHERE<condition>; Example:-If we have to remove specific row from the table STUDENT than,we have to write: DELETE FROM STUDENT WHERE NAME=’ARJUN’; 3.The UDATE Command. This command is used to update the contents of the table that have been added in the past. • To UPDATE all the rows. It is use to update all the rows in the table. Syntax:- UPDATE<table_name> Set,column_name>=<expression/value>; Example:- If we have to update the given rows than,we have to write: UPDATE STUDENT SET BRANCH=’MCA’; • To UPDATE a given rows. It is use to update a given or selected row in the table. Syntax:- UPDATE<table_name> Set,column_name>=<expression/value> WHERE<condition>; Example:- If we have to update a given row than,we have to write: UPDATE STUDENT SET BRANCH=’MCA’ WHERE NAME=’ATINDER’; Commands used in Data Query Language(DQL) 1.The SELECT Command. This command is use to retrieve information from one or more database.