Sessions
Sessions
Sessions
Let’s Start:
MySQL is a database system used on the web
MySQL is a database system that runs on a server
MySQL is ideal for both small and large applications
MySQL is very fast, reliable, and easy to use
MySQL uses standard SQL
MySQL compiles on a number of platforms
MySQL is free to download and use
MySQL is developed, distributed, and supported by Oracle Corporation
MySQL is named after co-founder Monty Wideness’s daughter: My
SQL vs MYSQL:
SQL: It is a structured query programming language that manages the
relational database management system
MYSQL: It is a relational database management system that uses SQL.
Table:
- CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
- DROP TABLE table_name;
- TRUNCATE TABLE table_name;
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Data Types:
- VARCHAR(size)
- TEXT(size)
- BOOL
- INT(size)
- INTEGER(size)
- FLOAT(size, d)
- DATE
Constraints:
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
Not Null:
o ID int NOT NULL,
UNIQUE:
o ID int NOT NULL UNIQUE,
Primary Key:
o ID int NOT NULL PRIMARY KEY,
o CONSTRAINT PK_Person PRIMARY KEY (ID)
FOREIGN KEY:
o FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
Check:
o Age int CHECK (Age>=18)
o CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sand
nes')
DEFAULT:
o City varchar(255) DEFAULT 'Sandnes'
o OrderDate date DEFAULT GETDATE()
AUTO INCREMENT:
o Personid int NOT NULL AUTO_INCREMENT,
id int(10) ,
user_id int(10),
Update Statement:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
o UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
o UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
o UPDATE Customers
SET ContactName='Juan';
Delete Statement:
DELETE FROM table_name WHERE condition;
o DELETE FROM Customers WHERE
CustomerName='Alfreds Futterkiste';
DELETE FROM table_name;
o DELETE FROM Customers;
Drop Table:
DROP TABLE Customers;
Select Data From Database:
SELECT column1, column2, ...
FROM table_name;
o SELECT CustomerName, City FROM Customers;
DISTINCT:
SELECT DISTINCT column1, column2, ...
FROM table_name;
o SELECT DISTINCT Country FROM Customers;
o SELECT Country FROM Customers;
IN:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
o SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
o SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
AND:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
o SELECT * FROM Customers
WHERE Country = 'Germany'
AND City = 'Berlin'
AND PostalCode > 12000;
OR:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
o SELECT *
FROM Customers
WHERE Country = 'Germany' OR Country = 'Spain';
NOT
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
o SELECT * FROM Customers
WHERE NOT Country = 'Spain';
LIKE:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;