Professional Documents
Culture Documents
•DML(Data Manipulation Language): Once the tables are created and the
database is generated using DDL commands, manipulation inside those
tables and databases is done using DML commands. The advantage of
using DML commands is, that if in case any wrong changes or values are
made, they can be changed and rolled back easily.
5
Type of SQL Commands
•DQL(Data Query Language): Data query language consists of only one
command upon which data selection in SQL relies. The SELECT command
in combination with other SQL clauses is used to retrieve and fetch data
from databases/tables based on certain conditions applied by the user.
23
24
Connect to MySQL Using mysql command-line client
mysql is a command-line client program that allows you to interact with MySQL in the interactive and
non-interactive mode.
The mysql command-line client is typically located in the bin directory of the MySQL’s installation
folder.
Entering Queries
A query normally consists of an SQL statement followed by a semicolon.
When you issue a query, mysql sends it to the server for execution and displays the
results, then prints another mysql>prompt to indicate that it is ready for another
query.
Notice how the prompt changes from mysql>to - > after you
enter the first line of a multiple-line query. This is how mysql
indicates that it has not yet seen a complete statement and is
waiting for the rest.
If you decide you do not want to execute a query that
you are in the process of entering, cancel it by typing \c:
• Your database needs to be created only once, but you must select it for use each
time you begin a mySQL session. You can do this by issuing a USE statement.
SHOW Databases;
Statement that displays all created databases
SHOW Tables;
Statement that enables you to find out what
tables are in a given database.
Display existing Selecting database Display the tables for
databases for use dbcongzon database
• Creating the database is the easy part, but at this point it is empty, as
SHOW TABLES tells you:
• SQL data type is an attribute that specifies type
of data of any object. Each column, variable
and expression has related data type in SQL.
• You would use these data types while creating
your tables. You would choose a particular data
type for a table column based on your
requirement.
• SQL Server offers six categories of data types
for your use.
Textual Datatype
▪ https://www.w3schools.com/sql/sql_datatypes.asp
▪ https://www.tutorialspoint.com/mysql/mysql-data-types.htm
The DESCRIBE
statement is used to
display a table
structure in a
database.
DROP DATABASE
• The column_name parameters specify the names of the columns of the table.
• The data_type parameter specifies what type of data the column can hold (e.g.
varchar, integer, decimal, date, etc.).
• The size parameter specifies the maximum length of the column of the table.
• SQL constraints are used to specify rules for the
data in a table.
• If there is any violation between the constraint and
the data action, the action is aborted by the
constraint.
• Constraints can be specified when the table is
created (inside the CREATE TABLE statement) or
after the table is created (inside the ALTER TABLE
statement).
• NOT NULL - Indicates that a column cannot store NULL value.
• UNIQUE - Ensures that each row for a column must have a
unique value.
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
Ensures that a column (or combination of two or more columns)
have a unique identity which helps to find a particular record in a
table more easily and quickly.
• FOREIGN KEY - Ensure the referential integrity of the data in one
table to match values in another table.
• CHECK - Ensures that the value in a column meets a specific
condition.
• DEFAULT - Specifies a default value for a column
• AUTO_INCREMENT - Specifies that the values for this column
should be automatically increased by 1 for each new record. This
feature is often used to generate a primary key for the table. By
default, the starting value for an auto increment column is 1.
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:
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.
Note that you can have many UNIQUE constraints
per table, but only one PRIMARY KEY constraint per
table.
The following SQL creates a UNIQUE constraint on the "P_Id" column
when the "Persons" table is created:
SQLServer / Oracle / MS
MySQL:
Access:
To allow naming of a UNIQUE constraint, and for defining a
UNIQUE constraint on multiple columns, use the following SQL
syntax:
• The PRIMARY KEY constraint uniquely
identifies each record in a database table.
• Primary keys must contain UNIQUE values. A
primary key column cannot contain NULL
values.
• Most tables should have a primary key, and
each table can have only ONE primary key.
Note: In the example above there is only ONE PRIMARY KEY
(pk_PersonID). However, the VALUE of the primary key is made
up of TWO COLUMNS (P_Id + LastName).
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
• 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 single
column it allows 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.
• 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 can also be used to insert
system values, by using functions like GETDATE():
Create a table Display the created table
Display the table structure
43
Drop Database
43
Drop Table
43
Truncate Table
43
Data Definition Language (DDL)
• DDL is a set of SQL commands used to create, modify,
and delete database structures but not data.
Data Definition Language (DDL)
Data Definition Language (DDL)
Option 2:
ALTER TABLE tableName
CHANGE COLUMN originalName
newname datatype()
Data Definition Language (DDL)
Data Definition Language (DDL)
Data Definition Language (DDL)
Data Definition Language (DDL)
Data Manipulation Language (DML)
• DML is a category of SQL commands that are used to
manipulate and modify data within a database.
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Data Query Language (DQL)
• DQL is a category of SQL commands that are used to
retrieve an manipulate data from a database.
Data Query Language (DQL)
• DQL is a category of SQL commands that are used to
retrieve an manipulate data from a database.
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
Data Query Language (DQL)
End of
Lesson 7
44