Tải bản đầy đủ (.pdf) (24 trang)

sql kho tài liệu training

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (277.13 KB, 24 trang )

LEARN SQL AND PHP
INTRODUCTION TO SQL


WHAT IS SQL ?!
• Structural Query Language is a language to
manage and
• To manage data inside database
• By manage we mean:








Selecting;
Deleting;
Updating;
Adding;
Creating on-demand Functions;
Views;
Procedures.


WHAT IS DATABASES
• A database is a collection of information that is
organized so that it can easily be accessed,
managed, and updated.



WHAT IS DATABASES
• FOOD






FISH
MEAT
VEGETABLES
FRUITS



WHAT IS DATABASES
RELATIONAL + DATABASES


DATA TYPE OF A DATABASE
• A database can contain multiple types of data:
• VARCHAR(n): characters where n is a number of characters
that admin chose;
• INT: integers;
• BLOB: for binary data fields(like images, files, maps,…);
• LONGBLOB: for long binary files;
• DATETIME: date and time field as: YYYY-MM-DD HH:mm:ss;
• DATE: for dates only as YYYY-MM-DD;
• TIME : for time fields;

• YEAR, DECIMAL, TIMESTAMP,…


CREATE DATABASE
• Regrouping multiple tables that hold a connected
information into one environment, represent a
database.
• Syntax: to create a database test_db
CREATE SCHEMA IF NOT EXISTS `test_db`;
USE `test_db` ;


CREATE A TABLE
• To create a table inside a database, let us assume
that we have a table called test_db that contains 2
columns (id and name) where id is primary key:
CREATE TABLE IF NOT EXISTS `test_db`.`test_tbl` ( `id`
INT NOT NULL, `name` VARCHAR(20) NOT NULL,
PRIMARY KEY (`id `))

• NOT NULL represent a mandatory field to be filled
by admins or users.


SELECT QUERY
• Select query consist of selecting all or specific rows
or columns of a table when a condition is true;
• It provide GROUP BY and ORDER BY functionality;
• Syntax:
DATA


QUERY

All data

SELECT * FROM tbl_name;

Specific columns

SELECT id, name, address FROM
tbl_name;

Specific rows

SELECT * FROM tbl_name WHERE
id_column >10;


SELECT QUERY
• GROUP BY:
• If sometimes, a specific user in a table has multiple rows of
data, we can regroup all of his rows into 1.
• Syntax:
SELECT * FROM tbl_name GROUP BY column_name;

• ORDER BY:
• To order data by ascending ASC or descending DESC;
• So if we want to re-order the table by placing
name_column from A to Z we can use the following query:
SELECT * FROM tbl_name GROUP BY column_name

ORDER BY column_name ASC;


SELECT QUERY
• LIMIT 1:
• If a user has multiple rows in a table and we want to select
at most 1 row for him we add LIMIT 1 clause to the query;
• Syntax:

SELECT * FROM tbl_name WHERE column_name = ‘X’
LIMIT 1;


INSERT QUERY
• Insert query help us to add more rows into a table;
• Syntax:
INSERT INTO tbl_name (name, address,
phone_number) VALUES (‘X’, ‘DC’, ‘1234567890’);

• If the primary key (usually an ID column) of the table
is not Auto-Increment, we should add id to the
query.
INSERT INTO tbl_name (id, name, address,
phone_number) VALUES (23, ‘X’, ‘DC’, ‘1234567890’);


UPDATE QUERY
• If we have a table called test_tbl and contain id as
primary key, name, and address, and we need to
update a row where an id=29 to new values:

• Syntax:
UPDATE test_tbl SET name=‘new name’, address =
‘new address’ WHERE id =29;


FOREIGN KEYS
• A foreign key in a table is a primary key of another;
• If we have the following table:
• Tbl_address
id

Name

Address

1

X

DC

2

Y

NY

3

Z


LA

• Tbl_phone
Another_id

id

Phone_number

5436

1

123456

5453

2

534535

5478

3

324227


FOREIGN KEY

• So, the id of the first table is a foreign key into the
other;
• To create a table with a foreign Key:
CREATE TABLE IF NOT EXIST tbl_phone (
new_id INT NOT NULL AUTO INCREMENT,
phone_number VARCHAR(18) NOT NULL,
PRIMARY KEY (new_id),
FOREIGN KEY (id) REFERENCES tbl_address);


JOINS
• To select multiple rows from different tables we use
JOIN;
• There is various types of JOINS but the most used is
INNER JOIN;
• Syntax:
SELECT tbl_phone.id, tbl_phone.phone_number,
tbl_address.name FROM tbl_phone INNER JOIN
tbl_address WHERE tbl_phone.id = tbl_phone.id


JOINS
• The result of the previous query is:
id

address

Phone_number

1


X

123456

2

Y

534535

3

Z

324227


FUNCTIONS – AVG
• AVG(): to calculate the average of a column when
a certain condition is true;
• Syntax:

SELECT avg(tbl_name.age_column) FROM tbl_name
WHERE age_column > 18;


FUNCTIONS – COUNT()
• TO count number of rows in a table;
• Syntax:


SELECT COUNT(id) FROM tbl_name;

• Or:
SELECT COUNT(id) FROM tbl_name WHERE id <18;


FUNCTIONS – MAX, MIN, SUM





MAX() to return the maximum value of a column;
MIN() to return the minimum value of a column;
SUM() to return the sum of a column;
Syntax:
Function

Query

MIN()

SELECT MIN(salary) FROM
tbl_name

MAX()

SELECT MAX(salary) FROM
tbl_name


SUM

SELECT SUM(salary) FROM
tbl_name


TABLES AND RELATIONS
• Primary Keys vs Foreign Keys
Id

Name

Address

12

John

WDC

13

Peter

NY

14

Sarah


LA

Id

Emp_id

Social Number

5426

12

56569

5427

13

23654

5428

14

71254


TABLES AND RELATIONS
• Primary Keys vs Foreign Keys

Id

Name

Address

12

John

WDC

13

Peter

NY

14

Sarah

LA

1:1
Id

Emp_id

Social Number


5426

12

56569

5427

13

23654

5428

14

71254


TABLES AND RELATIONS
• Primary Keys vs Foreign Keys
Id

Name

12

John


13

Peter

14

Sarah
1:N

Id

Emp_id

Project

5426

12

CIVIL

5427

13

ELECTRIC

5428

14


ELECTRONIC


TABLES AND RELATIONS
• Primary Keys vs Foreign Keys
Id

Name

12

John

13

Peter

14

Sarah
M:N

Id

Emp_id

Project

5426


12

MANAGER

5427

12

MANAGER

5428

14

IT SPECIALIST



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×