DBMS Note
DBMS Note
Example:
ALTER TABLE STU_DETAILS MODIFY (ADHAR_NUMVARCHAR2 (20));
3. Drop
You cannot only modify columns but you can also drop thementirely if it is no longer required in a
table. Using drop statement in alter command we can also remove the constraints formthe table.
Syntax: To drop a column froma table.
ALTER TABLE table_name DROP COLUMN column_name;
Example:
ALTER TABLE STU_DETAILS DROP COLUMN ADHAR_NUM;
4. Rename:
SQLprovides the facility to change the column name by using a ALTER TABLE statement.
Syntax:
ALTER TABLE TABLE_NAME RENAME COLUMN <OLD_TABLENAME> to <NEW_TABLENAME>;
Example:
ALTER TABLE STUDENT Rename column STU_NAME toSTUDENT_NAME;
D. TRUNCATE: It is used to delete all the rows fromthe table and free the space containing the
table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE EMPLOYEE;
2. Data Manipulation Language
DMLcommands are used to modify the database. It is responsible for all formof
changes in the database.
The command of DMLis not auto-committed that means it can't permanently save all
the changes in the database. They can be rollback.
Following are the some commands that come under DML:
INSERT,UPDATE,DELETE
a. INSERT: The INSERT statement is a SQLquery. It is used to insert data into the row of a
table.
Syntax: To add row in a table
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2, value3, .... v
alueN);
OR
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
EXAMPLE
INSERT INTO STUDENT(Author, Subject) VALUES (‘Sonoo’, ‘DBMS’);
OR
INSERT INTO STUDENT VALUES (‘Sonoo’, ‘DBMS’);
b. UPDATE: This command is used to update or modify the value of a column in the table.
Syntax: To update record in a table
UPDATE table_name SET [column_name1= value1] [WHERE CONDITION] ;
EXAMPLE
UPDATE students SET User_Name = 'Sonoo' WHERE Student_Id = '3'
d. SELECT: This is the same as the projection operation of relational algebra. It is used to select the
attribute based on the condition described by WHERE clause.
Syntax: It is used for retrieve the records fromtable
SELECT expressions FROMTABLES WHERE conditions;
Example:
SELECT emp_name FROMemployee WHERE age > 20;
SQLOperator
The SQLreserved words and characters are called operators, which are used with a WHERE clause
in a SQLquery. In SQL, an operator can either be a unary or binary operator. The unary operator
uses only one operand for performing the unary operation, whereas the binary operator uses two
operands for performing the binary operation.
SQLOperator Symbols
Operators
**
Exponentiation operator
+, -
Identity operator, Negation operator
*, /
Multiplication operator, Division operator
+, -, ||
Addition (plus) operator, subtraction (minus) operator, String Concatenation operator
=, !=, <, >, <=, >=, ISNULL, LIKE, BETWEEN, IN
Comparison Operators
NOT
Logical negation operator
&& or AND
Conjunction operator
OR
Inclusion operator
Types of Operator
SQLoperators are categorized in the following categories:
● SQLArithmetic Operators
● SQLComparison Operators
● SQLLogical Operators
● SQLSet Operators
● SQLBit-wise Operators
● SQLUnary Operators
Arithmetic Operators
The Arithmetic Operators performthe mathematical operation on the numerical data of the SQL
tables. These operators performaddition, subtraction, multiplication, and division operations on the
numerical operands.
Following are the various arithmetic operators
1. SQLAddition Operator (+)
2. SQLSubtraction Operator (-)
3. SQLMultiplication Operator (*)
4. SQLDivision Operator (/)
5. SQLModulus Operator (%)
1. SQLAddition Operator (+)
The Addition Operator in SQLperforms the addition on the numerical data of the database table. In
SQL, we can easily add the numerical values of two columns of the same table by specifying both the
column names as the first and second operand.
Example :
SELECT Emp_Salary + 20000 as Emp_New_Salary FROMEmployee_details;
SQLComparisonOperators
This comparison operator checks that the left side operand is lesser than the right
side operand. If the condition becomes true, then this operator in SQLdisplays the data which is
less than the value of the right-side operand.
Example :
Example:
SQLLogical Operators
The Logical Operatorsin SQLperformthe Boolean operations, which give two results
True and False. These operators provide True value if both operands match the logical condition.
Example:
Write a query to retrieve only those records of employees fromthe employees table where
the designation is 'Project Manager' and the City to which the employee belongs to is Mumbai.
SELECT * FROMemployees WHERE City = ‘Mumbai’ AND Designation = ‘Project Manager’;
2. SQLOR OPERATOR :
The SQLOR operator is used with the where clause in an SQLQuery. ANDoperator in SQL
returns only those records that satisfy any of the conditions in the SQLquery.
Example:
Write a query to retrieve only those records of employees fromthe employees table where
the employee's designation is 'SystemEngineer' or the city to which the employee belongs is Mumbai.
SELECT * FROMemployees WHERE Designation = ‘SystemEngineer’ OR City = ‘Mumbai’;
3. SQLNOT OPERATOR :
NOT operator in SQLshows those records fromthe table where the criteria is not met.
NOT operator is used with where clause in a SELECT query.
Example:
Write a query to retrieve only those records of employees fromthe employees table where
the employee's designation is not Project Manager.
SELECT * FROMemployees WHERE NOT Designation = ‘Project Manager’;
4. SQLBETWEENOPERATOR :
This operator displays the records which fall between the given ranges in the SQL
query. The results of the BETWEENoperator include begin and end values of the given
range.
Example:
Write a query to retrieve only those records of an employee fromthe employees table
where employee salary lies between 50000 to 90000.
SELECT * FROMemployees WHERE Salary BETWEEN 50000 AND 90000;
5. SQLINOPERATOR :
When we want to check for one or more than one value in a single SQLquery, we use IN
operator with the WHERE clause in a SELECT query.
Example:
Write a query to retrieve only those records of employees fromthe employees table
where the city to which the employee belongs to iseither Mumbai, Bangalore, or Pune.
SELECT * FROMemployees WHERE City IN (‘Mumbai’, ‘Bangalore’, ‘Pune’);
6. SQLLIKE OPERATOR :
LIKE Operator in SQLdisplays only those data fromthe table which matches the pattern
specified in the query. Percentage (%) and underscore (_) are the two wildcard operators
used with LIKE Operator to performpattern matching tasks.
Example:
Write a query to retrieve only those records of employees fromthe employees table
whose salary starts with the digit 5.
SELECT * FROMemployees WHERE Salary LIKE ‘5%’;
SQLSet Operation:
The SQLSet operation is used to combine the two or more SQLSELECT statements.
3. Intersect
It is used to combine two SELECT statements. The Intersect operation returns the common
rows fromboth the SELECT statements.
In the Intersect operation, the number of datatype and columns must be the same.
It has no duplicates and it arranges the data in ascending order by default.
Syntax:
SELECT column_name FROMtable1 INTERSECT SELECT column_name FROMtable2;
4. Minus
It combines the result of two SELECT statements. Minus operator is used to display the
rows which are present in the first query but absent in the second query.
It has no duplicates and data arranged in ascending order by default.
Syntax:
SELECT column_name FROMtable1 MINUS SELECT column_name FROMtable2;
Aggregate Functions:
SQLaggregation function is used to performthe calculations on multiple rows of a single
column of a table. It returns a single value.
1. COUNT FUNCTION
COUNT function is used to Count the number of rows in a database table. It can work on
both numeric and non-numeric data types.
COUNT function uses the COUNT(*) that returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and Null.
Syntax :
SELECT COUNT(COLUMNNAME) FROMTABLE_NAME ;
2. SUMFunction
Sumfunction is used to calculate the sumof all selected columns. It works on numeric
fields only.
Syntax :
SELECT SUM(COLUMNNAME) FROMTABLE_NAME ;
3. AVGFunction
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.
Syntax :
SELECT AVG(COLUMNNAME) FROMTABLE_NAME ;
4. MAXFunction
MAX function is used to find the maximumvalue of a certain column. This function
determines the largest value of all selected values of a column.
Syntax :
SELECT MAX(COLUMNNAME) FROMTABLE_NAME ;
5. MINFunction
MINfunction is used to find the minimumvalue of a certain column. This function
determines the smallest value of all selected values of a column.
Syntax :
SELECT MIN(COLUMNNAME) FROMTABLE_NAME ;
Constraintsin SQL:
SQLconstraints are used to specify rules for the data in a table. Constraintsare used to limit
the type of data that can go into a table.
2. UNIQUE
Duplicate values are not allowed in the columns to which the UNIQUE constraint is applied.
The column with the unique constraint will always contain a unique value.
This constraint can be applied to one or more than one column of a table, which means
more than one unique constraint can exist on a single table.
Syntax :
CREATE TABLE TableName (ColumnName1 datatype UNIQUE, ColumnName2 datatype,…., Colu
mnNameN datatype);
3. PRIMARY KEY
PRIMARY KEY Constraint is a combination of NOT NULLand Unique constraints.
NOT NULLconstraint and a UNIQUE constraint together forms a PRIMARY constraint.
Syntax :
CREATE TABLE TableName (ColumnName1 datatype PRIMARY KEY, ColumnName2 datatype,….,
ColumnNameN datatype);
4. FOREIGNKEY
Aforeign key is used for referential integrity.
When we have two tables, and one table takes reference fromanother table, i.e., the same
column is present in both the tables and that column acts as a primary key in one table.
That particular column will act as a foreign key in another table.
Syntax :
6. DEFAULT
Whenever a default constraint is applied to the table's column, and the user has not
specified the value to be inserted in it, then the default value which was specified while applying
the default constraint will be inserted into that particular column.
Syntax :
CREATE TABLE TableName (ColumnName1 datatype DEFAULT Value, ColumnName2 datatype,…., Colum
nNameN datatype);
Viewsin SQL
Views in SQLare considered as a virtual table. Aview also contains rows and columns.
To create the view, we can select the fields fromone or more tables present in the
database.
Aview can either have specific rows based on certain condition or all the rows of a table.
1. Creating view
Aview can be created using the CREATE VIEWstatement. We can create a view froma
single table or multiple tables.
Syntax:
CREATE VIEWview_name AS SELECT column1, column2..... FROMtable_name WHERE conditi
on;
Syntax:
CREATE VIEWMarksView AS SELECT Student_Detail.NAME, Student_Detail.ADDRESS, Student
_Marks.MARKS FROMStudent_Detail, Student_Mark WHERE Student_Detail.NAME = Student
_Marks.NAME;
4. Deleting View
Aview can be deleted using the Drop View statement.
Syntax :
DROP VIEWview_name;
SQLSubQuery
ASubquery is a query within another SQLquery and embedded within the WHERE clause.
Syntax:
UPDATE table SET column_name = new_value WHERE VALUE OPERATOR (SELECT COLUMN_
NAME FROMTABLE_NAME WHERE condition);