You are on page 1of 124

Oracle Live SQL

Steps for account creation

• Click on the link: https://livesql.oracle.com/

• Create account on it if you have already


account then sign in
Steps for account creation

• Click on the start coding.

• Write your query here.


MySQL
What is MYSQL

• MySQL is free, Open-source software for which the source code can
be download.

• Create your own database, either for educational purposes or to keep


track of important items at home.

• You can install it to learn SQL, the programming language used to


manage and interact with most databases.
What is MYSQL

• MySQL is best RDBMS being used developing web-based software


applications.

• MySQL is ideal for both small and large applications.

• MySQL is developed, distributed, and supported by Oracle


Corporation
Who Uses MYSQL

• Huge websites like Facebook, Twitter, Airbnb, Booking.com, Uber,


GitHub, YouTube, etc.

• Content Management Systems like WordPress, Drupal, Joomla!,


Contao, etc.

• A very large number of web developers around the world


Show Data on Website

To build a web site that shows data from a database, you will need:

• An RDBMS database program (like MySQL)


• A server-side scripting language, like PHP
• To use SQL to get the data you want
• To use HTML / CSS to style the page
MySQL Installation
Why Install MYSQL on Windows 10
• MySQL is free, Open-source software for which the source code can
be download.

• Create your own database, either for educational purposes or to keep


track of important items at home.

• You can install it to learn SQL, the programming language used to


manage and interact with most databases.
How to Download MySQL Free Community Edition
Before installing the MySQL Free Community edition, download it on
your PC:

Step 1: Go to the MySQL website and select Downloads.


https://www.mysql.com/
How to Download MySQL Free Community Edition
Step 2: Select MySQL Community (GPL) Downloads. The Community
Edition is the free, open-source version of MySQL.
How to Download MySQL Free Community Edition
Step 3: On the following page, select MySQL Community Server.
How to Download MySQL Free Community Edition
Step 4: Scroll down to the bottom of the page and select the Go to
Download Page next to Windows (x86, 32 & 64-bit), MySQL Installer
MSI.
How to Download MySQL Free Community Edition
Step 5: The next page will ask you to choose between two installer
files:
• If you have an active Internet connection, select the top download.
• If you have to be offline when you install, select the bottom
download.
How to Download MySQL Free Community Edition
Step 6: Finally, log in to your Oracle account. If you don't have one, or
don't want to sign in, select No thanks, just start my download in the
lower-left corner.
How to Install MYSQL on Windows 10
Step1: Open the file you downloaded to start the installation process.
Step2: You'll see the different installation types. Choose Custom and
then Next.
How to Install MYSQL on Windows 10
Step3: On the Select Products and Features screen, you'll need to move
items from the Select Products box to the Products to be Installed box.
First, open the MySQL Servers and expand the MySQL Server folder
and select the appropriate version for your system to move it to the
right column.
How to Install MYSQL on Windows 10
Step3: On the Select Products and Features screen, you'll need to move
items from the Select Products box to the Products to be Installed box.
First, open the MySQL Servers and expand the MySQL Server folder
and select the appropriate version for your system to move it to the
right column.
How to Install MYSQL on Windows 10
Step4: Select Applications to expand it, then select everything
except MySQL for Visual Studio. Again, select the right-facing arrow to
line it up for installation.
How to Install MYSQL on Windows 10
Step5: Finally select Documentation, and add its item. This option
provides some example databases you can look through
How to Install MYSQL on Windows 10
Step6: Select Execute. You can select Show Details to see the status of
the installation.
How to Install MYSQL on Windows 10
Step7: Now the installer will start downloading MySQL
How to Install MYSQL on Windows 10
Step8: Once everything downloads, MySQL will start installing. Once
the Status of each item changes to Complete, select Next.
How to Install MYSQL on Windows 10
Step9: Once product configuration is visible click on next
How to Install MYSQL on Windows 10
Step10: The wizard will then lead you through configuration. Configure
the MySQL Server as follows:
• Type and Networking: Select the default Config Type Development
Computer, which will set things up for you to work locally. Click on
NEXT
How to Install MYSQL on Windows 10
Step10: The wizard will then lead you through configuration. Configure
the MySQL Server as follows:
• Authentication Method*: Select Use Strong Password Encryption for
Authentication.
How to Install MYSQL on Windows 10
Step10: The wizard will then lead you through configuration. Configure
the MySQL Server as follows:
• Accounts and Roles: Enter a password for your MySQL root (i.e.,
admin) user. Normally, you can (and should) set up at least one
normal user with a name and password as well, but since you're just
testing things out, the root account will be enough.
How to Install MYSQL on Windows 10
Step10: The wizard will then lead you through configuration. Configure
the MySQL Server as follows:
• Accounts and Roles: If you want to add new user click on add user.
How to Install MYSQL on Windows 10
Step10: The wizard will then lead you through configuration. Configure
the MySQL Server as follows:
• Accounts and Roles: Now we have created a user and we also have
root user click
How to Install MYSQL on Windows 10
Step10: The wizard will then lead you through configuration. Configure
the MySQL Server as follows:
• Windows Service: You can keep the defaults here, but you should
select Start the MySQL Server at System Startup to disable it. As a
general rule, try not to leave services you don't need running on your
machine.
How to Install MYSQL on Windows 10
Step11: Select Execute to apply configurations.
How to Install MYSQL on Windows 10
Step12: Select Finish to apply your Configurations.
How to Install MYSQL on Windows 10
• Step13: Repeat this process for other components.
How to Install MYSQL on Windows 10
• Step14: Select Finish to complete the installation. You don’t need to
start any of the apps at this stage.
How to Install MYSQL on Windows 10
• Shell Started automatically
How to Install MYSQL on Windows 10
• Workbench started automatically click on local instance and enter
the root password.
How to Install MYSQL on Windows 10
• Workbench started automatically click on local instance and enter
the root password.
How to Install MYSQL on Windows 10
• After entering the root password the workbench will be open.
How to check successful
installation of MySQL
How to check successful installation of MySQL
• Open the command prompt and check whether the sql installed
successfully.
How to check successful installation of MySQL
• Open the command prompt
How to check successful installation of MySQL
• Now login on to the root
How to check successful installation of MySQL
Enter password of the root user and press enter. Now go logged into
server.
How to Start and Stop the
MYSQL
How to Start and Stop the MYSQL
The key to working with MySQL is a running server. You can start and
stop the server from the Windows Services app.

