0% found this document useful (0 votes)
24 views13 pages

DBMS Record Management Techniques

The document describes SQL commands including DDL, DML, DCL, TCL, and DQL commands. It provides the syntax and examples of using commands like CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, COMMIT, ROLLBACK, GRANT, REVOKE, and SELECT to manage tables, insert/modify data, manage transactions and users. Sample code is provided to demonstrate creating a table, adding/dropping columns, inserting and modifying data, using savepoints and rolling back transactions, and selecting data from the table.

Uploaded by

muzzammil4422
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views13 pages

DBMS Record Management Techniques

The document describes SQL commands including DDL, DML, DCL, TCL, and DQL commands. It provides the syntax and examples of using commands like CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, COMMIT, ROLLBACK, GRANT, REVOKE, and SELECT to manage tables, insert/modify data, manage transactions and users. Sample code is provided to demonstrate creating a table, adding/dropping columns, inserting and modifying data, using savepoints and rolling back transactions, and selecting data from the table.

Uploaded by

muzzammil4422
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Ex:1 SQL COMMANDS

Date:
Exp no:1-a
IMPLEMENTATION OF DDL/DML COMMANDS

AIM:

DESCRIPTION:
DDL:
CREATE: Creates a database, new table, a view of a
table, index, cursor and trigger or other object in
database.
ALTER: Modifies an existing database object, such as a
table, view and index etc.
DROP: Deletes an entire table, a view of a table or
other object in the database.
RENAME: Used to renaming table, view etc
TRUNCATE: Used to delete record permanent of a
table.
DML:
INSERT: used to insert a record on the table.
UPDATE: modification of records in the table.
DELETE: used to remove records in the table.

SYNTAX:
DDL:
CREATE DATABASE database_name
CREATE TABLE table_name
(Column_name datatype[(size)],
Column_name datatype[(size)],)

ALTER TABLE table_name


