Rdbms Unit3 SVDC
Rdbms Unit3 SVDC
SQL:
Advantages of SQL
1. High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount of
records from a database.
2. No coding needed
In the standard SQL, it is very easy to manage the database system. It no need to write
the code.
Long established are used by the SQL databases that are being used by ISO and ANSI.
SQL can be used in laptop, PCs, server and even some mobile phones.
4. Interactive language
Using the SQL language, the users can make different views of the database structure.
Each column in a database table is required to have a name and a data type.
An SQL developer must decide what type of data that will be stored inside each column
when creating a table.
Datatype description
0 to 255
values.
SQL command
a) CREATE
b) ALTER
c) DROP
d) TRUNCATE
Example:
b. DROP: It is used to delete both the structure and record stored in the table.
c. ALTER:
It is used to alter the structure of the database. It is used for two purposes :
1) To add new column
2) Modify the column
EXAMPLE
d. TRUNCATE: It is used to delete all the rows from the table and free the space
containing the table.
a) INSERT
b) UPDATE
c) DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the
row of a table.
For example:
b. UPDATE: This command is used to update or modify the value of a column in the
table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [W
HERE CONDITION]
For example:
UPDATE employee
SET Name = 'raju'
WHERE eno = 3;
DCL commands are used to grant and take back authority from any database user.
Grant
Revoke
Example
Example
TCL commands can only use with DML commands like INSERT, DELETE and
UPDATE only.
These operations are automatically committed in the database that's why they cannot
be used while creating tables or dropping them.
a) COMMIT
b) ROLLBACK
c) SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
Example:
b. Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
Syntax: ROLLBACK;
Example:
c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling
back the entire transaction.
For example:
4Q) explain about sql clauses or grouping results? (or) explain about the
following :
1. GROUP BY
SQL GROUP BY statement is used to arrange identical data into groups. The
GROUP BY statement is used with the SQL SELECT statement.
The GROUP BY statement follows the WHERE clause in a SELECT statement
and precedes the ORDER BY clause.
The GROUP BY statement is used with aggregation function.
Syntax
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Example:
Output:
Com1 2
Com2 2
Com3 1
2. HAVING
HAVING clause is used to specify a search condition for a group or an
aggregate.
Having is used in a GROUP BY clause. If you are not using GROUP BY clause
then you can use HAVING function like a WHERE clause.
Syntax:
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING COUNT(*)>=2;
Output: Com1 2
Com2 2
3. ORDER BY
The ORDER BY clause sorts the result-set in ascending or descending order.
It sorts the records in ascending order by default. DESC keyword is used to sort
the records in descending order.
Syntax:
Where
Table:CUSTOMER
12 Kathrin US
23 David Bangkok
34 Alina Dubai
Output:
34 Alina Dubai
23 David Bangkok
56 Harry US
Views in SQL
Views in SQL are considered as a virtual table. A view also contains rows and
columns.
To create the view, we can select the fields from one or more tables present in
the database.
A view can either have specific rows based on certain condition or all the rows of
a table.
View name can be used as sub title for the table name in sql.
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
1. Creating view
A view can be created using the CREATE VIEW statement. We can create a view
from a single table or multiple tables.
Syntax:
In this example, we create a View named DetailsView from the table Student_Detail.
Query:
Just like table query, we can query the view to view the data.
OUTPUT
Stephan Delhi
Kathrin Noida
David Ghaziabad
3. Deleting View
Example:
SQL Index
Indexes are special lookup tables. It is used to retrieve data from the database
very fast.
An index in a database is just like an index in the back of a book.
For example: When you reference all pages in a book that discusses a certain
topic, you first have to refer to the index, which alphabetically lists all the topics
and then referred to one or more specific page numbers.
SQL JOIN
As the name shows, JOIN means to combine something. In case of SQL, JOIN
means "to combine two or more tables".
In SQL, JOIN clause is used to combine the records from two or more tables in a
database.
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both tables as
long as the condition is satisfied. It returns the combination of all rows from both the
tables where the condition satisfies.
Syntax
Query
OUTPUT
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
The SQL left join returns all the values from left table and the matching values from
the right table. If there is no matching join value, it will return NULL.
Syntax
Query
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of right
table and the matched values from the left table. If there is no matching in both
tables, it will return NULL.
Syntax
Query
OUTPUT:
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join
tables have all the records from both tables. It puts NULL on the place of matches
not found.
Syntax
Query
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL