Unit III Fis
Unit III Fis
Unit III Fis
Data Definition Language (DDL) is a set of commands used to define and manage the structure
of a database. DDL commands are used to create, modify, and delete database objects such as
tables, indexes, and views. Some common DDL commands include CREATE, ALTER, and DROP.
Data Query Language (DQL) is a set of commands used to retrieve data from a database. DQL
commands are used to search, filter, and sort data in a database. Some common DQL commands
include SELECT, FROM, and WHERE.
When working with databases, DDL commands are used to create and modify the database
schema, while DQL commands are used to retrieve and manipulate data within the database. It's
important to understand both DDL and DQL commands in order to effectively manage and use a
database.
Basic DDL
Here are some basic DDL (Data Definition Language) commands:
1. CREATE: This command is used to create a new database, table, index, or view.
4. TRUNCATE: This command is used to delete all the data in a table, but it keeps the table
structure intact.
5. COMMENT: This command is used to add comments to the database objects, such as
tables, columns, views, etc.
6. RENAME: This command is used to rename an existing database object, such as a table,
column, or view.
These are just a few examples of the basic DDL commands that can be used to define and
manage the structure of a database.
Introduction to SQL
SQL (Structured Query Language) is a programming language used to manage and manipulate
relational databases. It is used to create, modify, and query databases, and is widely used in both
industry and academia. SQL is a standard language and is supported by most relational database
management systems, including Oracle, MySQL, Microsoft SQL Server, and PostgreSQL.
SQL consists of two types of commands: Data Definition Language (DDL) and Data Manipulation
Language (DML). DDL commands are used to create and modify the structure of a database,
including creating tables, defining relationships between tables, and creating indexes. DML
commands are used to insert, update, delete, and retrieve data from a database.
SQL queries are written using a syntax that is similar to English. The basic syntax for a SELECT
statement, which is used to retrieve data from a database, is as follows:
FROM table_name
WHERE condition;
This statement selects data from one or more columns in a table, based on a specified condition.
The result of the query is a set of rows that match the specified condition.
SQL also includes a number of built-in functions that can be used to manipulate data, such as
calculating averages, finding maximum or minimum values, and converting data types.
Overall, SQL is a powerful tool for managing and analyzing data in a relational database, and is
an essential skill for anyone working with databases.
Data Constraints
Data constraints are rules that are enforced on the data stored in a database to ensure its
accuracy, consistency, and integrity. Constraints are used to prevent invalid data from being
inserted or updated in a database, and to maintain the relationships between tables. Here are
some common data constraints used in databases:
1. Primary Key Constraint: This constraint is used to uniquely identify a row in a table. It
ensures that each row has a unique value for the specified column or set of columns. The
primary key constraint is used to enforce referential integrity between tables.
2. Foreign Key Constraint: This constraint is used to ensure that the values in a column or
set of columns in one table correspond to the values in a primary key column in another
table. It is used to enforce referential integrity between tables.
3. NOT NULL Constraint: This constraint is used to ensure that a column cannot have null
values. It is used to enforce data integrity by requiring that a value must be provided for a
column.
4. CHECK Constraint: This constraint is used to ensure that the values in a column or set of
columns meet a specified condition. It is used to enforce data integrity by requiring that
the data meet certain criteria.
5. UNIQUE Constraint: This constraint is used to ensure that the values in a column or set of
columns are unique. It is used to enforce data integrity by preventing duplicate values
from being inserted in a table.
6. Default Constraint: This constraint is used to provide a default value for a column if no
value is specified during an insert operation.
Data constraints help to ensure the accuracy and consistency of data in a database. They are an
important tool for maintaining data integrity and for enforcing relationships between tables.
Advanced SQL
Advanced SQL is a set of techniques and concepts used to manipulate data in a database beyond
the basic SELECT, INSERT, UPDATE, and DELETE statements. Here are some advanced SQL
techniques:
1. Subqueries: A subquery is a query nested inside another query. It is used to retrieve data
that will be used in the main query. Subqueries can be used in the WHERE, HAVING, and
FROM clauses.
Example:
FROM products
2. Joins: Joins are used to combine data from two or more tables based on a
common column. There are several types of joins, including INNER JOIN, LEFT
JOIN, RIGHT JOIN, and FULL OUTER JOIN.
Example:
FROM orders
Example:
FROM customers
Example:
BEGIN
FROM orders
END;
Example:
BEGIN TRANSACTION;
COMMIT TRANSACTION;
These are just a few examples of advanced SQL techniques. Advanced SQL is a powerful tool for
manipulating and analyzing data in a database, and is an essential skill for database
professionals.
Views
In SQL, a view is a virtual table that is based on the result of a SELECT statement. Views are used
to simplify complex queries and to provide a customized view of the data to different users. Here
are some key features of views:
1. A view is defined by a SELECT statement. The result of the SELECT statement is stored as a
virtual table in the database.
2. A view is created using the CREATE VIEW statement. The syntax is as follows:
CREATE VIEW view_name AS
FROM table_name
WHERE condition;
3. Views can be used to provide a customized view of the data to different users. For
example, a sales department may only need to see customer and order information, while
a marketing department may need to see additional data such as product details and
sales figures.
4. Views can be used to simplify complex queries. For example, a complex query that
involves multiple tables and joins can be simplified by creating a view that combines the
necessary data.
5. Views can be used to provide an additional layer of security by controlling access to
sensitive data. For example, a view can be created that only shows non-sensitive data to
users who do not have permission to view the sensitive data.
6. Views do not store data themselves. They are based on the underlying tables in the
database, and any changes to the underlying tables will be reflected in the view.
7. Views can be updated just like tables, provided that they meet certain criteria. For
example, the view must be based on a single table, and it must not contain any
aggregate functions or subqueries.
Overall, views are a powerful tool for simplifying complex queries and providing a customized
view of the data to different users. They can be used to improve performance, enhance security,
and provide an additional layer of abstraction between the data and the users.
Triggers
In SQL, a trigger is a set of actions that are automatically executed in response to a specific
database event, such as an insert, update, or delete operation on a table. Here are some key
features of triggers:
1. Triggers are defined using the CREATE TRIGGER statement. The syntax is as follows:
CREATE TRIGGER trigger_name
BEFORE/AFTER INSERT/UPDATE/DELETE
ON table_name
BEGIN
END;
The trigger_name is the name of the trigger, BEFORE or AFTER specifies when the trigger should
be executed, INSERT, UPDATE, or DELETE specifies the type of operation that should activate the
trigger, table_name is the name of the table on which the trigger should be defined, and the FOR
EACH ROW specifies that the trigger should be executed once for each row affected by the
operation.
2. Triggers can be used to enforce business rules and data integrity constraints. For
example, a trigger can be used to prevent the deletion of a row in a table that is
referenced by another table.
3. Triggers can be used to audit database activity. For example, a trigger can be used to log
information about changes to a table, such as the user who made the change and the
time of the change.
4. Triggers can be used to replicate data between tables or databases. For example, a
trigger can be used to automatically update a backup database whenever a row is
inserted, updated, or deleted in the primary database.
5. Triggers can be nested. That is, one trigger can activate another trigger.
6. Triggers can be disabled or enabled using the ALTER TABLE statement.
Overall, triggers are a powerful tool for enforcing business rules, maintaining data integrity, and
auditing database activity. However, they can also be complex to manage and can have
performance implications, especially if they are used extensively. As with any database feature, it
is important to use triggers judiciously and to carefully test and monitor their performance.
Database Security
Database security refers to the measures taken to protect a database from unauthorized access,
use, or modification. Here are some key aspects of database security:
Overall, database security is a complex and ongoing process that requires a combination of
technical controls, policies and procedures, and user education and awareness. By implementing
a comprehensive security program, organizations can help protect their databases from
unauthorized access and data breaches.
Embedded &Dynamic SQL
Embedded SQL and Dynamic SQL are two approaches for executing SQL statements in
applications.
1. Embedded SQL: Embedded SQL involves embedding SQL statements directly into
application code. The SQL statements are precompiled and optimized by the database
management system (DBMS) before they are executed. This approach is often used in
applications that require high performance and scalability, such as transaction processing
systems. Embedded SQL is typically used in programming languages like C/C++, COBOL,
and FORTRAN.
#include <stdio.h>
#include <sqlca.h>
char name[20];
int age;
int main() {
if (sqlca.sqlcode != 0) {
return 1;
EXEC SQL INSERT INTO users (name, age) VALUES (:name, :age);
if (sqlca.sqlcode != 0) {
return 1;
return 0;
import psycopg2
cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
print(row)
cur.close()
conn.close()
In summary, Embedded SQL and Dynamic SQL are two approaches for executing SQL statements
in applications. Embedded SQL is used when performance is critical and queries are static, while
Dynamic SQL is used when flexibility is important and queries are dynamic.