ADD (column datatype [Default Expression])
[REFERENCES table_name (column_name)’
[CHECK condition]
DROP TABLE table_name
DROP INDEX table_name
ALTER TABLE table_name RENAME TO
new_table_name;

TRUNCATE TABLE table_name;

DML:
INSERT INTO table_name[(column_list)] values
(value_list)

DELETE FROM table_name [WHERE Condition]

UPDATE table_name SET column_name1 = value1,


column_name2 = value2, …..[WHERE Condition]
CODE & OUTPUT:
SQL> create table studentDB(rrn int,name
varchar(10),department varchar(10));
Table created.
SQL> alter table studentDB add(year int);
Table altered.
SQL> alter table studentDB add(section varchar(10));
Table altered.
Name Null? Type
----------------------------------------- -------- ----------------------------
RRN NUMBER(38)
NAME VARCHAR2(10)
DEPARTMENT VARCHAR2(10)
YEAR NUMBER(38)
SECTION VARCHAR2(10)
SQL> alter table studentDB drop column section;
Table altered.
Name Null? Type
----------------------------------------- -------- ----------------------------
RRN NUMBER(38)
NAME VARCHAR2(10)
DEPARTMENT VARCHAR2(10)
YEAR NUMBER(38)
SQL>alter table studentDB rename to classDB;
Table altered.

SQL> insert into classDB values(1001,'Adhi','CSE',2);


1 row created.
SQL> insert into classDB values(1002,'Billy','CSE',2);
1 row created.
SQL> insert into classDB values(1003,'Cillian','CSE',2);
1 row created.
SQL> insert into classDB values(1004,'Dilli','CSE',2);
1 row created.
SQL> insert into classDB values(1005,'Edith','CSE',2);
1 row created.
SQL> select * from classDB;
RRN NAME DEPARTMENT YEAR
---------- ------------------ ------------------------
--------------------
1001 Adhi CSE 2
1002 Billy CSE 2
1003 Cillian CSE 2
1004 Dilli CSE 2
1005 Edith CSE 2
SQL> delete from classDB where rrn=1005;
1 rows deleted.
RRN NAME DEPARTMENT YEAR
---------- ------------------ ------------------------
--------------------
1001 Adhi CSE 2
1002 Billy CSE 2
1003 Cillian CSE 2
1004 Dilli CSE 2
SQL> update classDB set department='IT' where rrn=1003;
1 row updated.
SQL> update classDB set department='IT' where rrn=1004;
1 row updated.
RRN NAME DEPARTMENT YEAR
---------- ------------------ ------------------------
--------------------
1001 Adhi CSE 2
1002 Billy CSE 2
1003 Cillian IT 2
1004 Dilli IT 2
RESULT:
Exp no:1-b
IMPLEMENTATION OF DCL/TCL COMMANDS

AIM:

DESCRIPTION:
DCL:
GRANT COMMAND: It is used to create users and grant
access to the database. It requires database
administrator (DBA) privilege, except that a user can
change their password. A user can grant access to their
database objects to other users.

REVOKE COMMAND: Using this command, the DBA can


revoke the granted database privileges from the user.

TCL:
COMMIT: It is used to permanently save any
transaction into database.
SAVEPOINT: It is used to temporarily save a transaction
so that you can rollback to that point whenever
necessary.

ROLLBACK: It restores the database to last committed


state. It is also use with save point command to jump
to a save point in a transaction

SYNTAX:
GRANT COMMAND
Grant <database_priv [database_priv.....] > to
<user_name> identified by
<password>[,<password.....];
Grant <object_priv> | All on <object> to <user |
public> [ With Grant Option ];

REVOKE COMMAND
Revoke <database_priv> from <user [, user ]>;
Revoke <object_priv> on <object> from<user| public >;
<database_priv>
COMMIT:
Commit;

SAVEPOINT:
Savepoint savepoint_name;

ROLLBACK:
Rollback to savepoint_name;

CODE & OUTPUT:


SQL> grant all on classDB to public;
Grant succeeded.
SQL> revoke all on classDB from public;
Revoke succeeded.

-------------------------------------------------------------------------
SQL> update classDB set year='3' where name='Billy';
1 row updated.
SQL> savepoint A;
Savepoint created.

SQL> insert into classDB


values(1006,'Heisenberg','CHEM',2);
1 row created.

SQL> select * from classDB;

RRN NAME DEPARTMENT YEAR


---------- ------------------ ------------------------
--------------------
1001 Adhi CSE 2
1002 Billy CSE 3
1003 Cillian IT 2
1004 Dilli IT 2
1006 Heisenberg CHEM 2

SQL> rollback to A;
Rollback complete.

SQL> select * from classDB;


RRN NAME DEPARTMENT YEAR
---------- ------------------ ------------------------
--------------------
1001 Adhi CSE 2
1002 Billy CSE 3
1003 Cillian IT 2
1004 Dilli IT 2

SQL> commit;
Commit complete.

RESULT:
Exp no:1-C
IMPLEMENTATION OF DQL COMMANDS

AIM:

DESCRIPTION:
SELECT: It is used to retrieve data from the database.

SYNTAX:
SELECT column1,column2 FROM table_name
column1 , column2: names of the fields of the table
table_name: from where we want to apply query

SELECT * FROM table_name;


-- asterisks represent all attributes of the table
CODE & OUTPUT:

SQL> select rrn,name from classDB;


RRN NAME
---------- ------------------
1001 Adhi
1002 Billy
1003 Cillian
1004 Dilli

SQL> select * from classDB;


RRN NAME DEPARTMENT YEAR
---------- ------------------ ------------------------
--------------------
1001 Adhi CSE 2
1002 Billy CSE 3
1003 Cillian IT 2
1004 Dilli IT 2

RESULT:

Common questions

Powered by AI

The ALTER command enhances table management by allowing modifications to an existing table structure without dropping and recreating the table, which would otherwise result in data loss. It enables adding and dropping columns, changing data types, renaming columns or the table itself, and modifying constraints . These capabilities help administrators adapt tables to evolving data requirements or correct design oversights without affecting the continuity of data utilization or application operation. It provides flexibility and extensibility above what the initial CREATE command offers, which only defines an initial structure .

SAVEPOINT provides a significant advantage in scenarios involving complex transactions by allowing partial rollbacks to a specific point within a transaction, giving developers and users precise control over the state of a transaction . This capability is particularly useful in large transactions with multiple stages or steps, where reverting entire transactions would be too costly or disruptive. By utilizing SAVEPOINT, changes can be organized into manageable checkpoints, reducing the risk of data errors and improving overall transaction management flexibility . This can be especially beneficial for scenarios demanding iterative execution or testing of transaction logic.

TCL commands such as COMMIT, ROLLBACK, and SAVEPOINT ensure databases adhere to ACID properties (Atomicity, Consistency, Isolation, Durability). COMMIT guarantees durability by permanently saving changes once a transaction is completed . ROLLBACK restores the database to its last consistent state, ensuring atomicity by allowing a transaction to be undone if errors occur . SAVEPOINT enhances isolation by enabling partial reversions within transactions without affecting other operations . These properties are critical for maintaining data integrity and system reliability, preventing partial updates or corruption in high-concurrency environments and ensuring users see only consistent states across transactions.

GRANT and REVOKE commands are integral to managing database security. The GRANT command is used by administrators to provide users with necessary privileges for accessing or manipulating different database objects, thus facilitating the delegation of authority with optional granularity . This can include the ability to SELECT, INSERT, or UPDATE data, or even to grant permissions to others. Conversely, the REVOKE command withdraws these privileges, preventing unauthorized access or modifications when security policies change or when users' roles are adjusted . Together, these commands ensure that only authorized users can perform specific actions, maintaining database confidentiality and integrity.

COMMIT, SAVEPOINT, and ROLLBACK are crucial for managing database transactions consistently and safely. COMMIT permanently saves all changes made in the current transaction, ensuring data integrity by making modifications visible to other users . SAVEPOINT sets a point in a transaction to which you can later rollback, enabling more granular control over transactions by only undoing partial changes if necessary . ROLLBACK restores the database to its last committed state or a specific savepoint, useful in reverting errors without affecting other ongoing operations . In a multi-user environment, these commands are essential to prevent data inconsistencies and ensure database operations are atomic, consistent, isolated, and durable (ACID properties).

The use of RENAME within the ALTER TABLE command is significant for maintaining database clarity and adaptability. Renaming tables and their components can align the database schema with changes in business logic or naming conventions, improving readability and reducing errors from ambiguously named entities . It allows these changes without losing table data or requiring a full-scale database migration. Potential impacts include the need to update any dependent queries, procedures, or applications referencing the old names to avoid breaking integrations, highlighting the importance of comprehensive change management when altering schema names .

DDL commands such as CREATE, ALTER, DROP, and TRUNCATE are used to define or modify the structural schema of the database, directly influencing the database objects themselves (e.g., tables, schemas, indices). DML commands like INSERT, UPDATE, and DELETE manipulate the actual data within the existing database structure, focusing on data entries rather than the schema . The management impact is significant; DDL is crucial during the database setup phase, affecting how efficiently data can be stored and indexed, while DML governs the day-to-day operations, including the insertion and retrieval of data, which affects runtime performance and data integrity.

The SELECT command is the primary SQL statement used for retrieving data from a database. It empowers users to specify exactly which data they need through extensive combinations of columns, conditions, and ordering criteria . SELECT forms the basis of most queries by allowing users to view specific data subsets through precise filters and joins across multiple tables. Its flexible syntax enables complex retrieval requests that can include sorting, aggregating, and filtering functions, making it a versatile tool for any data analysis task .

A database administrator might prefer using TRUNCATE over DELETE when the goal is to quickly remove all records from a table without the need for conditional row evaluation or transaction logging for each row. TRUNCATE is faster as it deallocates all space occupied by the table’s data . This operation does not generate individual row delete triggers; instead, it resets the identity values unless an identity clause is specified, and it cannot be rolled back if it's inside a transaction. This decision implies that data cannot be recovered directly from a rollback once truncated, reflecting a preference for performance over recoverability .

Applying the concept of a relational database to join multiple tables involves using foreign keys to form relationships between tables and using SQL joins to retrieve linked data. For example, if you have tables like 'Employees' and 'Departments' with a foreign key 'Dept_ID' in 'Employees' referencing 'Dept_ID' in 'Departments', you may use: SELECT Employees.Name, Departments.Dept_Name FROM Employees INNER JOIN Departments ON Employees.Dept_ID = Departments.Dept_ID. This query will return each employee's name along with their associated department name, demonstrating a meaningful combination of data from both tables based on relational keys.

You might also like