Professional Documents
Culture Documents
MySQL Installation
MySQL Installation
• MySQL is free, Open-source software for which the source code can
be download.
To build a web site that shows data from a database, you will need:
• RDBMS is the basis for all modern database systems such as MySQL, Microsoft
SQL Server, Oracle, and Microsoft Access.
• 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.
• 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;
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.
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);
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));
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)
);
Syntax:
Alter table table_name add column_name datatype;
Example:
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
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.
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');