Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Basic Operations With MySQL - Part 2

The document provides an overview of the MySQL SELECT statement used to retrieve data from tables, detailing its syntax and various use cases such as fetching all rows, specific columns, and applying conditions. It also covers SQL constraints, including NOT NULL, PRIMARY KEY, UNIQUE, DEFAULT, CHECK, and FOREIGN KEY constraints, explaining their purpose in maintaining data integrity and relationships between tables. Examples are provided for each concept to illustrate their application in SQL queries.

Uploaded by

entcules849567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Basic Operations With MySQL - Part 2

The document provides an overview of the MySQL SELECT statement used to retrieve data from tables, detailing its syntax and various use cases such as fetching all rows, specific columns, and applying conditions. It also covers SQL constraints, including NOT NULL, PRIMARY KEY, UNIQUE, DEFAULT, CHECK, and FOREIGN KEY constraints, explaining their purpose in maintaining data integrity and relationships between tables. Examples are provided for each concept to illustrate their application in SQL queries.

Uploaded by

entcules849567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MySQL SELECT Statement

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.

SELECT Statement Syntax


It is the most used SQL query to display the selected data on the screen. The
general syntax of this statement to fetch data from tables are as follows:

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)] ;

• All Rows and Columns


Syntax: SELECT * FROM Tablename;

Where, * represents all columns and absence of ‘Where’ displays all the
records in a table.

Example: Display the details of a Product relation.

SELECT * FROM Product;

• Specific Columns and All Rows


Syntax: SELECT columnname, columnname, …,

FROM Tablename;

Example: Display OrderID and OderDate from Order_Details relation.

SELECT Order_ID, OrderDate

FROM Order_Details;

• Specific Rows and All Columns

‘Where’ clause in SELECT command is used to filter the records based


on the condition. SELECT statement display only those records which
satisfies the condition.
Syntax: SELECT * FROM Tablename

WHERE <Condition>;

Example: Display the details of Employees from Sales department.

SELECT * FROM Employees

WHERE Dept = ‘Sales’;

• Specific Columns and Specific Rows


Syntax: SELECT * FROM Tablename

WHERE <condition>;

Example: Display Eno and names of Finance department.

SELECT Eno, Ename FROM Employee

WHERE Dept LIKE ‘Finance’;

• Elimination of duplicate records

The ‘DISTINCT’ clause eliminates the duplicate values and displays only
unique data on the screen.

Syntax: SELECT DISTINCT column1, column2

FROM Tablename;

Example: SELECT DISTINCT Ename FROM Employees;

• 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.

Syntax: SELECT * FROM Tablename

ORDER BY Columnname [DESC];

Example 1: Display the Employee details in ascending order of names.

SELECT * FROM Employees

ORDER BY Ename;
Example 2: Display the Order details in descending order of OrderDate
from Order relation.

SELECT * FROM Order

ORDER BY OrderDate DESC;

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).

• Column level constraints


If the constrains are defined along with the column definition, then it is
called column level constrains. Column level constrains can be applied to
any one column at a time. i.e., they are local to specific column. If the
constrains spans over more column, then used must define table constrains.
Table level constraints

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.

• Null value concept:


The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This
means that we cannot insert a new record, or update a record without
adding a value to this field.
By default, a column can hold NULL values. The NOT NULL constraint
enforces a column to NOT accept NULL values. This enforces a field to
always contain a value, which means that you cannot insert a new record,
or update a record without adding a value to this field.

CREATE TABLE Persons (


ID int (10) NOT NULL,
LastName varchar2 (25) NOT NULL,
FirstName varchar2 (25) NOT NULL,
Age INT (3));

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.

• Primary Key Concept:


A primary key is one or more columns in a table used to uniquely identify
each row in the table. Primary key value must not be null and unique
across the column. The PRIMARY KEY constraint uniquely identifies each
record in a database table. Primary keys must contain UNIQUE values, and
cannot contain NULL values. A table can have only one primary key, which
may consist of single or multiple fields.
A multi column primary key is called composite primary key. A table can
have only one primary key, which may consist of single or multiple fields.
When multiple fields are used as a primary key, they are called a composite
key.
Example : Primary key as column constrains
CREATE TABLE Persons (
ID int (10) PRIMARY KEY,
LastName varchar (255) NOT NULL, FirstName varchar (255),
Age Int (3) NOT NULL );

Primary key as table level constrains:


CREATE TABLE Persons (
ID integer (10) NOT NULL,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Mobie_no integer (10) NOT NULL,
Age integer (3),
PRIMARY KEY (ID,Mobile_no) );

• 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) );

CREATE TABLE Persons (


ID int (10) NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int (3),
CONSTRAINT Ukey_Person UNIQUE (ID, LastName) );

• Default Value Concept:


The DEFAULT constraint is used to insert a default value into a column.
The default value will be added to all new records, if no other value is
specified. The DEFAULT constraint provides a default value to a column
when the INSERT INTO statement does not provide a specific value.
CREATE TABLE Persons (
ID int (10) NOT NULL, LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int (3),
City varchar (255) DEFAULT 'MANGALORE');

• Check Integrity Constraints


The CHECK constraint is used to limit the value range that can be placed in
a column. We define a CHECK constraint on a single column it allows only
certain values for this column or define a CHECK constraint on a table it
can limit the values in certain columns based on values in other columns in
the row.
Example

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:

CREATE TABLE Persons (


ID int (10) PRIMARY KEY,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Age int (3) CHECK (Age>=18),
City varchar (25) CHECK ( City IN
(‘MANGALORE’, ’MYSORE’, ’MUMBAI’, ’CHENNAI’)) );

Restriction of check constraint


CHECK constraints let you enforce very specific integrity rules by specifying
a check condition. The condition of a CHECK constraint has some
limitations:

1. It must be a Boolean expression evaluated using the values in the


row being inserted or updated, and

2. It cannot contain sub queries; sequences; the SQL functions


CURRDATE, UID, USER, or USERENV; or the pseudo columns
LEVEL or ROWNUM.

• Foreign Key Concept


Foreign key represents relationship between tables. A foreign key is column
or group of columns whose values are defined from primary key of the
same of some other table. A FOREIGN KEY is a key used to link two tables
together. A FOREIGN KEY is a field (or collection of fields) in one table that
refers to the PRIMARY KEY in another table. The table containing the
foreign key is called the child table, and the table containing the candidate
key is called the referenced or parent table.

Consider the following 2 tables


Person Order

PersonID LastName FirstName Age OrderID OrderNumber PersonID

1 Hansen Ola 30 1 77895 3

2 Svendson Tove 23 2 44678 3

3 Pettersen Kari 20 3 22456 2

4 24562 1

We Notice that the "PersonID" column in the "Orders" table


points to the "PersonID" column in the "Persons" table. The "PersonID"
column in the "Persons" table is the PRIMARY KEY in the "Persons"
table. The "PersonID" column in the "Orders" table is a FOREIGN KEY
in the "Orders" table. The FOREIGN KEY constraint is used to prevent
actions that would destroy links between tables. The FOREIGN KEY
constraint also prevents invalid data from being inserted into the
foreign key column, because it has to be one of the values contained in
the table it points to.

FOREIGN KEY as column constraints:

CREATE TABLE Orders (


OrderID int (10) PRIMARY KEY,
OrderNumber int NOT NULL,
PersonID int (10) REFERENCES Persons );

FOREIGN KEY as table constraint:

CREATE TABLE Orders (


OrderID int (5) NOT NULL,
OrderNumber int (5) NOT NULL,
PersonID int (10),
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons );



You might also like