Step :1 In the Windows Search box, enter services.


How to Start and Stop the MYSQL
Step :2 Select Open to run the Services app.
How to Start and Stop the MYSQL
Step 3: Once the Services app launches, locate the MySQL service. Its
name will be "MySQL" with the version number after it (in this
case, MySQL80).
How to Start and Stop the MYSQL
Step 4: Select the MySQL service, and you'll get options in the left
pane. If the service is stopped, select Start. If it's already running, you
can select Restart, Pause, or Stop. You can use this to make sure
MySQL is only running when you want to use it.
What is RDBMS
RDBMS
• RDBMS Stands for Relational Database Management System.

• RDBMS is a program used to maintain a relational database.

• RDBMS is the basis for all modern database systems such as MySQL, Microsoft
SQL Server, Oracle, and Microsoft Access.

• RDBMS uses SQL queries to access the data in the database.


RDBMS Terminology
• Database: - A database is a collection of tables, with related data.

• Table: - A table is a matrix with data. A table in a database looks like a simple
spreadsheet.

• Column: - One column (data element) contains data of one and the same kind,
for example the column postcode.

• Row − A row (= tuple, entry or record) is a group of related data, for example
the data of one subscription.
RDBMS Terminology
• Primary Key: - A primary key is unique. A key value can not occur twice in one
table. With a key, you can only find one row.

• Foreign Key: - A foreign is a field (or collection of fields) in one table, that refers
to the primary key in another table. The table with the foreign key is called the
child table, and the table with the primary key is called the referenced or
parent table.

• Compound Key − A compound key (composite key) is a key that consists of


multiple columns, because one column is not sufficiently unique.

• Referential Integrity − Referential Integrity makes sure that a foreign key value
always points to an existing row.
What is SQL
Advantages of SQL
Database Supported by SQL
Datatype in SQL
In MySQL there are three main data types:

