DBMS
DBMS
SQL or Structured Query Language is the language that we use to work with
these relational database management systems.
SQL was firstly developed by IBM for querying and altering relational
databases, using declarative statements.SQL became a standard of the
American National Standards Institute (ANSI) in 1986, and of the International
Organization for Standardization (ISO) in 1987. Although it is often called a
“query language”, SQL is much more than that. SQL can define data
structure, modify data in a database, specify security constraints, and can
perform many more tasks such as
Before we delve into the different features offered by SQL, let us understand
what are functions.
Functions are methods used to perform data operations. SQL has many in-
built functions used to perform string concatenations, mathematical
calculations, etc.
SQL functions are categorized into the following categories:
1. Aggregate Functions
2. Scalar Functions
Function Description
SUM() SUM() is used to return the sum of a group of values.
COUNT() Returns the number of rows either based on a condition or without a condition.
AVG() AVG() is used to calculate the average value of a numeric column.
MIN() MIN() function returns the minimum value of a column.
MAX() MAX() function returns a maximum value of a column.
FIRST() FIRST() is used to return the first value of the column.
LAST() LAST() function returns the last value of the column.
SQL Scalar Functions return a single value based on the given input value.
Following are a few of the most commonly used Aggregate Functions
Features of SQL
i) Create command:
If we want to make a new table, the create command is used.
Syntax: for creating a table
iii) Alter:
The alter command is used to modify the structure of a table.
Syntax: to add a new column to a table
Example:
The following SQL adds an “Email” column to the “Customers” table:
iv) Rename:
The rename command is used to change the name of a table.
Syntax: for renaming a table
v) Truncate:
The truncate command is used to delete all the data in a table without
affecting the table structure.
Syntax: for using the truncate command
2. Data Manipulation Language(DML)
i) Select command:
It is a very important command in SQL that is used to select a set of data or
information from a table or simply we can say that it is used to query
information from a table.
The syntax depends upon the requirement because the select command is
used with different conditions and clauses.
So let’s see a basic syntax for displaying all the information in a table:
Note: While using the select command, the table should not be empty.
3. Relational Foundation
4. High-performance
5. Scalability
SQL database is vertically scalable, which means that you can increase the
load on a single server by adding more RAM, SSDs, or CPUs. Due to the way
data is stored (related tables vs unrelated collections), SQL databases
generally support vertical scaling only – horizontal scaling is only possible for
NoSQL databases.
6. Security and authentication
7. Vendor Independence
With SQL support available from all the major DBMS vendors, no new DBMS
product has been highly successful over the last decade. It’s possible to
migrate SQL-based databases and programs from one DBMS to another
vendor’s DBMS with minimal conversion effort. Thus vendor independence is
one of the most important features of SQL and an important reason for its
early popularity.
Researchers at IBM created SQL, which has since become a strategic product
built on IBM’s flagship DB2 database. From personal computers to midrange
systems to IBM mainframes, SQL is supported on all major IBM products.
With IBM’s initial work, other database and system vendors followed IBM’s
lead early on in the development of SQL and relational databases, and as a
result of IBM’s wide support and commitment, SQL was accepted more
quickly by the market. In addition to IBM’s products, the SQL-based products
it has developed run on hardware from emerging competitors such as HP and
Sun.
Commit command:
The commit command is used to save the changes.
The syntax for commit command:
Rollback command:
The rollback command is used to roll back the changes to the previous
transaction.
The syntax for rollback command:
Savepoint command:
The savepoint command is used when we want to go back to the previous
transaction without going back to the entire transaction.
The syntax for savepoint command:
With SQL, the creator of a database can give different users different views of
the database’s structure and content. For eg:-An organization’s database can
be constructed in such a way that each user can access only his or her
department’s data. Additionally, data from different tables in the database can
be combined and displayed as a simple row/column table for the user.
14. Dynamic
One of the major advantages of SQL over other static databases is that, even
while users are accessing database content, it is possible to change and
expand a database’s structure dynamically. As a result, SQL provides
maximum flexibility, allowing online applications to continue uninterrupted
while a database can adapt to changing requirements.
In recent years, the integration of SQL with Java has been a major area of
SQL development. Sun Microsystems (the developer of JAVA) introduced
Java Database Connectivity JDBC(a standard API that allows Java programs
to use SQL for database access), to link the Java language to existing
relational databases. It ensured that SQL was still relevant in the new era of
Java-based programming.
SQL Versions
MySQL
What is Database?
It is very important to understand the database before learning MySQL. A database is
an application that stores the organized collection of records. It can be accessed and
manage by the user very easily. It allows us to organize data into tables, rows, columns,
and indexes to find the relevant information very quickly. Each database contains
distinct API for performing database operations such as creating, managing, accessing,
and searching the data it stores. Today, many databases available like MySQL,
Sybase, Oracle, MongoDB, PostgreSQL, SQL Server, etc. In this section, we are going
to focus on MySQL mainly.
What is MySQL?
MySQL is currently the most popular database management system software used for
managing the relational database. It is open-source database software, which is
supported by Oracle Company. It is fast, scalable, and easy to use database
management system in comparison with Microsoft SQL Server and Oracle Database. It
is commonly used in conjunction with PHP scripts for creating powerful and dynamic
server-side or web-based enterprise applications.
It is developed, marketed, and supported by MySQL AB, a Swedish company, and
written in C programming language and C++ programming language. The official
pronunciation of MySQL is not the My Sequel; it is My Ess Que Ell. However, you can
pronounce it in your way. Many small and big companies use MySQL. MySQL supports
many Operating Systems like Windows, Linux, MacOS, etc. with C, C++, and Java
languages.
The core of the MySQL database is the MySQL Server. This server is available as a
separate program and responsible for handling all the database instructions,
statements, or commands. The working of MySQL database with MySQL Server are as
follows:
1. MySQL creates a database that allows you to build many tables to store and
manipulate data and defining the relationship between each table.
2. Clients make requests through the GUI screen or command prompt by using
specific SQL expressions on MySQL.
3. Finally, the server application will respond with the requested expressions and
produce the desired result on the client-side.
A client can use any MySQL GUI. But, it is making sure that your GUI should be lighter
and user-friendly to make your data management activities faster and easier. Some of
the most widely used MySQL GUIs are MySQL Workbench, SequelPro, DBVisualizer,
and the Navicat DB Admin Tool. Some GUIs are commercial, while some are free with
limited functionality, and some are only compatible with MacOS. Thus, you can choose
the GUI according to your needs.
Advantages
MySQL is becoming so popular because of these following reasons:
o DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc.
o All the command of DDL are auto-committed that means it permanently save
all the changes in the database.
c. ALTER: It is used to alter the structure of the database. This change could be either
to modify the characteristics of an existing attribute or probably to add a new attribute.
Syntax:
o DML commands are used to modify the database. It is responsible for all form of
changes in the database.
o The command of DML is not auto-committed that means it can't permanently save all
the changes in the database. They can be rollback.
o INSERT
o UPDATE
o DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row
of a table.
Syntax:
b. UPDATE: This command is used to update or modify the value of a column in the table.
c. DELETE: It is used to remove one or more row from a table.
o Grant
o Revoke
o COMMIT
o ROLLBACK
o SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
b. Rollback: Rollback command is used to undo transactions that have not already been
saved to the database.
c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back
the entire transaction.
5. Data Query Language
DQL is used to fetch the data from the database.
o SELECT
a. SELECT: This is the same as the projection operation of relational algebra. It is used
to select the attribute based on the condition described by WHERE clause.
Data Types in SQL:
The following are the most common data types of SQL:
SQL Operators:
SQL Arithmetic Operators:
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
% Modulo
UPDATE:
• SQL provides the ability to change data through UPDATE command.
• The UPDATE command used to modify or update an already existing row or rows of a
table.
• The basic syntax of UPDATE command is given below.
UPDATE Table_name
SET
column_name = value
[, column_name =value ……….]
[WHERE condition];
DELETE command:
• In SQL, an already existing row or rows are removed from tables through the use of
DELETE command.
• The basic syntax of DELETE command is given below.
DELETE Table_name
[WHERE condition];
Example:
SQL> DELETE STUDENT WHERE REGNO=1412;
1 row deleted.
SELECT:
• SQL SELECT statement is used to fetch the data from a database table which returns data
in the form of result table. These result tables are called result-sets.
• Syntax: The basic syntax of SELECT statement is as follows:
SELECT column1, column2, columnN
FROM Table_name; Compulsory
Part
[WHERE condition(s)]
[GROUPBY column-list]
[HAVING condition(s)]
[ORDER BY column-name(s)];
TASK - 3
Step 3: After step 2 choose the preferred download option as per the specification
of your device (32bit or 64bit).
Step 4: Now to proceed download sign in with your oracle account.
Step 7: After the completion of extraction of file open it and copy the folder
sqldeveloper.
Step 8: Now paste the copied folder inside the program files folder of C drive.
Step 9: Now wait until the folder is being copied at the specified location.
Step 10: Now open the sqldeveloper.exe file from the sqldeveloper folder.
TASK-4
Create tables with all required constraints as per the ER diagram above, insert 5 records each in 3
tables, write atleast 3 conditional updates, useselect command to display data of all 3 tables.
create table student(rollNo int primary key,
firstName varchar(25),
lastName varchar(20),
grade varchar(3));
create table course(CId varchar(10) primary key,
cName varchar(10),
duration int check(duration<6),
rollNo int, foreign key(rollNo) references student(rollNo));
create table login(UserId int primary key,
password varchar(25),
foreign key(UserId) references student(rollNo));
insert into student values(1, "Aman", "jha", "A++");
insert into student values(2, "priti", "chy", "A");
insert into student values(3, "sonu", "Sharma", "A+");
insert into student values(4, "Ankit", "Kumar", "A");
insert into student values(5, "ruhi", "jha", "B");
insert into course values("101", "ME ", "4", 1);
insert into course values("102", "CSE", "4",2);
insert into course values("103", "SE ", "3", 3);
insert into course values("104", "COA","3", 4);
insert into course values("105", "BCA", "2", 5);
insert into login values(1, "AMAN101");
insert into login values(2, "PRITI102");
insert into login values(3, "SONU103");
insert into login values(4, "ANKIT104");
insert into login values(5, "RUHI105");
update login set password ="AMAN105" where UserId=1;
update login set password="PRITI107 " where UserId=2;
update login set password="RUHI108 " where UserId=5;
Select operation;
Select operations:
Select * from student;
Select * from course;
TASK-5
1. Display order no. of all orders placed by aman or any other customer
select ORDERS.Order_No,CUSTOMER.First_Name from ORDERS, CUSTOMER where
ORDERS.C_ID=CUSTOMER.C_ID and CUSTOMER.First_Name = 'Aman' ;
2. Display the name of the customer who placed ordered more then 5
quantities of a given productno.
select CUSTOMER.First_Name,ORDERS.Order_No,
ORDER_FOR_STOCK.Qty_Order from CUSTOMER,
ORDERS,ORDER_FOR_STOCK where CUSTOMER.C_ID = ORDERS.C_ID and
ORDER_FOR_STOCK.Order_No=ORDERS.Order_No and ORDER_FOR_STOCK.Qty_Order>5;