Relational Database 2
Relational Database 2
Query
Language
Database changed
mysql> select * from company;
+------+-------+
| SID | SALES |
+------+-------+
| S101 | 20000 |
| S103 | NULL |
| S104 | 10000 |
| S105 | 15000 |
+------+-------+
4 rows in set (0.31 sec)
i) Querying data
ii) Inserting, updating, and deleting rows in a table
iii) Creating, replacing, altering, and dropping
objects (tables)
iv) Controlling access to the database and its objects
(tables)
v) Guaranteeing database consistency and integrity
SQL unifies all of the preceding tasks in one
consistent language.
Advantages of using SQL:
1) SQL is portable: SQL is running in all servers, mainframes,
PCs, laptops, and even mobile phones.
2) High speed: SQL queries can be used to retrieve large
amounts of records from a database quickly and efficiently.
3) Easy to learn and understand: SQL generally consists of
English statements and as such, it is very easy to learn and
understand. Besides, it does not require much coding unlike
in programming languages.
4) SQL is used with any DBMS system with any vendor: SQL
is used by all the vendors who develop DBMS. It is also used
to create databases, manage security for a database, etc. It
can also be used for updating, retrieving and sharing data
with users.
Advantages of using SQL:
5) SQL is used for relational databases: SQL is widely used
for relational databases.
6) SQL acts as both programming language and interactive
language: SQL can do both the jobs of being a programming
language as well as an interactive language at the same time.
7) Client/Server language: SQL is used for linking front end
computers and back end databases. It provides client server
architecture (Email, and the World Wide Web - all apply the
client-server architecture).
8) Supports object based programming: SQL supports the
latest object based programming and is highly flexible.
Types of SQL Statements
The SQL statements are categorized into different
categories based upon the purpose. They are;
i) Data Definition Language (DDL) statement
ii) Data Manipulation Language (DML) statement
iii) Transaction Control Statement
iv) Session Control Statement
v) System Control Statement
vi) Embedded SQL Statement
The values shown as stored in the last row of the table apply only
when not using strict SQL mode; if strict mode is enabled, values
that exceed the column length are not stored, and an error results.
VARCHAR2:
(Used in Oracle SQL)
• VARCHAR2 is used to store variable length
character strings. The string value's length will be
stored on disk with the value itself. VARCHAR2 can
store up to 4000 bytes of characters. Thus, the
difference between VARCHAR and VARCHAR2 is
that VARCHAR is ANSI standard but takes up space
for variables, whereas the VARCHAR2 is used only
in Oracle but makes more efficient use of space.
• Format: VARCHAR2 (n)
Examples:
• CHAR(10) has fixed length, right padded with spaces.
• VARCHAR(10) has fixed length, right padded with NULL
• VARCHAR2(10) has variable length.
• Name char (10): Suppose if we store Name is as "Ravi",
then first four places of the ten characters are filled with
Ravi and the remaining 6 spaces are also allocated to
Name. Thus, the size of name is always ten.
• Name varchar (10): Suppose if we store Name is as "Ravi",
then first four places of the ten characters are filled with
Ravi and the remaining 6 spaces are filled with NULL.
• Name varchar2 (10): Suppose if we store Name is as "Ravi",
then only first four places are filled with Ravi.
The following table gives possible string data
types used in different DBMS
Numeric data type:
• Numeric data types are mainly used to store
number with or without fraction part.
1. NUMBER
2. DECIMAL
3. NUMERIC
4. INT
5. FLOAT
NUMBER:
The Number data type stores fixed and floating-point
numbers. The Number data type is used to store integers
(negative, positive, floating) of up to 38 digits of precision.
• Format: DATE
Q: Answer the following questions:
1. Write two types of statements used in SQL.
2. Write two examples for each statement given in
question 1.
3. Write any four advantages of SQL.
4. Write four tasks which can be performed using SQL.
5. What are three main data types used in SQL? Write
format for each data type.
Q: Answer the following questions:
1. Write two main statements used in SQL. (DDL & DML)
2. Write two examples for each statement given in
question 1. (Some of the DDL statements are CREATE
TABLE, ALTER TABLE and DROP TABLE & The common
DML statements are SELECT, UPDATE, DELETE and
INSERT.)
3. Write any four advantages of SQL.
4. Write four tasks which can be performed using SQL.
5. What are three main data types used in SQL? Write
format for each data type. (In SQL there are three main
data types: Character, Number and Date types.)
Q: Which data type you will use for the following
attributes/fields/records in table Hospital
TABLE : HOSPITAL
No Name Age Department Datofadm Charges Gender
1 Sandeep 65 Surgery 23/02/98 300 M
2 Ravina 24 Orthopedic 20/01/98 200 F
3 Karan 45 Orthopedic 19/02/98 200 M
4 Tarun 12 Surgery 01/01/98 300 M
5 Zubin 36 ENT 12/02/98 250 M
6 Ketaki 16 ENT 24/02/98 300 F
7 Ankita 29 Cardiology 20/02/98 800 F
8 Zareen 45 Gynecology 22/02/98 300 F
9 Kush 19 Cardiology 13/01/98 800 M
10 Shaliya 31 Nuclear 19/02/98 400 M
Medicine
Write a statement to create the table Hospital
mysql> CREATE TABLE HOSPITAL (No INT(3), Name VARCHAR(10),
Age INT(3), Department VARCHAR(15), DateOfAdm DATE, Charges
DECIMAL(7,2),Gender CHAR(1));
Query OK, 0 rows affected, 2 warnings (0.56 sec)
Q: Which data type you will use for the following
attributes/fields/records in Table: STOCK
Table: STOCK
ItemNo Item Dcode Qty UnitPrice StockDate
5005 Ball Pen 0.5 102 100 16.00 31-Mar-10
5003 Ball Pen 0.25 102 150 20.00 01-Jan-10
5002 Gel Pen Premium 101 125 14.00 14-Feb-10
5006 Gel Pen Classic 101 200 22.50 01-Jan-09
5001 Eraser Small 102 210 5.75 19-Mar-09
5004 Eraser Big 102 60 10.50 12-Dec-09
5009 Sharpener Classic 103 160 8.00 23-Jan-09
NU
Structure of the DEPT Table
Constraints
In SQL, we have the following constraints:
• NOT NULL - To check a column cannot store NULL value.
• PRIMARY KEY - To check that a column has an unique identity which
helps to find a particular record in a table. It can’t have NULL values
• Unique: To check that a column has unique values. It can have NULL
values.
https://www.w3schools.com/mysql/mysql_constraints.asp
CREATE TABLE Syntax:
CREATE TABLE<table name>
(<column name1> <data type> [size] [ constraints
],
<column name2> <data type>[size] [constraints],
.
.
.
<column name n> <data type>[size] [constraints]);
Example: Create the following Table Classlist
Column Name Data Type Size Constraints
ROLL int 2 PRI
NAME varchar 15
DOB date
SUB1 iNT 3
SUB12 INT 3
SUB13 INT 3
AVG Decimal 5,2
MOBILE varchar 10
EMIRATES varchar 10
Command to Create Table CLASSLIST
Arithmetic operators:
Arithmetic operator takes two operands and
performs a mathematical calculation on them.
However, they can be used only in SELECT
command. The arithmetic operators used in SQL
are:
+ Addition
- Subtraction
* Multiplication
/ Division
Write query to add sub1, sub2 and sub3 marks and
displaying it under heading Total
Adding sub1, sub2 and sub3 marks and displaying it as Total
Syntax:
ALTER TABLE <table name> [ADD/MODIFY/DROP] <column name>;
For example:
1. Add one new column totalfees with number (10, 2).
ALTER TABLE student ADD totalfees number(10,2);
ALTER TABLE table_name ADD column_name datatype --add column
Syntax:
DROP TABLE<table name>;
For example:
Remove the whole structure of MYTABLE table.
DROP TABLE MYTABLE;
SQL DROP CONSTRAINT Keyword
DROP CONSTRAINT
The DROP CONSTRAINT command is used to delete
a UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK
constraint.
DROP a PRIMARY KEY Constraint
To drop a PRIMARY KEY constraint, use the following
SQL:
SQL Server / Oracle / MS Access:
ALTER TABLE Persons DROP CONSTRAINT PK_Person;
MySQL:
ALTER TABLE Persons DROP PRIMARY KEY;
Write a query to drop the primary key
from table classlist.
Write a query to drop the primary key
from table classlist.
mysql> ALTER TABLE classlist DROP PRIMARY KEY;
SQL ADD CONSTRAINT Keyword
ADD CONSTRAINT
The ADD CONSTRAINT command is used to create a
constraint after a table is already created.
The following SQL adds a constraint named
"PK_Person" that is a PRIMARY KEY constraint on
multiple columns (ID and LastName):
Example
ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY
(ID,LastName);
Write a query to ADD constraint to make
ROLL as primary key in table classlist.
Write a query to ADD constraint to make
ROLL as primary key in table classlist.
mysql> ALTER TABLE classlist ADD CONSTRAINT
PRIMARY KEY (ROLL);
OR
mysql> ALTER TABLE classlist ADD CONSTRAINT
PK_ROLL PRIMARY KEY (ROLL);
Write a statement to change column name activity to
Game in table classlist.
Write a query to perform Equi-Join operation on
Tables CUST and BAL to display the name of the
customer and balance.
Write a query to perform Equi-Join operation on
Tables CUST and BAL to display the name of the
customer and balance.
mysql> SELECT NAME, BALANCE FROM CUST, BAL WHERE
CUST.ACCT=BAL.ACCT;
For example:
ALTER TABLE supplier ADD supplier_name varchar2(50);
This will add a column called supplier_name to the
supplier table.
SQL ALTER TABLE
Adding column(s) to a table
Syntax #2
To add multiple columns to an existing table, the SQL
ALTER TABLE syntax is:
ALTER TABLE table_name ADD (column_1 column-
definition, column_2 column-definition, ... column_n
column_definition);
For example:
ALTER TABLE supplier ADD (supplier_name varchar2(50),
city varchar2(45));
This will add two columns (supplier_name and city) to
the supplier table.
ALTER TABLE
Modifying column(s) in a table
Syntax #1
To modify a column in an existing table, the SQL ALTER
TABLE syntax is:
ALTER TABLE table_name MODIFY column_name
column_type;
For example:
ALTER TABLE supplier MODIFY supplier_name
varchar2(100) not null;
This will modify the column called supplier_name to be
a data type of varchar2(100) and force the column to not
allow null values.
ALTER TABLE
Modifying column(s) in a table
Syntax #2
To modify multiple columns in an existing table, the SQL
ALTER TABLE syntax is:
ALTER TABLE table_name MODIFY (column_1
column_type, column_2 column_type, ... column_n
column_type);
For example:
ALTER TABLE supplier MODIFY (supplier_name
varchar2(100) not null, city varchar2(75));
This will modify both the supplier_name and city
columns.
SQL ALTER TABLE
Drop column(s) in a table
Syntax
To drop a column in an existing table, the SQL ALTER
TABLE syntax is:
ALTER TABLE table_name DROP COLUMN
column_name;
For example:
ALTER TABLE supplier DROP COLUMN
supplier_name;
This will drop the column called supplier_name
from the table called supplier.
SQL ALTER TABLE
Rename column(s) in a table
(NEW in Oracle 9i Release 2)
Syntax #1
Starting in Oracle 9i Release 2, you can now rename a
column. To rename a column in an existing table, the
SQL ALTER TABLE syntax is:
ALTER TABLE table_name RENAME COLUMN old_name
to new_name;
For example:
ALTER TABLE supplier RENAME COLUMN
supplier_name to sname;
This will rename the column called supplier_name to
sname.
SQL: SELECT Statement
The SQL SELECT statement allows you to
retrieve records from one or more tables in
your SQL database.
You can also use the SQL SELECT statement to select individual
fields from the table, as opposed to all fields from the table.
For example:
SELECT name, city, state FROM suppliers WHERE supplier_id > 1000;
This SQL SELECT statement would return only the name, city, and
state fields from the suppliers table where the supplier_id value is
greater than 1000.
SQL: SELECT Statement
SQL SELECT Statement - Select fields from multiple tables
example
You can also use the SQL SELECT statement to retrieve fields
from multiple tables.
For example:
INSERT INTO suppliers (supplier_id, supplier_name)
VALUES (24553, 'IBM');