Important MYSQL Commands
Important MYSQL Commands
Relation:- In database, a relation means a 'table', in which data is organized in the form of
rows and columns. Therefore in database, relations are equivalent to tables.
Domain: A domain is the original sets of atomic values used to model data. It refers to all the
possible unique values of a particular column.
For example:
i) The domain of gender column has a set of two possible values i.e, Male or Female.
ii) The domain of marital status has a set of four possible values i.e, Married, Unmarried,
Widows and Divorced.
Tuple: -The horizontal subset of a Table is known as a Row or Tuple.
Attribute:- The vertical subset of a Table is known as a Column or Attribute.
Degree: - The degree is the number of attributes (columns) in a table.
Cardinality: -Cardinality is number of rows (tuples) in a table.
Primary Key: A column or set of columns that uniquely identifies a row within a table is called
primary key.
Candidate Key: Candidate keys are set of fields (columns with unique values) in the relation
that are eligible to act as a primary key.
Alternate Key: Out of the candidate keys, after selecting a key as primary key, the remaining
keys are called alternate key.
Foreign Key: A foreign key is a field (or collection of fields) in one table that uniquely identifies
a row of another table. In other words, a foreign key is a column or a combination of columns
that is used to establish a link between two tables.
SQL Commands:
SQL commands are instructions, coded into SQL statements, which are used to communicate
with the database to perform specific tasks with data.
SQL commands are grouped into four major categories depending on their functionality:
Data Definition Language (DDL) - These SQL commands are used for creating, modifying, and
dropping the structure of database objects. The commands are CREATE, ALTER, DROP,
RENAME, and TRUNCATE.
Data Manipulation Language (DML) - These SQL commands are used for storing, retrieving,
modifying, and deleting data.
These Data Manipulation Language commands are: SELECT, INSERT, UPDATE, and DELETE.
Transaction Control Language (TCL) - These SQL commands are used for managing changes
affecting the data. These commands are COMMIT, ROLLBACK, and SAVEPOINT.
Data Control Language (DCL) - These SQL commands are used for providing security to
database objects. These commands are GRANT and REVOKE.
Data Type
Each value manipulated by SQL Database has a data type. The data type of a value associates a
fixed set of properties with the value. In SQL there are three main data types: Character,
Number, and Date types.
UNIQUE Constraint
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.
CHECK Constraint
The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a column it will allow only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on
values in other columns in the row.
DEFAULT Constraint
The DEFAULT constraint is used to set a default value for a column.
The default value will be added to all new records, if no other value is specified.
Referential integrity refers to the accuracy and consistency of data within a relationship.
In relationships, data is linked between two or more tables. This is achieved by having the
foreign key (in the associated table) reference a primary key value (in the primary – or parent
– table). Because of this, we need to ensure that data on both sides of the relationship remain
intact.
Referential Integrity: When one attribute of a relation can only take values from other
attribute of same relation or any other relation, it is called referential integrity.
Referential integrity is a subset of data integrity,
Aggregate Functions
A single row function works on a single value. SQL also provides us multiple row functions. A
multiple row function works on multiple values. These functions are called aggregate
functions or group functions. These functions are:
1 MAX() Returns the MAXIMUM of the values under the specified column/expression.
2 MIN() Returns the MINIMUM of the values under the specified column/expression.
3 AVG() Returns the AVERAGE of the values under the specified column/expression.
4 SUM() Returns the SUM of the values under the specified column/expression.
5 COUNT() Returns the COUNT of the number of values under the specified
column/expression.
CARTISIAN PRODUCT: The Cartesian product of two relations is the concatenation of tuples
belonging to the two relations.
The degree of the new relation is the sum of the degrees of two relations on which Cartesian
product is operated. The Cardinality of the new relation is equal to the product of number of
tuples of the two relations on which Cartesian product is operated.
The Cartesian product is also referred to as a cross-join, returns all the rows in all the tables
listed in the query.
Types of Joins:
Equi- join
Natural Join
Non-equi join
Equi – Join: An equi-join is an operation that combines multiple tables based on equality or
matching column values in the associated tables. It is also called as inner join or simple join.
Points to remember:
There is no need to be the same column names.
The resultant result can have repeated column names.
We can also perform an equijoin operation on more than two tables.
Syntax:
The following are the basic syntax that illustrates the equijoin operations:
SELECT column_name(s) FROM table_name1, table_name2 WHERE
table_name1.column_name = table_name2.column_name;
OR
SELECT (column_list | *) FROM table_name1 INNER JOIN table_name2 ON
table_name1.column_name = table_name2.column_name;
Natural Join: A natural join is a type of join operation that creates an implicit join by
combining tables based on columns with the same name and data type. It is similar to the
INNER or LEFT JOIN, but we cannot use the ON or USING clause with natural join as we used in
them.
Points to remember:
There is no need to specify the column names to join.
The resultant table always contains unique columns.
It is possible to perform a natural join on more than two tables.
Do not use the ON clause.
Syntax:
The following is a basic syntax to illustrate the natural join:
First, we will create two tables named customer and balance using the below statements:
Next, we will fill some records into both tables using the below statements:
Execute the below statement for joining tables using natural join:
We can do the same job with the help of INNER JOIN using the ON clause. Here is the query
to explain this join:
Suppose we use the asterisk (*) in the place of column names, then the natural join
automatically searches the same column names and their data types and join them internally.
Also, it does not display the repeated columns in the output. Hence, we should get the below
output after executing the above statement:
The WHERE clause is used to return the filter result from the table. The following example
illustrates this with the natural join clause:
Non Equi-join: When there is no common column in two table and we perform a join then this
type of join is called non-equi join.
Example: Teacher table attributes: TN0 , TNAME, TADDRESS, SALARY, DEPT_NO,DOJ
Grade table attributes: GRADE, MINSAL, MAXSAL
In these tables there is no common column. So, it is not possible to perform equi-join. The
only relationship between the two tables is that the salary in the teacher table is in between
minsal and maxsal column of the grade table. So, we cannot obtain the result using the ‘=’
operator.
Thus the query will be:
SELECT T.TNAME, T.SALARY, G.GRADE FROM TEACHER T , GRADE G WHERE T.SALARY
BETWEEN G.MINSAL AND G.MAXSAL;
Sub-Queries: When you place one query inside another query then it is called a sub query. In
the sub query, the inner query generates values that are tested in the conditions of the outer
query.
Example: Write a query to display the teacher names whose salary is greater than the salary
of teacher number T05.
SELECT TNAME FROM TEACHER WHERE SALARY > (SELECT SALARY FROM TEACHER WHERE TNO=’T05’)
UNION OPERATION
Union is an operation of combining the output of two select statements. Union of two select
statements can be performed only if their outputs contain same number of columns and same
data types of the corresponding columns.
Syntax:
Select columns from table where condition union Select columns from table where
condition.
To use union , each select statement must have the same number of columns selected and
same data types of the corresponding columns and must have them in the same order, but
they do not need to be of the same length.
The UNION operator selects only distinct values by default. To allow duplicate values, use the
ALL keyword with UNION.
For example: There are two tables called ‘Class12A’ and ‘Class12B’ with same columns like:
Field Name Field Type
RegNo Varchar(10)
Name Varchar(50)
City Varchar(50)
Two tables contain the following data:
Class12A
RegNo Name City
NIS101 Amit Kumar Kanpur
NIS102 Akash Goel Lucknow
NIS103 Om Verma Agra
NIS104 Vishwash Kumar Chennai
NIS105 Ramesh Pal New Delhi
NIS106 Prakhar Kumar Lucknow
NIS110 Nitin Patel Chennai
Class12B
RegNo Name City
NIS106 Prakhar Kumar Lucknow
NIS107 Ratandeep Kanpur
NIS108 K.K Malhotra Agra
NIS109 Maria New Delhi
NIS110 Nitin Patel Chennai
NIS102 Akash Goel Lucknow
NIS105 Ramesh Pal New Delhi
Write a query to display all the rows from the ‘Class12A’ and the ‘Class12B’ tables in ascending
order of city.
Select * from class12A UNION select * from class 12B order by city;