Introduction To Structured Query Language11
Introduction To Structured Query Language11
MySQL Features
1. MySQL are very fast and much reliable for any type of application.
2. MySQL is very Lightweight application.
3. MySQL command line tool is very powerful and can be used to run SQL
queries against database.
4. MySQL supports indexing and binary objects.
5. MySQL offers privileges and password system that is very secure.
6. MYSQL can Handle large database
7. MySQL Written in C and C++ language.
8. MySQL server can provide error messages to clients in many languages
9. MySQL provide many data types to support different types of data.
10. MYSQL is high performance and relatively simple database system.
Advantages of MYSQL:
Installing MySQL
MySQL is an open source RDBMS software which can be easily downloaded from
the official website https:// dev.mysql.com/downloads. After installing MySQL,
start MySQL service. The appearance of mysql> prompt means that MySQL is
ready for us to enter SQL statements.
Few rules to follow while writing SQL statements in MySQL:
• SQL is case insensitive. That means name and NAME are same for SQL.
• Always end SQL statements with a semicolon (;).
• To enter multiline SQL statements, we don’t write ‘;’ after the first line. We
put enter to continue on next line. The prompt mysql> then changes to ‘->’,
indicating that statement is continued to the next line. After the last line, put ‘;’
and press enter.
RDBMS
RDBMS stands for Relational Database Management System. RDBMS is the basis
for SQL, and for all modern database systems like MS SQL Server, IBM DB2,
Oracle, MySQL, and Microsoft Access. The data in RDBMS is stored in database
objects called tables. A table is a collection of related data entries and it consists
of columns and rows.
Keep in Mind That...
SQL is not case sensitive
Semicolon after SQL Statements
Data type
Data type indicates the type of data value that an attribute can have. The data type of an
attribute decides the operations that can be performed on the data of that attribute.
Constraints
Constraints are certain types of restrictions on the data values that an attribute can have.
They are used to ensure the accuracy and reliability of data. However, it is not mandatory to
define constraint for each attribute of a table.
The AND & OR Operators
The AND & OR operators are used to filter records based on more than one
condition. The AND operator displays a record if both the first condition and the
second condition is true. And OR operator displays a record if either the first
condition or the second condition is true.
DDL commands: The DDL commands, as the name suggests, allow you to
perform tasks related to data definition. The DDL part of SQL permits database
tables to be created or deleted. The most important DDL statements in SQL are:
CREATE DATABASE - creates a new database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
DROP DATABASE-Deletes a database
The CREATE TABLE Statement
The CREATE TABLE statement is used to create a table in a database.
Syntax
CREATE TABLE table_name
(
column_name1 data_type,column_name2 data_type,column_name3 data_type,....);
CREATE TABLE Example
Create table STUDENT.
mysql> CREATE TABLE STUDENT(
-> RollNumber INT,
-> SName VARCHAR(20),
-> SDateofBirth DATE,
-> GUID CHAR(12));
Query OK, 0 rows affected (0.91 sec)
SQL NOT NULL Constraint
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 you cannot insert a
new record, or update a record without adding a value to this field. The following SQL
enforces the "P_Id" column and the "LastName" column to not accept NULL values:
Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in the
"Persons" table.
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" 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 link between
tables.
The FOREIGN KEY constraint also prevents that invalid data is inserted into the foreign key
column, because it has to be one of the values contained in the table it points to.
SQL DEFAULT Constraint
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.
SQL DEFAULT Constraint on CREATE TABLE
The following SQL creates a DEFAULT constraint on the "City" column when the "Persons"
table is created:
CREATE TABLE Persons ( P_Id int NOT NULL,LastName varchar(25) NOT NULL,
FirstName varchar(25),Address varchar(25),City varchar(20) DEFAULT 'Bhopal');
The ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
SQL ALTER TABLE Syntax
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To delete a column in a table, use the following syntax
ALTER TABLE table_name
DROP COLUMN column_name;
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
Modify COLUMN column_name datatype;
To change the Column name of a column in a table, use the following syntax:
ALTER TABLE table_name
Change Old COLUMN name New column_name datatype;
Drop Table Command
Drop table command of SQL lets you drop a table from the database.
Drop Table table_name;
DML commands: A DML is a language that enables users to access and manipulates data as
organized by the appropriate data model. The query and update commands form the DML
part of SQL:
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
The INSERT INTO Statement
The INSERT INTO statement is used to insert a new row in a table.
SQL INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two forms.
The first form doesn't specify the column names where the data will be inserted, only their
values:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
The UPDATE Statement
The UPDATE statement is used to update existing records in a table.
SQL UPDATE Syntax
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value;
Warning!: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies
which record or records that should be updated. If you omit the WHERE clause, all records
will be updated!
The DELETE Statement
The DELETE statement is used to delete rows in a table.
SQL DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value;
Warning!: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which
record or records that should be deleted. If you omit the WHERE clause, all records will be
deleted!
DISTINCT Clause : By default, SQL shows all the data retrieved through query as output.
However, there can be duplicate values. The SELECT statement when combined with
DISTINCT clause, returns records without repetition (distinct records).
mysql> SELECT DISTINCT city FROM EMPLOYEE;
Substring pattern matching : Many a times we come across situations where we don’t want
to query by matching exact text or value. Rather, we are interested to find matching of only a
few characters or values in column values. For example, to find out names starting with ‘T’ or
to find out pin codes starting with ‘60’. This is called substring pattern matching.
SQL Wildcards
SQL wildcards can substitute for one or more characters when searching for data in a
database.SQL wildcards must be used with the SQL LIKE operator.
SQL LIKE Operator : With SQL, the following wildcards can be used:
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
This search can be done only for string values.
SQL LIKE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
Example
Now we want to select the persons living in a city that starts with "s".
We use the following SELECT statement:
SELECT * FROM Persons WHERE City LIKE 's%';
The "%" sign can be used to define wildcards (missing letters in the pattern) both before and
after the pattern.Next, we want to select the persons living in a city that contains the pattern
"tav" from the "Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons WHERE City LIKE '%tav%';
MEMBERSHIP OPERATOR IN : IN operator compares a value with a set of values and returns
true if the value belongs to that set.
Syntax :
SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,...);
Example
mysql> SELECT * FROM EMPLOYEE WHERE DeptId IN ('D01', 'D02' , 'D04');
The BETWEEN Operator
The BETWEEN operator selects a range of data between two values. The values can be
numbers, text, or dates.
SQL BETWEEN Syntax
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND
value2;
ORDER BY Clause : ORDER BY clause is used to display data in an ordered (arranged) form
with respect to a specified column. By default, ORDER BY displays records in ascending order
of the specified column’s values. To display the records in descending order, the DESC
(means descending) keyword needs to be written with that column.
Syntax :
Select column_name(s) FROM table_name [where] [order by <column_name>] ASC/DESC;
Handling NULL Values : SQL supports a special value called NULL to represent a missing or
unknown value. It is important to note that NULL is different from 0 (zero).
Example:
SELECT * FROM student WHERE city IS NULL;
Renaming of columns: In case we want to rename any column while displaying the output,
we can do so by using alias 'AS' in the query.
SELECT column_name(s) As alias_name ,Column_name as alias_name FROM table_name ;
TCL commands: The TCL commands used to manage and control the transactions of data in
database. The most important TCL commands are:
COMMIT – it make all the changes made by statement issued.
ROLLBACK – it undoes all changes since the beginning of the transaction or since save point.
SAVEPOINT – it marks a point up to successfully completed transaction.