LAB MANUAL

Joshua 1:9 Be strong and courageous. Do not be terrified; do not be discouraged, for the Lord your God will be with you wherever you go.

Introduction to SQL The Structured Query Language (SQL) is a database language, pure and simple. SQL is a language with its own syntax and grammar has a number of commands for managing the database structure, powerful functions, many data types, and other useful features. The SQL statements are used to communicate with Oracle Database.

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.

DML (Data Manipulation Language ) It is used to retrieve, store, modify, delete, insert and update data in database. Examples: SELECT, UPDATE, INSERT , DELETE statements DDL (Data Definition Language) It is used to create and modify the structure of database objects in database. Examples: CREATE, ALTER, DROP, TRUNCATE, RENAME statements DCL (Data Control Language) It is used to create roles, permissions, and referential integrity as well it is used to control access to database by securing it. Examples: GRANT, REVOKE statements TCL (Transactional Control Language) Copyright © 2012 College of Engineering, Anna University, Chennai – 600 025 Phone: 9445700859/ 22358814

It is used to manage different transactions occurring within a database. It is used to manage the changes made by DML statements. Examples: COMMIT, ROLLBACK statements

Week I
(Practice the environment with simple table creation)

Follow the instructions that will enable you to login and connect to a database. To start with go to Start > All Programs > Oracle-OraDb10g-home>Application Development> SQL Plus You can see this screen

Enter the User Name: it3j[1---70] (the order in the roll no) Password: it Host String: ora2012a After successful login you can work with the SQL Prompt

Copyright © 2012 College of Engineering, Anna University, Chennai – 600 025 Phone: 9445700859/ 22358814

