Professional Documents
Culture Documents
• Tables – In the Relational model the, relations are saved in the table
format. It is stored along with its entities. A table has two properties
rows and columns. Rows represent records and columns represent
attributes.
• Relation key - Every row has one, two or multiple attributes, which is
called relation key.
Example:
Example:
ALTER TABLE Employee ADD City VARCHAR(20)
Change data type and size of an existing column
Syntax to change the existing column datatype and size:
For example, If we want to grant a user ‘john’ for the SELECT, INSERT, UPDATE, and DELETE
privileges on a table called ‘Employee’, we should run the following GRANT statement.
REVOKE – Withdraws or take back some or all the user’s access privileges to
the database given by using the GRANT command.
Example to revoke the privileges SELECT, INSERT, UPDATE, and DELETE from a
user on a table ;
1 Abhi
2 Adam
4 Alex
Lets use some SQL queries on the table:
1 Abhi
2 Adam
4 Alex
5 Abhijit
6 Chris
7 Bravo
Now let's use the ROLLBACK command to roll
back the state of data to the savepoint B.
• ROLLBACK TO B;
• SELECT * FROM student;
Student table at savepoint B
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
6 Chris
• Now let's again use the ROLLBACK command to
roll back the state of data to the savepoint A
• ROLLBACK TO A;
• SELECT * FROM student;
Student table at savepoint A
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
Data Query Language
DQL is used to fetch the data from the database.
It uses only one command:
SELECT
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;
For example:
SELECT emp_name
FROM employee
WHERE age > 20;
Class Table
SQL queries:
• INSERT into CLASS VALUES (101, ‘Rahul);
• Commit;
• Rollback to B;
• rollback to A;
• COMMIT;
•NOT NULL: This constraint tells that we cannot store a null value in a column. That is, if a column is
specified as NOT NULL then we will not be able to store null in this particular column any more.
•UNIQUE: This constraint when specified with a column, tells that all the values in the column must be
unique. That is, the values in any row of a column must not be repeated.
•PRIMARY KEY: A primary key is a field which can uniquely identify each row in a table. And this
constraint is used to specify a field in a table as primary key.
•FOREIGN KEY: A Foreign key is a field which can uniquely identify each row in a another table. And this
constraint is used to specify a field as Foreign key.
•CHECK: This constraint helps to validate the values of a column to meet a particular condition. That is, it
helps to ensure that the value stored in a column meets a specific condition.
•DEFAULT: This constraint specifies a default value for the column when no value is specified by the user.
• We can specify constraints at the time of creating the table
using CREATE TABLE statement.
• We can also specify the constraints after creating a table using
ALTER TABLE statement.
Syntax:
CREATE TABLE sample_table
( column1 data_type(size) constraint_name,
column2 data_type(size) constraint_name,
column3 data_type(size) constraint_name, .... );
NOT NULL
Example:
Example:
Example:
Example:
Example:
1) DROP Command.
2) ALTER Command.
DROP Command:
The DROP command can be used to drop named
schema elements, such as tables, domains, or
constraints.
• With the CASCADE option, all such constraints, views, and other
elements that reference the table being dropped are also dropped
automatically from the schema, along with the table itself.
• The DROP TABLE command not only deletes all the records in the
table successful, but also removes the table definition from the
catalog
ALTER Command:
The definition of a base table or of other named schema
elements can be changed by using the ALTER command.
Syntax:
Alter Table Drop Column .. cascade;
Ex:
ALTER TABLE COMPANY.EMPLOYEE DROP
COLUMN Address CASCADE;
It is also possible to alter a column definition by
dropping an existing default clause or by defining a
new default clause.
Syntax:
Alter Table Alter Column ….;
Ex:
ALTER TABLE COMPANY.DEPARTMENT
ALTER(modify) COLUMN Mgr_ssn Varchar(10);
View in SQL
• Views in SQL are kind of virtual tables.
• A view also has rows and columns as they are in a
real table in the database.
• We can create a view by selecting fields from one or
more tables present in the database.
• A View can either have all the rows of a table or
specific rows based on certain condition.
• We can create View using CREATE VIEW statement.
• A View can be created from a single table or
multiple tables.
Syntax:
Name Age
1 19
2 20
3 19
• Example:
Department
Dept_ID Stream Stud_id
1 CS 1
2 IT 1
3 ECE 2
4 ECE 9
Stud_details
Stream Stud_id
ECE 2
ECE 9
Creating View from multiple tables:
• To create a View from multiple tables we can simply
include multiple tables in the SELECT statement.
Syntax :
Marks
CREATE VIEW MarksView AS
SELECT Student.NAME, Student.ADDRESS, Marks.MARKS
FROM Student, Marks
WHERE Student.NAME = Marks.NAME;
Syntax
DROP VIEW view_name;
Example:
If we want to delete the View MarksView, we can
do this as:
DROP VIEW MarksView;
- Create View containing name, item name and quantity from the following tables.
- Create View containing all the id details from the following tables.
Create view containing customer name with
corresponding customer’s account number
Joins in SQL
• An SQL join is a join operation in relational
algebra – combines columns from one or more
tables in a relational database.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Syntax:
• SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 FULL JOIN table2
ON table1.matching_column = table2.matching_column;