Basic Operations With MySQL - Part 2
Basic Operations With MySQL - Part 2
The SELECT statement in MySQL is used to fetch data from one or more
tables. We can retrieve records of all fields or specified fields that match
specified criteria using this statement.
SYNTAX:
SELECT field_name1, field_name 2,... field_nameN
FROM table_name1, table_name2...
[WHERE condition]
[GROUP BY field_name(s)]
[HAVING condition]
[ORDER BY field_name(s)] ;
Where, * represents all columns and absence of ‘Where’ displays all the
records in a table.
FROM Tablename;
FROM Order_Details;
WHERE <Condition>;
WHERE <condition>;
The ‘DISTINCT’ clause eliminates the duplicate values and displays only
unique data on the screen.
FROM Tablename;
• Sorting of data
By default oracle sort the data in ascending order using ORDER BY. If
we need to sort the data in descending we have to use DESC predicate
in the statement.
ORDER BY Ename;
Example 2: Display the Order details in descending order of OrderDate
from Order relation.
DATA CONSTRAINTS
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This
ensures the accuracy and reliability of the data in the table. If there is any
violation between the constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply
to a column, and table level constraints apply to the whole table.
Whenever two tables contain one or more common columns, MySql
Database can enforce the relationship between the two tables through a
referential integrity constraint. Define a PRIMARY or UNIQUE key constraint on
the column in the parent table (the one that has the complete set of column
values).
If the data constraints attached to specific cell in the table reference the
contents of another cell in the table, then the user will have to use the table
level constraints. Table level constrains are stored as a part of global
definition of table.
If the table has already been created, you can add a NOT NULL constraint to
a column with the ALTER TABLE statement.
When a column name is defined as NOT NULL, then that column becomes
mandatory column. It implies that the use’s is forced to enter data into that
column. A NULL value is not equivalent to zero.
• Unique Key
The UNIQUE constraint uniquely identifies each record in a database table.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for
uniqueness for a column or set of columns. A PRIMARY KEY constraint
automatically has a UNIQUE constraint defined on it. We can have many
UNIQUE constraints per table, but only one PRIMARY KEY constraint per
table.
CREATE TABLE Persons (
ID int (10) NOT NULL UNIQUE,
LastName varchar (255) NOT NULL,
FirstName varchar (255),
Age int (3) );
The following SQL creates a CHECK constraint on the "Age" column when
the "Persons" table is created. The CHECK constraint ensures that you can
not have any person below 18 years:
4 24562 1