Anna University. column_name3 data_type [constraint]. date Stores year.Practice the following statements SQL CREATE TABLE Syntax CREATE TABLE table_name ( column_name1 data_type [constraint]. int /integer /smallint is equivalent to NUMBER(38) Copyright © 2012 College of Engineering. ) [ ] = optional Note: Today you don’t want to work with constraints SQL Data types char(size) Fixed-length character string.d) Number value with a maximum number of digits of "size" total. month.. column_name2 data_type [constraint]. with a maximum number of "d" digits to the right of the decimal. number(size) Number value with a max number of column digits specified in parenthesis. Max 255 bytes. . Max size is specified in parenthesis. number(size. Chennai – 600 025 Phone: 9445700859/ 22358814 . varchar(size) Variable-length character string. Size is specified in parenthesis.. and day values.d) / decimal(p..

. 'm'. value2. &cgpa) new 1: insert into student values ( 1.) INSERT INTO table_name (column1. &gender.8 old 1: insert into student values ( &sid. Copyright © 2012 College of Engineering. value2. &age. &sname.8) 1 row created. double precision are floating-point and double-precision floating-point (machine dependent) time Stores hour:minute:second timestamp Has both DATE and TIME components To VIEW the relation schema DESC table_name. &sname.. &cgpa) new 1: insert into student values ( 2. 'it'. &cgpa). SQL> insert into student values ( &sid. Chennai – 600 025 Phone: 9445700859/ 22358814 . &cgpa). SQL> / Enter value for sid: 2 Enter value for sname: 'akila' Enter value for gender: 'f' Enter value for age: 20 Enter value for branch: 'it' Enter value for cgpa: 9.. value3. Enter value for sid: 1 Enter value for sname: 'anand' Enter value for gender: 'm' Enter value for age: 20 Enter value for branch: 'it' Enter value for cgpa: 9 old 1: insert into student values ( &sid. &branch.) INSERT INTO student VALUES ( &sid.. 9. &sname. &branch. 'anand'.float(N) is floating-point with at least N digits real.. 20.. &branch. 'it'.. Anna University. 'akila'. &age. &gender. &age. column3. 9) 1 row created. &branch. &age. value3. 20. 'f'.. &sname. column2. &gender.. &gender. The INSERT INTO Statement INSERT INTO table_name VALUES (value1.) VALUES (value1.

Anna University. CREATE TABLE Crsenrl (courseid CHAR (6). CREATE TABLE Course (courseid CHAR(6). dept CHAR (3). cname VARCHAR(20). Modifying Relation Schemas SQL ALTER TABLE Syntax The SQL ALTER TABLE command is used to modify the definition (structure) of a table by modifying the definition of its columns. maxenrl SMALLINT. rank CHAR (4). sname VARCHAR(20). CRSENRL CREATE TABLE Student (sid NUMBER(10). grade CHAR (1)). fid NUMBER(6) ). Creating the Tables STUDENT. branch CHAR(3) . age INTEGER. FACULTY. cgpa REAL). salary REAL). Copyright © 2012 College of Engineering. CREATE TABLE Faculty (fid NUMBER (6). COURSE. gender CHAR(1). fname VARCHAR (20). credit INT . The ALTER command is used to perform the following functions. sid NUMBER (10). Chennai – 600 025 Phone: 9445700859/ 22358814 .Simple SELECT statement SELECT * [column_list] FROM table-name.

Column cannot be dropped. Conditions to remember while altering the structure of the table. modify table columns 2) Add and drop constraints 3) Enable and Disable constraints To add a column This can add a new column to the table. Chennai – 600 025 Phone: 9445700859/ 22358814 .---------------------------COURSEID NOT NULL CHAR(6) CNAME VARCHAR2(20) CREDIT NUMBER(38) MAXENRL NUMBER(38) FID NUMBER(6) SQL> alter table course modify credit number(1).-------. SQL> desc course. Size cannot be decreased if data is already inserted. Anna University. 2. Name Null? Type ----------------------------------------. Column name and table name cannot be changed. Copyright © 2012 College of Engineering. 3.-------. 1. ALTER TABLE Students ADD phoneno NUMBER(10). To modify a column This can modify the data type and size of the column. ALTER TABLE table_name ADD column_name datatype. Table altered. Name Null? Type ----------------------------------------. drop. SQL> desc course.1) Add. ALTER TABLE table_name MODIFY column_name datatype.---------------------------COURSEID NOT NULL CHAR(6) CNAME VARCHAR2(20) CREDIT NUMBER(1) MAXENRL NUMBER(38) FID NUMBER(6) SQL> SQL> alter table course modify cname varchar2(15). Table altered.

Syntax to define a Primary key at table level: [CONSTRAINT constraint_name] PRIMARY KEY (column_name1. CREATE TABLE Faculty (fid NUMBER (6) PRIMARY KEY. cgpa REAL. Copyright © 2012 College of Engineering. Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement). Below are the lists of constraints:       NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK DEFAULT SQL Primary key: This constraint defines a column or combination of columns which uniquely identifies each row in the table. CREATE TABLE Crsenrl (courseid CHAR (6). branch CHAR(3) . Syntax to define a Primary key at column level: column name datatype [CONSTRAINT constraint_name] PRIMARY KEY CREATE TABLE Student (sid NUMBER(10) CONSTRAINT Stu_PKey PRIMARY KEY. Anna University. sname VARCHAR(20) NOT NULL. maxenrl SMALLINT. CONSTRAINT Stu_PKey PRIMARY KEY (sid) ). Chennai – 600 025 Phone: 9445700859/ 22358814 .credit INT .Week II (Practice table creation with constraints) SQL Constraints Constraints are used to limit the type of data that can go into a table. fid NUMBER(6) ). branch CHAR(3) .) CREATE TABLE Student (sid NUMBER(10). cgpa REAL ). fname VARCHAR (20).age INTEGER. salary REAL). gender CHAR(1).sid NUMBER(10). dept CHAR (3).column_name2. age INTEGER... gender CHAR(1).cname VARCHAR(20). sname VARCHAR(20). rank CHAR (4). CREATE TABLE Course (courseid CHAR(6) PRIMARY KEY. grade CHAR (1). PRIMARY KEY( courseid. sid)).

