CHAPTER - 1
QUERYING AND SQL
FUNCTIONS
RECAP…..
• What is SQL?
• RDBMS
• DB and table
What is SQL?
• SQL stands for Structured Query Language
• It is used to interact with a relational databases.
• It tool for organizing, managing, and retrieving archived data
from a computer database.
• The original name was given by IBM as Structured English
Query Language(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.
• SQL statements describe how a collection of data should be
organized or what data should be extracted or added to the
database.
IP
RDBMS
What is DBMS?
A database is an organized collection of data stored in a
computer system and usually controlled by a database management
system (DBMS).
What is RDBMS?
• RDBMS stands for Relational Database Management Systems.
• A program that allows us to create, delete, and update a relational
database.
• A Relational Database is a database system that stores and
retrieves data in a tabular format organized in the form of rows and
columns.
• Relational DBMS owes its foundation to the fact that the values of
each table are related to others.
• It has the capability to handle larger magnitudes of data and
simulate queries easily.
IP
Characteristics of RDBMS:
• Data must be stored in tabular form in DB file(in the form of rows and
columns).
• Each row of table is called record/tuple . Collection of such records is known
as the cardinality of the table
• Each column of the table is called an attribute/field. Collection of such
columns is called the arity of the table.
• No two records of the DB table can be same. Data duplicity is therefore
avoided by using a candidate key. Candidate Key is a minimum set of
attributes required to identify each record uniquely.
• Tables are related to each other with the help for foreign keys.
• Database tables also allow NULL values, that is if the values of any of the
element of the table are not filled or are missing, it becomes a NULL value,
which is not equivalent to zero.
• Primary key cannot have a NULL value.
Ex: SQL, My-SQL, and ORACLE
DB and Table:
DB and Table:
DB and Table:
IP
MYSQL QUERIES – A RECAP
1) CREATE DATABASE:
You can connect to an existing database or, create your own.
Syntax:
CREATE DATABASE database_name;
Example:
CREATE DATABASE student;
Once a database is created, you can check it in the list of databases as
follows −
show databases;
Output:
The above query produces the following output −
Database
information_schema
student
performance_schema
world
If you try to create a database with an existing name as −
CREATE DATABASE student;
An error will be generated as −
ERROR 2006 (HY000): MySQL server has gone away MySQL CREATE
DATABASE Statement No connection. Trying to reconnect... Connection id: 10
Current database: sampledb ERROR 1007 (HY000): Can't create database
'mydatabase';
TIP 1:
If you use the IF NOT EXISTS clause along with the CREATE
statement as shown below a new database will be created
and if a database with the given name, already exists the
query will be ignored.
CREATE DATABASE IF NOT EXISTS student;
MySQL DROP DATABASE:
You can drop/delete an existing database using the DROP DATABASE
Statement..
Syntax:
DROP DATABASE database_name;
Example:
DROP DATABASE student;
Once a database is deleted, you can check it in the list of databases as
follows −
show databases;
Output:
The above query produces the following output −
Database
information_schema
performance_schema
world
MySQL USE :
The USE statement of MySQL helps you to select/use a database.
You can also change to another database with this statement.
Syntax:
USE db_name;
Example:
use student;
CREATE TABLE :
The CREATE TABLE statement is used to create tables in MYSQL database.
Syntax:
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
);
Example:
CREATE TABLE Employee( Name VARCHAR(255),
Salary INT NOT NULL, Location VARCHAR(255) );
The SHOW TABLES statements gives you the list of tables in the current
database.
show tables;
DESCRIBE TABLE :
You can verify the above created table using the DESC statement too..
Syntax:
DESC tablename;
Example:
DESC employee;
Output:
ADDING PRIMARY KEY (ALTER):
The PRIMARY KEY constraint uniquely identifies each record in
a table.Primary keys must contain UNIQUE values, and cannot contain
NULL values. A table can have only ONE primary key;
Syntax:
ALTER TABLE table_name
ADD CONSTRAINT
PRIMARY KEY (column_name);
Example:
ALTER TABLE Employee
ADD PRIMARY KEY (ID);
REMOVING PRIMARY KEY (ALTER):
ALTER TABLE table_name
DROP PRIMARY KEY;
ADDING FOREIGN KEY (ALTER):
The FOREIGN KEY constraint is used to prevent actions that
would destroy links between tables.
A FOREIGN KEY is a field (or collection of fields) in one table,
that refers to the PRIMARY KEY in another table.
Syntax:
ALTER TABLE table_name
ADD FOREIGN KEY (column_name)
REFERENCE table_name(Referencing column_name in
table_name);
Example:
ALTER TABLE exam
ADD FOREIGN KEY(student_id)
REFERENCES student(student_id);
ADDING UNIQUE CONSTRAINT (ALTER):
The UNIQUE constraint ensures that all values in a
column are different.Both the UNIQUE and PRIMARY KEY constraints
provide a guarantee for uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE
constraint.However, you can have many UNIQUE constraints per table, but
only one PRIMARY KEY constraint per table.
Syntax:
ALTER TABLE table_name
ADD UNIQUE (Attribute name);
Example:
ALTER TABLE employee
ADD UNIQUE (first_name);
Syntax:
ALTER TABLE table_name
ADD attribute Datatype;
Example:
ALTER TABLE employee
ADD Income Int;
MODIFY DATATYPE OF AN ATTRIBUTE (ALTER):
Syntax:
ALTER TABLE table_name
MODIFY attribute Datatype;
Example:
ALTER TABLE employee
MODIFY address Varchar(40);
MODIFY CONSTRAINT OF AN ATTRIBUTE (ALTER):
Syntax:
ALTER TABLE table_name
MODIFY attribute Datatype NOT NULL;
Example:
ALTER TABLE employee
MODIFY address Varchar(40) NOT NULL;
ADD DEFAULT VALUE TO AN ATTRIBUTE (ALTER):
Syntax:
ALTER TABLE table_name
MODIFY attribute Datatype DEFAULT default value;
Example:
ALTER TABLE employee
MODIFY dateofbirth DATE DEFAULT 2000-05-15;
REMOVE AN ATTRIBUTE (ALTER):
Syntax:
ALTER TABLE table_name
DROP attribute;
Example:
ALTER TABLE employee DROP income;
DROP STATEMENT:
DROP TABLE:
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE employee;
DROP DATABASE:
Syntax:
DROP DATABASE database_name;
Example:
DROP DATABASE student;
RENAME TABLE:
Syntax:
RENAME TABLE table_name TO new_name;
Example:
RENAME TABLE employee to workers;
RENAME COLUMN:
Following are the two options that can be used with the ALTER TABLE statement
to rename a column(s) of a table:
• RENAME COLUMN
• CHANGE COLUMN
Syntax:
ALTER TABLE table_name RENAME COLUMN old_column1_name
TO new_column1_name, RENAME COLUMN old_column2_name TO
new_column2_name, ...;
OR
ALTER TABLE table_name CHANGE COLUMN old_column_name
new_column_name Data Type;
Example1:
ALTER TABLE CUSTOMERS RENAME COLUMN ID TO cust_id;
EXAMPLE 2:
ALTER TABLE CUSTOMERS CHANGE COLUMN cust_id ID
varchar(10);
INSERT Statement:
Syntax:
INSERT INTO table_name (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
Example:
Insert into sales values
(3, 'Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'),
3000, 'Vijayawada'),
(4, 'Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'),
9000, 'Chennai'),
(5, 'Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'),
6000, 'Goa');
SELECT Statement:
The SELECT statement is used to retrieve the rows from one or more (existing)
tables. You can use various clauses along with this statement.
Syntax:
SELECT select_expr FROM table_references WHERE
where_condition;
Where, select_expr is the expression indicating the columns you
need to retrieve.
Example:
SELECT * FROM SALES WHERE PRICE>6000;
Selecting specific columns:
SELECT ID, CustomerName, ProductName, Price FROM SALES;
UPDATE Statement:
We can update the values of existing records in MySQL
using the UPDATE statement. To update specific rows,
you need to use the WHERE clause along with it.
Syntax:
UPDATE table_reference SET column1 = value1,
column2 = value2...., columnN = valueN WHERE
[condition];
Example :
UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE gender =
'M';
DELETE Statement:
The DELETE statement from MySQL is used to delete records from a
MySQL table. To remove specific records, you need to use WHERE clause
along with it.
Syntax:
DELETE FROM table_name [WHERE Clause];
Example:
Following MySQL statement deletes the record of the employee with
FIRST_NAME "Mac".
DELETE FROM EMPLOYEE WHERE FIRST_NAME = 'Mac';
REPLACE:
REPLACE INTO table_name (column1, column2,
column3,...columnN) VALUES (value1, value2,
value3,...valueN);
Example:
REPLACE INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Kaushik', 20, 'Kota', 2000.00 );
WHERE Clause:
We can also use a conditional clause called the WHERE
Clause in conjunction with the SELECT statement to filter out
the results. Using this WHERE clause, we can specify a
selection criteria to select the required records from a table.
Syntax:
SELECT field1, field2,...fieldN table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....
Example:
Select * From CUSTOMERS Where AGE > 23;
ORDER BY CLAUSE:
The MySQL ORDER BY clause is used to sort one or
more columns of a table in provided order that can be either
ascending or descending order. By default, it sorts the
column(s) in ascending order if the sort order is not specified.
The sort is specified with two keywords; ASC for ascending
order and DESC for descending order.
Syntax:
SELECT column-list FROM table_name [ORDER BY column1,
column2, ..., columnN] [ASC|DESC]
Example:
SELECT * FROM CUSTOMERS ORDER BY NAME;
DISTINCT clause:
The DISTINCT clause in MySQL is used with a SELECT
statement to return the distinct values (unique values) from a
single or multiple of columns in a table. It ignores all the
duplicates values present in the particular column(s) and
returns only the distinct values.
Syntax:
SELECT DISTINCT column1, column2, ..., columnN FROM
table_name WHERE conditions
Example:
SELECT DISTINCT ADDRESS FROM CUSTOMERS;
Dmembership operator:
The DISTINCT clause in MySQL is used with a SELECT
statement to return the distinct values (unique values) from a
single or multiple of columns in a table. It ignores all the
duplicates values present in the particular column(s) and
returns only the distinct values.
Syntax:
SELECT DISTINCT column1, column2, ..., columnN FROM
table_name WHERE conditions
Example:
SELECT DISTINCT ADDRESS FROM CUSTOMERS;