Unit 3
Unit 3
Unit 3
UNIT-3
Table of Content
DBMS v/s RDBMS
Introduction to SQL: Data types, Constraints
Commands in SQL: Create table, Drop command, Alter Queries in SQL
Statements in SQL (Insert, delete and update)
Features of SQL
Manipulation of data
Tables in SQL
Conclusion of the Unit
DBMS vs RDBMS
Although DBMS and RDBMS both are used to store information in physical database but
there are some remarkable differences between them.
No. DBMS RDBMS
1) DBMS applications store data as file. RDBMS applications store data in a tabular
form.
2) In DBMS, data is generally stored in In RDBMS, the tables have an identifier
either a hierarchical form or a called primary key and the data values are
navigational form. stored in the form of tables.
3) Normalization is not present in DBMS. Normalization is present in RDBMS.
4) DBMS does not apply any security with RDBMS defines the integrity constraint for
regards to data manipulation. the purpose of ACID (Atomocity,
Consistency, Isolation and Durability)
property.
5) DBMS uses file system to store data, so in RDBMS, data values are stored in the
there will be no relation between the form of tables, so a relationship between
tables. these data values will be stored in the form
of a table as well.
6) DBMS has to provide some uniform RDBMS system supports a tabular structure
methods to access the stored information. of the data and a relationship between them
to access the stored information.
4. FOREIGN KEY
• A foreign key is used for referential integrity.
• When we have two tables, and one table takes reference from another 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.
5. CHECK
• Whenever a check constraint is applied to the table's column, and the user wants to
insert the value in it, then the value will first be checked for certain conditions before
inserting the value into that column.
• For example: if we have an age column in a table, then the user will insert any value of
his choice. The user will also enter even a negative value or any other invalid value.
But, if the user has applied check constraint on the age column with the condition age
greater than 18. Then in such cases, even if a user tries to insert an invalid value such as
zero or any other value less than 18, then the age column will not accept that value and
will not allow the user to insert it due to the application of check constraint on the age
column.
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.
Commands in SQL
1. Create
• SQL CREATE TABLE statement is used to create table in a database.
• If you want to create a table, you should name the table and define its column and each
column's data type.
• Let's see the simple syntax to create the table.
create table "tablename"
("column1" "data type", Parameters used in syntax
•table_name: It specifies the name of the
"column2" "data type", table which you want to create.
"column3" "data type", •column1, column2, ... column n: It specifies
... the columns which you want to add in the
table. Every column must have a datatype.
"columnN" "data type");
Oracle CREATE TABLE Example
CREATE TABLE customers
( customer_id number(10),
customer_name varchar2(50),
city varchar2(50)
);
This table contains three columns
• customer_id: It is the first column created as a number datatype (maximum 10 digits
in length) .
• customer_name: it is the second column created as a varchar2 datatype (50
maximum characters in length)
• city: This is the third column created as a varchar2 datatype.
Create table with Column Level Constraints
CREATE TABLE customers
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50)
);
Create table with Table Level Constraints
CREATE TABLE customers
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50),
CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);
In Oracle Database, create table emp(
Dept is Parent table empno number(4,0),
Emp is Child Table ename varchar2(20),
job varchar2(20),
create table dept ( mgr number(4,0),
deptno number(2,0), hiredate date,
dname varchar2(20), sal number(7,2),
loc varchar2(20), comm number(7,2),
constraint pk_dept primary key(deptno) deptno number(2,0),
); constraint p_k primary key(empno),
constraint fk_deptno foreign key(deptno)
references dept(deptno)
);
You can verify it, if you have created the table successfully by looking at the message
displayed by the SQL Server, else you can use DESC command as follows:
DESC customers;
ADD
How to add column in a table
Syntax:
ALTER TABLE table_name ADD column_name column-definition;
Example:
Consider that already existing table customers. Now, add a new column customer_age
into the table customers.
ALTER TABLE customers ADD customer_age varchar2(50);
Now, a new column "customer_age" will be added in customers table.
How to add multiple columns in the existing table
ALTER TABLE customers ADD (customer_type varchar2(50),
customer_address varchar2(50));
MODIFY
How to modify column of a table
ALTER TABLE customers MODIFY customer_name varchar2(100) not null;
RENAME
How to rename column of a table
Syntax:
ALTER TABLE table_name RENAME COLUMN old_name to new_name;
Example
ALTER TABLE customers RENAME COLUMN customer_name to cname;
3. DROP
Oracle DROP TABLE statement is used to remove or delete a table from the Oracle
database.
Syntax
DROP [schema_name].TABLE table_name [ CASCADE CONSTRAINTS ] [ PURGE ];
Parameters
schema_name: It specifies the name of the schema that owns the table.
table_name: It specifies the name of the table which you want to remove from the
Oracle database.
CASCADE CONSTRAINTS: It is optional. If specified, it will drop all referential
integrity constraints as well.
PURGE: It is also optional. If specified, the table and its dependent objects are placed
in the recycle bin and can?t be recovered.
DROP TABLE Example
DROP TABLE customers;
This will drop table customers
Example
1. INSERT INTO customers(customer_id,customer_name,city) values (101,‘ALLEN’,
‘NEW YORK’)
2. INSERT INTO customers(customer_name,city, customer_id,) values (‘ANDREW’,
‘LONDON’,102)
3. INSERT INTO customers values (103, ‘EMMA’, ‘LONDON’)
4. INSERT INTO customers(customer_id, customer_name,) values (104, ‘JOY’)
5. INSERT INTO customers values (105, ‘JIMMY’)
Syntax:
UPDATE table SET column1 = expression1, column2 = expression2, ... column_n = expr
ession_n WHERE conditions;
Example:
UPDATE customers SET customer_name = ’ANNA' WHERE customer_id = 102;
UPDATE customers SET customer_name = ’ANNA’ , city=‘DALLAS’
WHERE customer_id = 102; updating multiple columns
UPDATE customers SET customer_name = ’ANNA’; changes will be made to all records
DELETE Statement
In Oracle, DELETE statement is used to remove or delete a single record or multiple
records from a table.
Syntax: DELETE FROM table_name WHERE conditions;
Parameters
1) table_name: It specifies the table which you want to delete.
2) conditions: It specifies the conditions that must met for the records to be deleted.
Oracle Delete Example: On one condition
DELETE FROM customers WHERE customer_name = ’ANDREW';
Oracle Delete Example: On multiple conditions
DELETE FROM customers WHERE customer_name = ’ANNA' AND customer_id > 101;
DELETE all Records of table
DELETE from customers;
SQL
SQL stands for Structured Query Language and is a computer language that we use to
interact with a relational database.
SQL is a tool for organizing, managing, and retrieving archived data from a computer
database.
The original name was given by IBM as Structured English Query Language,
abbreviated by the acronym SEQUEL.
When data needs to be retrieved from a database, SQL is used to make the request.
The DBMS processes the SQL query retrieves the requested data and returns it to us.
Rather, SQL statements describe how a collection of data should be organized or what
data should be extracted or added to the database.
In common usage, SQL encompasses DDL and DML commands for CREATE,
UPDATE, modified, or other operations on database structure.
Uses of SQL
1. Data definition: It is used to define the structure and organization of the stored data
and the relationships among the stored data items.
2. Data retrieval: SQL can also be used for data retrieval.
3. Data manipulation: If the user wants to add new data, remove data, or modifying in
existing data then SQL provides this facility also.
4. Access control: SQL can be used to restrict a user’s ability to retrieve, add, and modify
data, protecting stored data against unauthorized access.
5. Data sharing: SQL is used to coordinate data sharing by concurrent users, ensuring
that changes made by one user do not inadvertently wipe out changes made at nearly
the same time by another user.
Features of SQL
Flexibility and Scalability: SQL provides users with flexibility and scalability for relational
database management systems. With SQL, it's easier to create new tables while dropping or
deleting previously-created or seldom-used tables.
Comprehensive Application Development Tool: Programmers use SQL to program
applications to access a database, making it a comprehensive and effective application
development tool. SQL is suitable for every large or small organization, regardless of size.
Rich Transactional Support: SQL's programming capability of handling large records while
managing several other transactions is top-notch.
High Performance: SQL offers high-performance programming capabilities for high usage,
incredibly transactional, and heavy workload database systems. SQL's programming provides
different ways to describe data more analytically.
High Availability: SQL is compatible with other databases such as Microsoft SQL Server,
Oracle Database, MS Access, MySQL, SAP Adaptive Server, and more. These RDBMSs
support SQL, and it's easier to create application extensions for procedural programming
and several other SQL functions that are extra features, hence making SQL a strong tool.
High Security: SQL also has high security as one of its notable features. It's easy to give
permissions on views, procedures, and tables. So with SQL, you get optimum security for
your data.
SQL's Management Ease: Almost every Relational Database Management System uses
Structured Query Language. These commands help users manage large amounts of data
from a database efficiently and quickly.
Manipulation of data
The SQL data manipulation language (DML) is used to query and modify database
data. In this chapter, we will describe how to use the SELECT, INSERT, UPDATE,
and DELETE SQL DML command statements, defined below.
SELECT – to query data in the database
INSERT – to insert data into a table
UPDATE – to update data in a table
DELETE – to delete data from a table
Tables in SQL
Create table, Alter Table, Drop Table (Commands in SQL, Slide Number 11-19)
Rename Table
RENAME TABLE allows you to rename an existing table in any schema (except the
schema SYS).
To rename a table, you must either be the database owner or the table owner.
Syntax: RENAME TABLE table-Name TO new-Table-Name
If there is a view or foreign key that references the table, attempts to rename it will
generate an error. In addition, if there are any check constraints or triggers on the table,
attempts to rename it will also generate an error.
Example : RENAME departments_new TO emp_departments;
Truncate Table
In Oracle, TRUNCATE TABLE statement is used to remove all records from a table. It
works same as DELETE statement but without specifying a WHERE clause.
Once a table is truncated, it can’t be rolled back. The TRUNCATE TABLE statement
does not affect any of the table’s indexes, triggers or dependencies.
Syntax: TRUNCATE TABLE table_name
Parameters
1) table_name: It specifies the table that you want to truncate.
Example
TRUNCATE TABLE customers;
Thankyou