0% found this document useful (0 votes)
43 views

Unit 1

Uploaded by

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

Unit 1

Uploaded by

riddhima.thanki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Subject Code: 4330702

Unit -1
Presented By:
Disha Prajapati
Introduction to Database System and SQL commands

1.1 Concepts and Definitions: Database and database systems


and database environment
 What is Database?
• "A database is a collection of inter-related data."
• It represents some aspects of the real world.
• For example, consider a collection of customer id, name, address, and contact number for some bank customers. All
the data, such as id, name, address, and contact number, are interrelated here. They all belong to some particular
customer. In other words, they represent some particular Customer.
• Also, data about accounts, loans, employees, and branches are interrelated in the context of some banking system. So,
this data, combined, forms a bank database.
• A database is some collection of data. However, any random data collection is not considered
• A database is some collection of data. However, any random data collection is not considered a database. For example,
the collection of some vehicle numbers, account balances. and student percentages does not form a database.
 What is Database Management System

• "A database management system is a collection of inter-related data and a set of programs to manipulate those
data."
• Data manipulation involves various operations such as storing, modifying, removing and retrieving data.
• In other words, DBMS can be described as -
• DBMS = Database + A set of Programs
Where a Database is referred to as DB and a set of programs is referred to as MS (Management System).
 Database Environment
• A database environment is a collective system of components that comprise and regulates the group of data,
management, and use of data, which consist of software, hardware, people , techniques of handling database,
and the data also.
 Data, Information, Data Item or Fields, Records, Files, Metadata, Data dictionary and it’s components
• Data
o “DATA is a raw fact, anything can be data”.
o It is not be meaningful.
o It is used analysis and reasoning.
o It is input processing unit.
o Data is basic row material which taken by certain observation, certain experiment and storing in paper, stored
memory, human mind.
o Ex:- marks of student ,account information

• INFORMATION
o This is a processed form of the data.
o It is always meaningful.
o “Meaningful data is called information”.
o It is output processing units.
o Whenever we organize the data and process is done on it then we get proper information.
• Data Item or Fields:
o A field is a character or group of characters that have a specific meaning.
o It is also called a data item. It is represented in the database by a value.
o For Example, customer id, name, society and city are all fields for customer Data.

• Records:
o A record is a collection of logically related fields. For examples, collection of fields (id, name, society & city)
forms a record for customer.

• Files:
o A group of related records. Files are frequently classified by the application for which they are primarily
used (employee file).
• Metadata:
o Metadata is data about data.
o Data such as table name, column name, data type, authorized user and user access zprivileges for any
table is called metadata for that table.
• DATA DICTIONARY
o A data dictionary or data directory is a file that contains metadata. A data dictionary is usually a part of the
system catalog generated for each database
o Data dictionary stores and manages information such as Description of the schema, Detailed information on
physical database design, Descriptions of the database transactions, Relationships among entities

 Components of data dictionary

o Entities: It is represents any item about which information is stored. For example, any particular
customer, say 'Riya', or some particular account, say 'A01, can be considered an entity.
o Attributes : An attribute is a property or characteristic of un entity. For example, customer id, name,
address, and contact numbers are attributes of the entity customer Similarly, account number,
balance, und branch name are attributes of an entity account.
o Relationships : A relationship is an association or connection among entities. For example, an
association between some account, say 'AO1, and a branch, say Vadodara’ i referred to as a
relationship.
o Key :A data item or a field, which is used to identify u record in a database, is referred to as a key.
o Primary key
o Concatenated key
 Active and Passive Data Dictionaries
o Active Data Dictionary : If a data dictionary is managed automatically by database management software, it
is called an active data dictionary.
o Passive Data Dictionary : If the user of the system manages a data dictionary, it is called a passive data
dictionary
 Schemas, Sub-schemas, and Instances
• Schemas :
o The plan or formulation of database is known as schema.
o Schema gives the names of the entities and attributes. It specifies relationship among them.
o Schema includes the definition of database name, record type, components that make up records.
o Schema can be categorized in two ways:
o Logical schema (Table, View)
o Physical Schema (Secondary storage)
• Sub-schemas :
o It is a subset of schema and inherits the same property that schema has.
• Instances :
o The contents of the database at any point of time (or current state), it is referred to as INSATANCE of
database
Differences between database schema and instance

Schema Instance

The overall logical design of the The collection of The collection of information stored
Definition information stored in database. in database at a particular moment.
Table names, Column names, data types and size of Actual data or information is stored in
Includes columns, and constraints at a logical level. tables in the form of different
records/rows

Change Changes infrequently Changes frequently


Insertion/deletion of tables or columns; change in Insert, delete or update operation on
Case of change data type, size or constraint on any column. data stored in a database.

Analog Variable declaration [ int n: ] Value of the variable.