Syntax to define a Foreign key at table level: [CONSTRAINT constraint_name] FOREIGN KEY(column_name) REFERENCES referenced_table_name(column_name). Syntax to define a Foreign key at column level: [CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name) CREATE TABLE Course (courseid CHAR(6) PRIMARY KEY. cname VARCHAR(20). ). FOREIGN KEY (fid) REFERENCES Faculty (fid). use ALTER statement ALTER TABLE Student ADD PRIMARY KEY (sid) ALTER TABLE Student ADD CONSTRAINT Stu_PKey PRIMARY KEY (sid) ALTER TABLE Student DROP CONSTRAINT Stu_PKey SQL Foreign key or Referential Integrity: This constraint identifies any column referencing the PRIMARY KEY in another table. One or more columns can be defined as Foreign key. CREATE TABLE Course (courseid CHAR(6) PRIMARY KEY. sid)). Anna University. credit INT . For a column to be defined as a Foreign Key. CREATE TABLE Crsenrl (courseid CHAR (6). fid NUMBER(6). cname VARCHAR(20). maxenrl SMALLINT.To alter already created table. It establishes a relationship between two columns in the same table or between different tables. fid NUMBER(6) REFERENCES Faculty (fid) ). Copyright © 2012 College of Engineering. maxenrl SMALLINT. credit INT . grade CHAR (1). it should be a defined as a Primary Key in the table which it is referring. PRIMARY KEY( courseid. Chennai – 600 025 Phone: 9445700859/ 22358814 . sid NUMBER (10) CONSTRAINT Stu_FKey REFERENCES Student (sid). CREATE TABLE Crsenrl (courseid CHAR (6) CONSTRAINT Cou_FKey REFERENCES Course (courseid).

CONSTRAINT Stu_FKey FOREIGN KEY (sid) REFERENCES Student (sid) ). PRIMARY KEY( courseid. CONSTRAINT Cou_FKey FOREIGN KEY (courseid) REFERENCES Course (courseid) ON DELETE CASCADE . CASCADE.sid NUMBER (10). CONSTRAINT Stu_FKey FOREIGN KEY (sid) REFERENCES Student (sid) ON DELETE CASCADE ). Copyright © 2012 College of Engineering. sid). use ALTER statement ALTER TABLE Course ADD FOREIGN KEY (fid) REFERENCES Faculty (fid) ALTER TABLE Course ADD CONSTRAINT Fac_FKey FOREIGN KEY (fid) REFERENCES Faculty (fid) ALTER TABLE Course DROP CONSTRAINT Fac_FKey Referential Integrity Options: We can specify RESTRICT. To alter already created table. Chennai – 600 025 Phone: 9445700859/ 22358814 . sid NUMBER (10). grade CHAR (1). PRIMARY KEY( courseid. sid). SET NULL or SET DEFAULT on referential integrity constraints (foreign keys) Oracle allows the following possibilities • NO ACTION ON DELETE NO ACTION left blank (deletion/update rejected) • SET TO NULL: set child tuples to NULL ON DELETE SET NULL • CASCADE: propagate deletion/update DELETE: delete child tuples ON DELETE CASCADE UPDATE: set child tuples to updated values ON UPDATE CASCADE CREATE TABLE Crsenrl (courseid CHAR (6). Anna University. CONSTRAINT Cou_FKey FOREIGN KEY (courseid) REFERENCES Course (courseid). grade CHAR (1).

ON DELETE SET NULL action allows data that references the parent key to be deleted.age INTEGER. cname VARCHAR(20) UNIQUE . maxenrl SMALLINT. cname VARCHAR(20). gender CHAR(1) NOT NULL . All the rows must satisfy this rule. Syntax to define a Unique key at table level: [CONSTRAINT constraint_name] UNIQUE(column_name) CREATE TABLE Course (courseid CHAR(6).ON DELETE CASCADE to delete all child rows when a parent row is deleted. sname VARCHAR(20) NOT NULL. Syntax to define a Not Null constraint: [CONSTRAINT constraint name] NOT NULL CREATE TABLE Student (sid NUMBER(10) PRIMARY KEY. Anna University. Syntax to define a Unique key at column level: [CONSTRAINT constraint_name] UNIQUE CREATE TABLE Course (courseid CHAR(6). fid NUMBER(6). but not updated. maxenrl SMALLINT. Which means a null value is not allowed. fid NUMBER(6) ). UNIQUE (cname) ). SQL Unique Key: This constraint ensures that a column or a group of columns in each row have a distinct value. cgpa REAL ). all rows in the child table that depend on those parent key values have their foreign keys set to null. ON DELETE NO ACTION (which is the default) prevents deleting a parent when there are children (would be nice arrangement for real life) SQL Not Null Constraint: This constraint ensures all rows in the table contain a definite value for the column which is specified as not null. branch CHAR(3) . Syntax to define a Check constraint at column level: [CONSTRAINT constraint_name] CHECK (condition) Copyright © 2012 College of Engineering. When referenced data in the parent key is deleted. The constraint can be applied for a single column or a group of columns. Chennai – 600 025 Phone: 9445700859/ 22358814 . SQL Check Constraint: This constraint defines a business rule on a column. credit INT . A column(s) can have a null value but the values cannot be duplicated. credit INT .

'F')) ALTER TABLE student ADD CHECK(branch IN ('cse' . cgpa REAL. maxenrl SMALLINT. Syntax to define a Check Constraint at table level: [CONSTRAINT constraint_name] CHECK (condition) CREATE TABLE Student (sid NUMBER(10). fid NUMBER(6) ).CREATE TABLE Student (sid NUMBER(10). sname VARCHAR(20). branch CHAR(3) CHECK (branch IN (‘CSE’ . age INTEGER. cname VARCHAR(20) UNIQUE .'F')).'F')) ALTER TABLE Student ADD CONSTRAINT chk_gen CHECK (gender IN ('M'. gender CHAR(1) CHECK (gender IN ('M'. ALTER TABLE Student DROP CONSTRAINT chk_gen SQL Default Constraint: The DEFAULT constraint is used to insert a default value into a column. To alter already created table. Anna University. sname VARCHAR(20). credit INT DEFAULT 3. The default value will be added to all new records. use ALTER statement ALTER TABLE Student ADD CHECK (gender IN ('M'. ALTER TABLE Course MODIFY credit DEFAULT 3 ALTER TABLE Course ALTER COLUMN credit DROP DEFAULT Copyright © 2012 College of Engineering. branch CHAR(3). 'it'. 'eee') ). gender CHAR(1).’EEE’)). ‘ECE’.'F'))). CREATE TABLE Course (courseid CHAR(6). age INTEGER. if no other value is specified. cgpa REAL). 'ece'. CONSTRAINT chk_gen CHECK (gender IN ('M'. Chennai – 600 025 Phone: 9445700859/ 22358814 . ‘IT’.

SELECT * FROM Student. (OR) SELECT DISTINCT cname FROM Course. Display the different courses enrolled by students SELECT DISTINCT courseid FROM Crsenrl. branch. Chennai – 600 025 Phone: 9445700859/ 22358814 .Week III (Practice simple queries using SELECT statement) SQL SELECT Statement SELECT [DISTINCT] FROM table-name [WHERE Clause] [GROUP BY clause] [HAVING clause] [ORDER BY clause]. Retrieve the different branches available in the college. SELECT sname FROM Student. SELECT sname. SELECT column_name(s) FROM table_name SQL is not case sensitive. Copyright © 2012 College of Engineering. branch and cgpa of all students. Retrieve the names . Display the contents of student table. Anna University. * column_list} Retrieve the names of all students. SELECT DISTINCT branch FROM Student. cgpa FROM Student. SELECT is the same as select. SELECT DISTINCT column_name(s) FROM table_name SQL uses single quotes around text values (most database systems will also accept double quotes).

Logical Operators SELECT column1. SELECT sname. The AND & OR Operators The AND operator displays a record if both the first condition and the second condition is true. column2.AND [conditionN]. SELECT column_name(s) FROM table_name WHERE column_name operator value Operators Allowed in the WHERE Clause Comparison Operators Comparison operators are used to compare the column data with specific values in a condition.columnN FROM table_name WHERE CONDITION-1 {AND|OR} CONDITION-2. SELECT sname FROM Student WHERE branch = ‘IT’..WHERE Clause The where clause (optional) specifies which data values or rows will be returned or displayed. cgpa FROM Student WHERE cgpa > 8.. = Equal <> Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal Retrieve the names of Information Technology students. Copyright © 2012 College of Engineering. based on the criteria described after the keyword where.. SELECT column1. Chennai – 600 025 Phone: 9445700859/ 22358814 . column2. Select names and cgpa’s of students whose cgpa are more than 8. columnN FROM table_name WHERE [condition1] AND [condition2]. Anna University...

The OR operator displays a record if either the first condition or the second condition is true. ‘ECE’ .value2. Retrieve the names and branch of all students who are not in IT or in ECE. BETWEEN Between an inclusive range LIKE Search for a pattern IN To specify multiple possible values for a column IS NULL column value does not exist. SELECT sname.OR [conditionN] We can also combine AND and OR (use parenthesis to form complex expressions). branch FROM Student WHERE branch IN (‘CSE’ . Anna University..) List the names and branches of students who are either in CSE or in ECE or in EEE. SELECT column1.. cgpa FROM Student WHERE (branch = ‘IT’) OR (cgpa > 8). SELECT sname.. branch and cgpa of students who are either in IT branch or have cgpa more than 8. Copyright © 2012 College of Engineering. columnN FROM table_name WHERE [condition1] OR [condition2]. branch FROM Student WHERE (branch <> ‘IT’) AND (branch <> ‘ECE’).. In some versions of SQL the <> operator may be written as!= SQL IN Syntax SELECT column_name(s) FROM table_name WHERE column_name [NOT] IN (value1. branch. ‘EEE’). SELECT sname. column2. Chennai – 600 025 Phone: 9445700859/ 22358814 . NOT reverses the meaning of the logical operator Retrieve the names. Other special Comparison operators The other comparison keywords available in SQL which are used to enhance the search capabilities of a SQL query..

SELECT sname WHERE sname FROM Student LIKE ‘p%’ . With SQL.SQL BETWEEN Syntax SELECT column_name(s) FROM table_name WHERE column_name [NOT] BETWEEN value1 AND value2 List the name and cgpa’s of student whose cgpa lies between 6. the following wildcards can be used: Wildcard Description % A substitute for zero or more characters _ A substitute for exactly one character Display the names of all students whose names starts with ‘p%’.5. SQL NULL Operator SELECT column_name(s) FROM table_name WHERE column_name IS [NOT] NULL List the name of any student whose branch is unknown.5 and 8. Copyright © 2012 College of Engineering. SQL LIKE Operator SELECT column_name(s) FROM table_name WHERE column_name [NOT]LIKE pattern SQL Wildcards SQL wildcards can substitute for one or more characters when searching for data in a database.5 AND 8. SELECT sname.5 . Anna University. Chennai – 600 025 Phone: 9445700859/ 22358814 . SELECT sname FROM Student WHERE cgpa BETWEEN 6. SQL wildcards must be used with the SQL LIKE operator. branch FROM Student WHERE branch IS NULL .

SELECT "function type" ("column_name") FROM "table_name" AVG: Average of the column. SELECT COUNT(column) FROM table_name MAX: Maximum of the column. SQL ORDER BY Syntax SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC Retrieve the list of students in alphabetical order SELECT sname FROM Student ORDER BY sname. If you want to sort the records in a descending order. cgpa FROM Student ORDER BY branch.0 in the ascending order of cgpa. The ORDER BY keyword sort the records in ascending order by default. SELECT sname. Chennai – 600 025 Phone: 9445700859/ 22358814 . SELECT sname. SELECT AVG(column) FROM table_name COUNT: Number of records. SQL Aggregate Functions The syntax for using functions is.The ORDER BY Keyword The ORDER BY keyword is used to sort the result-set by a specified column. Anna University. you can use the DESC keyword. Retrieve a list of all students with cgpa above 5.0 ORDER BY cgpa. Retrieve a list of students by branch and with branch arranges by highest cgpa first. cgpa FROM Student WHERE cgpa > 5. branch. cgpa DESC. SELECT MAX(column) FROM table_name Copyright © 2012 College of Engineering.

Chennai – 600 025 Phone: 9445700859/ 22358814 . List the departments having an average salary above Rs. SUM(column2) FROM table_name GROUP BY column1 HAVING (arithmetic function condition) What is the average salary paid to each department? SELECT dept. rank. Copyright © 2012 College of Engineering. avg(salary) FROM faculty where dept = ‘IT’. SELECT dept. SUM (column2) FROM table_name GROUP BY column1 SQL HAVING Syntax SELECT column1. SELECT SUM(column) FROM table_name What is the total salary paid to the faculty? SELECT sum(salary) FROM faculty. What is the total salary paid by rank in each department? SELECT dept. avg(salary) FROM faculty group by dept’. SELECT MIN(column) FROM table_name SUM: Sum of the column. Anna University. rank. avg(salary) FROM faculty GROUP BY dept HAVING avg(salary) > 100000 ORDER BY avg(salary) DESC. sum(salary) FROM faculty group by dept. SQL GROUP BY Syntax SELECT column1. SELECT count(*). 100000. Display the number of faculty members and the average salary paid to them for the faculty in IT department. order the results by average salary in the descending order of it.MIN: Minimum of the column.

SQL INNER JOIN Syntax SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1..column_name Display the list of courses and faculty members teaching the course. col2. table_name2 WHERE table_name1. A Join condition is a part of the sql query that retrieves rows from two or more tables.. A SQL Join condition is used in the SQL WHERE Clause of select. FROM table_name1.fid. fname FROM faculty INNER JOIN course ON faculty. as if the table were two tables. update.column_name=table_name2. SELECT courseid. Copyright © 2012 College of Engineering.Week IV (Practice simple queries using SELECT statement) SQL Joins SQL Joins are used to relate information in different tables. col3. SELECT courseid.col2 = table_name2. Different SQL JOINs      JOIN / INNER JOIN/ EQUI JOIN: Return rows when there is at least one match in both tables LEFT JOIN: Return all rows from the left table. Chennai – 600 025 Phone: 9445700859/ 22358814 .col1. SQL INNER JOIN Keyword The INNER JOIN keyword return rows when there is at least one match in both tables. Display the list of courses and faculty members teaching the course.fid. The Syntax for joining two tables is: SELECT col1. delete statements.fid = course. temporarily renaming at least one table in the SQL statement. even if there are no matches in the left table FULL JOIN: Return rows when there is a match in one of the tables SELF JOIN: is used to join a table to itself. cname. Anna University. even if there are no matches in the right table RIGHT JOIN: Return all rows from the right table.fid = course. fname FROM faculty. cname. course WHERE faculty.

Anna University. even if there are no matches in the left table (table_name1). SQL RIGHT JOIN Syntax SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.fid. cname.column_name=table_name2. even if there are no matches in the right table (table_name2).column_name SELECT courseid. Chennai – 600 025 Phone: 9445700859/ 22358814 .fid = course. fname FROM faculty LEFT JOIN course ON faculty. fname FROM faculty RIGHT JOIN course ON faculty. COURSE -----1 2 3 4 5 6 CNAME --------------a1 a2 a3 a4 a5 a6 FNAME -------------------aaa bbb ccc ddd eee aaa fff SQL RIGHT JOIN Keyword The RIGHT JOIN keyword returns all the rows from the right table (table_name2).COURSE -----1 2 3 4 5 6 CNAME --------------a1 a2 a3 a4 a5 a6 FNAME -------------------aaa bbb ccc ddd eee aaa SQL LEFT JOIN Keyword The LEFT JOIN keyword returns all rows from the left table (table_name1).fid. cname.column_name SELECT courseid.column_name=table_name2.fid = course. Copyright © 2012 College of Engineering. SQL LEFT JOIN Syntax SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.

cname. Copyright © 2012 College of Engineering. SELECT course.courseid = crsenrl. The report should include courseid.column_name SELECT courseid. SQL FULL JOIN Syntax SELECT column_name(s)FROM table_name1 FULL JOIN table_name2 ON table_name1. cname. Provide a class roster of students enrolled in ‘CS9202’. Chennai – 600 025 Phone: 9445700859/ 22358814 . sname FROM student.courseid.courseid. fname FROM faculty FULL JOIN course ON faculty.fid = course. COURSE -----1 2 3 4 5 6 7 CNAME --------------a1 a2 a3 a4 a5 a6 a7 FNAME -------------------aaa bbb ccc ddd eee aaa fff In which course student ‘anand’ has enrolled? SELECT courseid FROM student JOIN crsenrl ON student. course.sid = crsenrl. crsenrl WHERE course.fid. cname and sname. Anna University.sid and course.sid and sname = ‘anand’.column_name=table_name2.COURSE -----1 2 3 4 5 6 7 CNAME --------------a1 a2 a3 a4 a5 a6 a7 FNAME -------------------aaa bbb ccc ddd eee aaa SQL FULL JOIN Keyword The FULL JOIN keyword return rows when there is a match in one of the tables.Courseid = 1 and student.sid = crsenrl.

Anna University. SQL Alias SQL Alias Syntax for Tables SELECT column_name(s) FROM table_name AS alias_name SQL Alias Syntax for Columns SELECT column_name AS alias_name FROM table_name Copyright © 2012 College of Engineering.. WHERE some_column=some_value SQL DELETE Syntax DELETE FROM table_name WHERE some_column=some_value Delete All Rows DELETE * FROM table_name DELETE FROM table_name (OR) TOP CLAUSE The SQL TOP clause is used to fetch a TOP N number or X percent records from a table... Note: All the databases do not support TOP clause.SQL Syntax SQL UPDATE Syntax UPDATE table_name SET column1=value. column2=value2. Chennai – 600 025 Phone: 9445700859/ 22358814 . For example MySQL supports LIMIT clause to fetch limited number of records and Oracle uses ROWNUM to fetch limited number of records. SELECT TOP number|percent column_name(s) FROM table_name SELECT TOP 3 * FROM CUSTOMERS.

To select the new database. drop. CREATE DATABASE database_name CREATE DATABASE jnfdb. Syntax to drop a column ALTER TABLE table_name DROP COLUMN column_name. execute the following statement: USE jnfdb. SHOW DATABASES. Note: admin privilege is required to do these operations SQL ALTER TABLE Syntax The SQL ALTER TABLE command is used to modify the definition (structure) of a table by modifying the definition of its columns. Copyright © 2012 College of Engineering. DROP DATABASE DatabaseName. modify table columns 2) Add and drop constraints 3) Enable and Disable constraints Syntax to add a column ALTER TABLE table_name ADD column_name datatype. Anna University. 1) Add.SQL SELECT INTO Syntax We can select all columns into the new table: SELECT * INTO new_table_name [IN externaldatabase] FROM old_tablename SQL CREATE DATABASE Syntax The SQL CREATE DATABASE statement is used to create new SQL database. Chennai – 600 025 Phone: 9445700859/ 22358814 . The ALTER command is used to perform the following functions.

