Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 28
Course Learning
Using MySQL to Manage Databases
What is MySQL? • MySQL is currently the most popular database management system software used for managing the relational database. • Free and open-source software written in C and C++ • It is fast, scalable, and easy to use database management system in comparison with Microsoft SQL Server and Oracle Database. • It is commonly used in conjunction with PHP scripts for creating powerful and dynamic server-side or web-based enterprise applications. Why do we learn MySQL? • It’s the most common database in the web (client-server model) • Uses by: Facebook, Google,Twitter, • Is super simple (comparing to Oracle, PostgreSQL) Things to know about MySQL • First version was out on 1995 • It is actually owned by Oracle, since 2010 • When it happened, one of the founders quit and forked Maria-DB which is still free under “Open Source” The MySQL Meta-Data Information Schema MySQL server has a default database called “information_schema” TABLES table contains information about each table in the database. e.g, name, type,number of rows etc. COLUMNS table contains information about each column, such as the table it's belong to, the data type, etc. USER_PRIVILEGES table contains information about the users listed in the database (do not confuse with web-users accessing the website. The MySQL Meta-Data
MySQL Data Types
Each column has a predefined type and possibly a default value ★Integers:TINYINT, MEDIUMINT, BIGINT ★Strings:VARCHAR (strings), BLOB (for binaries) ★Dates:TIMESTAMP, DATE, DATETIME Set when the database schema is created What is SQL? • SQL is the standard language for dealing with Relational Databases. SQL can be used to insert, search, update, and delete database records. SQL can do lots of other operations, including optimizing and maintenance of databases. Types of SQL Statements • Data Definition Language (DDL) • Data Manipulation Language (DML) • Data Control Language (DCL) • Transaction Control Language (TCL) • Data Query Language (DQL) Types of SQL Statements SQL Commands • CREATE – defines the database structure schema • INSERT – inserts data into the row of a table • UPDATE – updates data in a database • DELETE – removes one or more rows from a table • SELECT – selects the attribute based on the condition described by the WHERE clause • DROP – removes tables and databases • ALTER TABLE - modifies a table. The MySQL SQL Commands Syntax 1. SELECT — extracts data from a database
SELECT column_name FROM table_name;
SELECT statements fetch data from a database.
The MySQL SQL Commands Syntax Example: Selecting particular rows SELECT EmployeeID, EmployeeName FROM Employee_Info;
--(*) is used to select all from the table
SELECT * FROM Employee_Info;
-- To select the number of records to return use:
SELECT TOP 3 * FROM Employee_Info; The MySQL SQL Commands Syntax Example: -- To create a backup of database 'Employee' SELECT * INTO EmployeeBackup FROM Employee;
--To select only few columns from Employee
SELECT EmployeeName, PhoneNumber INTO EmployeeContactDetails FROM Employee;
SELECT * INTO BlrEmployee
FROM Employee WHERE City = ‘Quezon'; The MySQL SQL Commands Syntax 2. UPDATE — updates data in a database
UPDATE table_name SET some_column = some_value WHERE some_column = some_value;
UPDATE statements allow us to edit rows in a table.
The MySQL SQL Commands Syntax Example: mysql> UPDATE tblPerson -> SET Birthdate = '30-07-2001' -> WHERE id = 1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 The MySQL SQL Commands Syntax 3. DELETE — deletes data from a database
DELETE FROM table_name
WHERE some_column = some_value;
DELETE statements remove rows from a table.
The MySQL SQL Commands Syntax Example:
DELETE FROM Employee_Info
WHERE EmployeeName=‘Macky'; The MySQL SQL Commands Syntax 4. INSERT INTO — inserts new data into a database
INSERT INTO table_name (column_1, column_2, column_3)
VALUES (value_1, ‘value_2’, value_3);
INSERT statements add a new row to a table.
The MySQL SQL Commands Syntax Example: INSERT INTO tblEmployeeInfo VALUES ('07', ‘Magic’,’Johnson', ‘9932323', ‘LA Cal', ‘California', ‘USA'); The MySQL SQL Commands Syntax 5. CREATE DATABASE — creates a new database
CREATE DATABASE databasename;
CREATE DATABASE statements create a new SQL database.
The MySQL SQL Commands Syntax Example: mysql> CREATE TABLE tblPerson -> ( -> id int auto_increment not null primary key, -> Firstname varchar(20), -> Lastname varchar(20), -> Age int(3) -> ); Query OK, 0 rows affected (0.09 sec) The MySQL SQL Commands Syntax 6. ALTER TABLE — modifies a table
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE statements add, delete, or modify columns in an
existing table. The MySQL SQL Commands Syntax Example #1: Adding a Column to the Table mysql> ALTER TABLE tblPerson -> ADD COLUMN Birthdate varchar(20); Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM tblPerson;
+----+-----------+----------+------+-----------+ | id | Firstname | Lastname | Age | Birthdate | +----+-----------+----------+------+-----------+ | 1 | Jay | Abaleta | 40 | NULL | +----+-----------+----------+------+-----------+ 1 row in set (0.00 sec) The MySQL SQL Commands Syntax 7. DROP • This statement is used to drop an existing table or a database.
DROP TABLE — deletes a table
DROP TABLE table_name;
DROP TABLE statements drop an existing table in a database.
The MySQL SQL Commands Syntax
The ‘DROP DATABASE’ Statement
• This statement is used to drop an existing database. The ‘DROP TABLE’ Statement • This statement is used to drop an existing table. The MySQL SQL Commands Syntax Example #1: The ‘DROP DATABASE’ Statement DROP DATABASE Employee;
Example #2: The ‘DROP TABLE’
Statement DROP Table Employee_Info; Database: tbldept Dbemployee Dbstudent tblempprof dbUser
Database Composed of the following:
1. Table 2. Fieldname 3. Records End of Presentation Thank You