Database Language commands:
1. Data Definition Language (DDL):
o CREATE, ALTER, TRUNCATE, DROP

❖ Create: It used to create table.


• Syntax:
 CREATE TABLE TableName (column1 datatype (size), column2 datatype(size)...Column
datatype(size));
• Example:
 CREATE TABLE student(Rollno number(15),Name varchar2(20),Age number(5),DOB date);

• Output: Table created.


❖ To describe a table use command DESCRIBE,
Syntax:
DESCRIBE TableName;
OR
DESC TableName;
Example: DESC student;
Output:
Name ? Null Type
------------------------------------------------------------------------------------------------------
Rollno NUMBER (15)
Name VARCHAR (20)
Age NUMBER (5)
DOB DATE
❖ ALTER: It used to modify table structure.
A. Adding New Columns
Syntax:
Alter Table TableName Add (NewColumnName1 Datatype (size), NewColumnName2 Datatype
(size)…. NewColumnNameN Datatype (size));
Example:
ALTER TABLE student ADD (marks number (15)); //Add column marks
Output: Table altered.
B. Modifying Existing Columns
Syntax:
Alter Table TableName Modify (ColumnName1 NewDatatype (NewSize), ColumnName2
NewDatatype (NewSize)……ColumnNameN NewDatatype (NewSize));
Example: ALTER TABLE student Modify (marks number (20)); // Change size of column
Output: Table altered
C. Dropping(deleting) Existing Columns
Syntax: Alter Table TableName DROP CLOUMN ColumnName1, ColumnName2;
Example: ALTER TABLE student DROP COLUMN marks; // Remove marks column
Output: Table altered.
❖ DROP: It used to delete or destroy table from a database.
Syntax: DROP TABLE TableName
Example: DROP TABLE student;
Output: Table Dropped.

❖ TRUNCATE:
• TRUNCATE TABLE used to delete all data from a table
• Logically, this is equivalent to DELETE statement that deletes all rows
• TRUNCATE command is faster than DELETE command
• The number of deleted rows are not returned
Syntax: TRUNCATE TABLE TableName;
Example: TRUNCATE TABLE student;
Output: Table Truncated.
2. Data Manipulation Language (DML):

❖ DML is a set of SQL Commands used to insert, modify and delete data in a database.
❖ These SQL commands are used for storing, retrieving, modifying, and deleting data.
❖ It is normally used by general users who are accessing database via pre-developed
applications.
❖ These Data Manipulation Language commands are:
1) INSERT
2) UPDATE
3) DELETE
4) SELECT
1) SQL INSERT Statement
❖ The INSERT Statement is used to add new rows of data to a table.
• Syntax:
INSERT INTO TableName(ColumnName1, ColumnName2…ColumnNameN) values(Expression1,Expression2….ExpressionN);
• Example:
INSERT INTO Student (Sno, Sname, age, Branch) values (1,’Niyati’, 17,’CE’);
• Output: 1 row inserted
❖ We can insert NULL value in Table Using INSERT Statement.
Example:
INSERT INTO Student (Sno, Sname, age, Branch) values (1,’Niyati’, 17, NULL);
Output: 1 row inserted.
2) SQL UPDATE Statement
❖ The UPDATE Statement is used to modify the existing rows in a table.
Syntax:
UPDATE TableName SET Column_Name1 = value1, Column_Name2 = value2 WHEREcondition;
Example:
UPDATE Student SET Name=’nilam’ WHERE Rollno=1;
Output: 1 row Updated.
NOTE:
❖ In the Update statement, WHERE clause identifies the rows that get affected.
❖ If you do not include the WHERE clause, column values for all the rows get affected.
3) SQL Delete Statement
❖ The DELETE Statement is used to delete rows from a table.
Syntax:
DELETE FROM TableName WHERE condition;
Example:
❖ To delete an employee with id 100 from the employee table, the SQL delete query would be like,
DELETE FROM Student WHERE Rollno = 100;