Syntax to add CHECK constraint to a table ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION). Syntax to rename a table RENAME old_table_name To new_table_name. column2 ] Copyright © 2012 College of Engineering.). Chennai – 600 025 Phone: 9445700859/ 22358814 .columnN = valueN [WHERE CONDITION ]. SQL DELETE Statement: DELETE FROM table_name WHERE {CONDITION}...Syntax to modify a column ALTER TABLE table_name MODIFY column_name datatype. Anna University.).. SQL UPDATE Statement: UPDATE table_name SET column1 = value1. SQL SET OPERATORS UNIONS CLAUSE SELECT column1 [. ALTER TABLE table_name ALTER COLUMN column_name datatype. Syntax to add UNIQUE constraint to a table ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1.. column2 = value2.. Syntax to DROP constraint from a table ALTER TABLE table_name DROP CONSTRAINT MyUniqueConstraint. column2. column2... Syntax to add PRIMARY KEY constraint to a table ALTER TABLE table_name ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1. Syntax to add a NOT NULL constraint to a column ALTER TABLE table_name MODIFY column_name datatype NOT NULL.

table2 ] [WHERE condition] EXISTS The EXISTS operator is used to search for the presence of a row in a specified table that meets certain criteria. Chennai – 600 025 Phone: 9445700859/ 22358814 . SELECT column1 [. Copyright © 2012 College of Engineering. column2 ] FROM table1 [. column2 ] FROM table1 [. column2 ] FROM table1 [. Anna University. table2 ] [WHERE condition] INTERSECT SELECT column1 [. column2 ] FROM table1 [. table2 ] [WHERE condition] The UNION ALL Clause: The UNION ALL operator is used to combine the results of two SELECT statements including duplicate rows. table2 ] [WHERE condition] UNION SELECT column1 [. column2 ] FROM table1 [. table2 ] [WHERE condition] INTERSECT Clause SELECT column1 [. column2 ] FROM table1 [. column2 ] FROM table1 [.FROM table1 [. table2 ] [WHERE condition] EXCEPT Clause The SQL EXCEPT clause/operator is used to combine two SELECT statements and returns rows from the first SELECT statement that are not returned by the second SELECT statement. table2 ] [WHERE condition] UNION ALL SELECT column1 [. table2 ] [WHERE condition] EXCEPT SELECT column1 [. SELECT column1 [.

Calculated columns may not be updated. A PL/SQL Block consists of three sections:    The Declaration section (optional). The FROM clause may not contain multiple tables. BETWEEN etc. The SELECT clause may not contain set operators. The SELECT clause may not contain set functions. and embedded within the WHERE clause.. The Exception (or Error) Handling section (optional). The WHERE clause may not contain subqueries. column2. IN. The Execution section (mandatory). >.. and DELETE statements along with the operators like =. CREATE VIEW view_name AS SELECT column1. INSERT. Copyright © 2012 College of Engineering. UPDATE. A view is actually a composition of a table in the form of a predefined SQL query. The SELECT clause may not contain an ORDER BY clause. Chennai – 600 025 Phone: 9445700859/ 22358814 . <. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. <=. Anna University..Sub queries/ Nested Queries A Subquery or Inner query or Nested query is a query within another SQL query. The query may not contain GROUP BY or HAVING. FROM table_name WHERE [condition]. Subqueries can be used with the SELECT. Views A view is nothing more than a SQL statement that is stored in the database with an associated name. >=. Updating a View: A view can be updated under certain conditions:           The SELECT clause may not contain the keyword DISTINCT.. All NOT NULL columns from the base table must be included in the view in order for the INSERT query to function. The SELECT clause may not contain summary functions.

Anna University. Copyright © 2012 College of Engineering.DECLARE Variable declaration BEGIN Program Execution EXCEPTION Exception handling END. PL/SQL Variables These are placeholders that store the values that can change through the PL/SQL Block. Chennai – 600 025 Phone: 9445700859/ 22358814 . The General Syntax to declare a variable is: variable_name datatype [NOT NULL := value ].

Sign up to vote on this title
UsefulNot useful