Database and SQL Final
Database and SQL Final
Candidate key All column combinations that can serve as a primary key (are
candidates for primary key)
Alternate key A candidate key which is not the primary key
Foreign key Non-key attribute whose value is derived from the primary key
of another table
OR
Non-key attribute which is the primary key of some other table
Foreign/Detail table- in which foreign key exists
Primary/Master table – defines the Primary key, which the
foreign key of Detail table refers to.
is a data sublanguage containing a set of commands used by
RDBMS(e.g MySQL database) to access data.
Is a 4th generation non-procedural language (specifies WHAT
is required but not on HOW it is to be done) that enables us to
create and operate on relational databases
It works on a PC, a workstation, a mini or a mainframe.
There are numerous versions of SQL.
Structured Query Language used by RDBMS to communicate
with database
Manages one or more data tables and relationships between data
tables with information in the data dictionary
Is efficient, easy to use and learn , functionally
complete
Recovery and concurrency
Security -by views
Integrity constraints –to ensure data values in
columns confirm to certain specified rules
Char(n) Varchar(n)
Specifies fixed-length Specifies variable-length
character string character string
If char(n) is specified If varchar(n) is specified datatype
datatype for a column, for a column,
nsize of values stored in n max.size of values stored in
this column is n bytes. this column is n bytes.
USE databasename;
To know the list of tables within a database.
SHOW TABLES;
To create a database, we use the CREATE
DATABASE statement:
DESC tablename;
Is used to change or alter the structure
(schema) of the table
Distinct Clause
SELECT DISTINCT DeptId FROM EMPLOYEE;
WHERE Clause
Membership operator IN
ORDER BY Clause
Handling NULL Values
Substring pattern matching
Relational operators (<=,< , >, >=, =, !=) -
can be used to specify such conditions.
Here, column1, column2, ... are the field names of the table you want to select
data from.
Example
SELECT * FROM Customers;
is used to return only distinct (different) values.
WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
The following SQL statement selects all the
customers from the country "Mexico", in the
"Customers" table:
Example
SELECT * FROM Customers
WHERE Country='Mexico';
Operator Description
= Equal
NOT TRUE.
The following SQL statement selects all fields
from "Customers" where country is
"Germany" AND city is "Berlin":
Example:
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
The following SQL statement selects all fields
from "Customers" where city is "Berlin" OR
"München":
Example
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
The following SQL statement selects all fields
from "Customers" where country is NOT
"Germany":
Example
SELECT * FROM Customers
WHERE NOT Country='Germany';
The following SQL statement selects all fields from
"Customers" where country is "Germany" AND city must
be "Berlin" OR "München" (use parenthesis to form
complex expressions):
Example
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR
City='München');
The ORDER BY keyword is used to sort the result-
set in ascending or descending order.
The ORDER BY keyword sorts the records in
ascending order by default. To sort the records in
descending order, use the DESC keyword.
ORDER BY Syntax
Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;
The following SQL statement selects all
customers from the "Customers" table, sorted
ascending by the "Country" and descending by
the "CustomerName" column:
Example
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
groups rows that have the same values into summary rows,
like "find the number of customers in each country".
is often used with aggregate functions (COUNT, MAX, MIN,
SUM, AVG) to group the result-set by one or more
columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
The following SQL statement lists the number of
customers in each country:
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
The HAVING clause was added to SQL because the
WHERE keyword could not be used with aggregate
functions.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
The following SQL statement lists the number of
customers in each country. Only include countries
with more than 5 customers:
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
HAVING clause WHERE clause
1. The HAVING clause is used in 1. The WHERE clause is used in
database systems to fetch the database systems to fetch the
data/values from the groups data/values from the tables
according to the given condition. according to the given condition.
2. The HAVING clause is always 2. The WHERE clause can be
executed with the GROUP BY executed without the GROUP BY
clause. clause.
3. The HAVING clause can include 3. We cannot use the SQL
SQL aggregate functions in a query aggregate function with WHERE
or statement. clause in statements.
4. We can only use SELECT 4. Whereas, we can easily use
statement with HAVING clause for WHERE clause with UPDATE,
filtering the records. DELETE, and SELECT statements.
5. The HAVING clause is used in 5. The WHERE clause is always
SQL queries after the GROUP BY used before the GROUP BY clause
clause. in SQL queries.
6. It is used to filter groups. 6. It is used to filter the single record
of the table.
is used to insert new records in a table.