1)String
2)Numeric
3)date and time.
Datatype in SQL (String Data Type)
Datatype in SQL (Numeric Data Type)
Datatype in SQL (Date and Time)
SQL LANGUAGE AND COMMAND
SQL LANGUAGE AND COMMAND
DDL COMMANDS
Commands
Show default databases.
> Show databases;
Commands
Create new database.
> Create database databasename
Commands
To view the created database
>Show databases;
Commands
Drop new database.
➢drop database databasename;
Commands
Use new database.
➢use databasename;

Create table inside the new database

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
column3 datatype,
....
);
Commands
Show created tables.
➢Show tables;
Create Constraints
Constraints can be specified when the table is created with CREATE
TABLE STATEMENT

Syntax:
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
MYSQL Constraints
SQL constraints are used to specify rules for the data in a table.

Constraints can be column level or table level. Column level constraints apply to a
column, and table level constraints apply to the whole table.

The following constraints are commonly used in SQL:


Not Null: Ensures that a column cannot have a NULL value.
Unique: Ensures that all values in a column are different.
Primary Key: A combination of a NOT NULL and UNIQUE. Uniquely identifies each
row in a table.
Foreign Key: Prevents actions that would destroy links between tables.
Check: Ensures that the values in a column satisfies a specific condition.
Default: Sets a default value for a column if no value is specified.
NOT NULL Constraints
By default the column can hold NULL values.
The NOT NULL constraint enforces a column to Not accept NULL values.

Not Null on CREATE TABLE

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
Unique Constraints
The Unique constraint ensures that all value in a column are different.
You can have many unique constraints per table but only one primary key
constraints per table.
Unique Constraints
UNIQUE CONSTRAINT at column level
CREATE TABLE Persons (ID int NOT NULL, LastName varchar(255) NOT NULL,
FirstName varchar(255), Age int, UNIQUE (ID));

UNIQUE CONSTRAINT at Table level

CREATE TABLE Persons (ID int NOT NULL, LastName varchar(255) NOT NULL,
FirstName varchar(255), Age int, CONSTRAINT UC_Person UNIQUE (ID,LastName));
Primary Key Constraints
The Primary key constraint uniquely identifies each record in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only one primary key; and in the table, this primary key can
consists of single or multiple columns.
Primary Key Constraints
Example: Primary Key constraint Defined at Column Level
CREATE TABLE Persons ( ID int primary key, LastName varchar(255) NOT NULL,
FirstName varchar(255), Age int);

Example: Primary Key constraint Defined at Table Level


CREATE TABLE Persons (ID int, LastName varchar(255) NOT NULL,
FirstName varchar(255), Age int, CONSTRAINT PK_Person PRIMARY KEY (ID,
LastName));
Foreign Key Constraints
Foreign Key Constraints
• The foreign Key constraint is used to prevent actions that would destroy links
between tables.
• The foreign key is a field in one table, that refers to the primary key in
another table.
Persons Table

Orders Table
Foreign Key on Create Table
The following SQL creates a foreign Key on the personID column when the Orders
table is created.
CREATE TABLE Orders (OrderID int NOT NULL, OrderNumber int NOT NULL,
PersonID int, PRIMARY KEY (OrderID), FOREIGN KEY (PersonID) REFERENCES
Persons(PersonID));

Defining Multiple column as foreign Key

CREATE TABLE Orders (OrderID int NOT NULL,OrderNumber int NOT NULL,
PersonID int, PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)
);
MYSQL CHECK CONSTRAINT
MYSQL CHECK CONSTRAINT
The check constraint is used to limit the value range that can be placed in a column.
CHECK on CREATE TABLE
CREATE TABLE Persons (ID int NOT NULL,
LastName varchar(255) NOT NULL, FirstName varchar(255), Age int,
CHECK (Age>=18)
);

CHECK constraint on multiple column on CREATE TABLE


CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);
MYSQL Default CONSTRAINT
The default constraint is used to set a default value for a column
Default constraint on CREATE TABLE
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT ’Hyderabad'
);
Default constraint on multiple column on CREATE TABLE
CREATE TABLE Orders (
ID int NOT NULL,
OrderNumber int NOT NULL,
OrderDate date DEFAULT CURRENT_DATE()
);
Alter Command (DDL Command)
Alter Command- Adding new columns
Adding New column
A new column can be added in the table.