❖ To delete all the rows from the employee table, the query would be like,
DELETE FROM Student;
DELETE FROM Student;
4) SQL SELECT Statement
❖ The SELECT command is used to retrieve selected rows from the Tables.
Syntax:
a. SELECT * FROM TableName;
b. SELECT ColumnName1, ColumnName2… ColumnNameN From TableName;
c. SELECT * FROM TableName WHERE Condition;
d. SELECT ColumnName1, ColumnName2… ColumnNameN From TableName WHERE Condition;
Example:
a. SELECT * FROM student;
b. SELECT Rollno, Name FROM student;
c. SELECT * FROM student where Rollno=101;
d. SELECT Rollno, Name FROM student where Rollno=101;
3. Transactional Control (TCL):
A Transaction is a set of database operations that performs a particular task.
❖ A transaction must be completely successful or completely fail without doing anything to maintain database
consistency.
❖ Example, Successfully Complete Transaction: A task, say fund transfer from one account to another account.
❖ To complete a transaction needs to update balance in two accounts – source and
destination.
❖ It requires two database operations: one, to debit an amount in source account and to credit that amount in
destination account.
❖ Example, Fail Transaction: In a fund transfer, if a system failure occurs after debiting amount from source account,
but before crediting it to destination account then database will be in inconsistent state.
❖ So, balance should be updated in both the accounts in both accounts or not in anyone.
❖ We can say that a transaction is considered as a sequence of database operations.
❖ These operations involve various data manipulation operations such as insert, update and delete.
❖ These operations are performed in two steps:
o Changes are made in memory only.
o Changes are permanently saved to hard disk.
❖ A transaction begins with the execution of first SQL statement after a COMMIT and can
be undone using ROLLBACK command.
❖ A transaction can be closed by using COMMIT or ROLLBACK command. When a
transaction is closed, all the locks acquired during that transaction are released.
❖ TCL commands are used to manage transactions, that are given below:
o Commit
o Rollback
o Savepoint
COMMIT: Committing a Transaction
❖ There are two ways to commit a transaction:
o Explicitly
o Implicitly
Explicit Commit:
❖ To commit a transaction explicitly, user needs to request COMMIT command explicitly.
❖ A COMMIT command terminates the current transaction and makes all the changes permanent.
❖ Various data manipulation operations such as insert, update and delete are not effect permanently until they are
committed.
❖ Syntax:
COMMIT;
❖ Output:
Commit complete
Implicit Commit:
❖ There are some operations which forces a COMMIT to occur automatically, even user
don’t specify the COMMIT command.
❖ Some of commands are given below:
Quit Command:
To end SQL*PLUS session disconnecting from the Oracle.
Exit Command:
To end SQL*PLUS session disconnecting from the Oracle

ROLLBACK: Canceling a Transaction Completely


❖ A transaction can be cancelled using ROLLBACK command either completely or partially.
❖ A ROLLBACK command terminates the current transaction and undone any changes
made during the transaction.
❖ Oracle also performs auto rollback. In situation like, Computer failure, Oracle automatically rollbacks any
uncommitted work, when the database bought back next time.
SAVEPOINT: Cancelling a Transaction Partially
❖ A ROLLBACK command can also used to terminate the current transaction partially.
❖ Syntax:
ROLLBACK TO SAVEPOINT savepoint_name;
❖ Output:
Rollback Complete.
❖ It is required to create a savepoint to cancel transaction partially.
❖ A savepoint marks and save the current point in the processing of a transaction.
❖ A savepoint can be created using command SAVEPOINT as given below:
❖ Syntax:
SAVEPOINT savepoint_name;
❖ Output:
Savepoint created.
❖ When a ROLLBACK is used with SAVEPOINT, part of the transaction is cancelled.
❖ All the operations performed after creating a savepoint are undone.
❖ It is also possible to create more than one savepoint within a single transaction.
4. Data Control Language (DCL): Grant and Revoke
GRANT – Granting Privileges
❖ GRANT command is used to granting privileges means to give permission to some user to access database object
or a part of a database object.
❖ This command provides various types of access to database object such as tables, views and sequences.
Syntax:
GRANT object privileges
ON object name
TO user name
[ WITH GRANT OPTION ];
❖ The owner of a database object can grant all privileges or specific privileges to other users.
❖ The WITH GRANT OPTION allows the grantee. User to which privilege is granted to in turn grant object
privilege to other users.
❖ User can grant all or specific privileges owned by him/her.
Example:
GRANT ALL
ON Customer
TO user2
WITH GRANT OPTION;
Output:
Grant Succeeded.
❖ Observe the use of WITH GRANT OPTION. If this option is not specified, user2 will have privileges, but he
cannot grant these privileges to other users.
REVOKE – Revoking Privileges
❖ Revoking privileges means to deny (decline) permission to user given previously.
❖ The owner on an object can revoke privileges granted to another user. A user of the
object, who is not an owner, but has been granted privileges using WITH GRANT
OPTION, can revoke the privilege from the grantee.
Syntax: REVOKE object privileges
ON object name FROM user name;
Example:
Revoke SELECT, INSERT
ON Customer
FROM user2;
GRANT REVOKE

This DCL command grants permissions to the user on the This DCL command removes permissions if any granted to
database objects. the users on database objects.

It assigns access rights to users. It revokes the user access rights of users.

For each user you need to specify the permissions If access for one user is removed; all the particular
permissions provided by users to others will be removed.

When the access is decentralized granting permissions If decentralized access removing the granted permissions
will be easy. is difficult.

You might also like