sql kho tài liệu training

24 40 0
  • Loading ...
    Loading ...
    Loading ...

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Tài liệu liên quan

Thông tin tài liệu

Ngày đăng: 17/11/2019, 08:22

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 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 • 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 row for him we add LIMIT 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 X DC Y NY Z LA • Tbl_phone Another_id id Phone_number 5436 123456 5453 534535 5478 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 X 123456 Y 534535 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
- Xem thêm -

Xem thêm: sql kho tài liệu training , sql kho tài liệu training