Syntax:
Alter table table_name add column_name datatype;

Example:

Alter table customers add email varchar(255);


Alter Command- modifying existing columns
Modifying Existing Columns
• This command change a column datatype, size, and default value.
• It can increase or decrease the size of the column.
• Note: we can only decrease the size of a table if table is empty else table size cannot be decreased.

Syntax:
Alter table table_name modify column column_name datatype;

Example:
Increasing size of column in a table
Alter table student modify column age varchar2(40);
Decreasing size of column in a table can possible if table column does not holds the data.
Alter Table student modify column age varchar2(15);
Alter Command (DDL Command)
Alter Table- Drop Column
It drop a column from the table which may or may not contain the data.
Only one column can be dropped at a time.
Once a column is dropped it can not recovered

Syntax:
ALTER TABLE table_name Drop column column_name;
Example:
Alter table customers Drop column email;
Defining Integrity Constraint by Alter command
Drop Command (DDL Command)
Defining Integrity Constraint by Alter command
Truncate Command (DDL Command)
Rename Command (DDL Command)

Method-1

Rename Table old_table_name to new_table_name;

Method-2
Alter table old_table_name rename new_table_name;
Rename Command (DDL Command)
Rename offers more flexibility. It allows renaming multiple tables in one statement.

Example
Rename Table products to products_old, products_new to products;
Display Command
DML COMMANDS
INSERT STATEMENT
INSERT INTO statement is used to insert new records in a table.
It is possible to write insert into statements in two ways:
1. If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of the
values is in the same order as the columns in the table.

INSERT INTO table_name VALUES (value1, value2, value3, ...);

2. Specify both the column names and the values to be inserted:

Insert into table_name (column1, column2, column3,….) values (value1,


value2,…,valuen);
INSERT STATEMENT
INSERT STATEMENT
Null value Concept
Update STATEMENT
Update STATEMENT
Delete Command
DQL COMMANDS
Select Command
Select Command Syntax
Select Distinct Statement
Select Example Without Distinct Statement
The following SQL statement selects all (including the duplicates) values
from the "Country" column in the "Customers" table:

SELECT Country FROM Customers;


MYSQL Where Clause
The where clause is used to filter records.
Where Syntax:
SELECT column1, column2, ... FROM table_name WHERE condition;
Where Clause Example:
SELECT * FROM Customers WHERE Country = 'Mexico';
Comparison Operators in Where Clause
Operators in Where Clause
Defining a column Alias
Arithmetic Operations
Arithmetic Operations
Arithmetic Operations
MYSQL Logical Operators
MYSQL AND, OR, and Not Operators (Logical
Operators)
• The where clause can be combined with AND, OR and Not operators
Logical Operator AND
Logical Operator OR
Logical Operator NOT
Logical Operator AND, NOT, and OR
Logical Operator AND, NOT, and OR
Between Logical Operator
Between allows the selection of rows that contain values within a specified
upper and lower limit.

Syntax:
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN
value1 AND value2;

Example:
Select * From teachers where salary between 18000 and 20000.
Not Between Logical Operator
It allows selection of rows that does not contain values within a specified lower
and upper limit.
Syntax:
SELECT column_name(s) FROM table_name WHERE column_name Not
BETWEEN
value1 AND value2;

Example:
Select * From teachers where salary not between 18000 and 20000.
Between with IN Logical Operator
Between allows the selection of rows that contain values within a specified
upper and lower limit. In addition it did not show rows with selected columnID
Syntax:
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN
value1 AND value2 AND column_name not in (value1, value2, value3);

Example:
Select * from products where price between 10 and 20 and category not in
(1,2,3);
Mysql IN Logical Operator
The IN operator allows you to specify multiple values in a where clause.
The IN operator is shorthand for multiple OR conditions.
IN Syntax:
Select column1, column2..,column N from table where column_name IN(value1,
value2, ….);
OR
Select column1, column2..,column N from table where column_name IN(select
statement);
Example Syntax:
SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'UK');

You might also like