SlideShare a Scribd company logo
Statement and Syntax Data Definition Statements   CREATE DATABASE  CREATE TABLE CREATE INDEX  CREATE VIEW  DROP DATABASE  DROP TABLE  DROP INDEX  DROP VIEW  ALTER TABLE  RENAME TABLE
CREATE DATABASE  CREATE DATABASE creates a database with the given name.  Syntax CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name Example CREATE schema test1;  CREATE database test1;
CREATE TABLE   creates a table with the given name.  You must have the CREATE privilege for the table. Syntax CREATE TABLE  [IF NOT EXISTS] tbl_name (create_definition,...)  [table_options] Example CREATE TABLE `test`.`students` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT , `name` VARCHAR( 30 ) NOT NULL , PRIMARY KEY ( `id` )  ) ENGINE = InnoDB
CREATE INDEX   creates a index with the given name.  Syntax CREATE  INDEX index_name ON tbl_name (index_col_name,...) Example create index tt on students(name)
CREATE VIEW The  CREATE VIEW  statement creates a new view, or replaces an existing  one if the OR REPLACE clause is given.  Syntax CREATE [OR REPLACE]  VIEW view_name [(column_list)]  AS select_statement Example CREATE VIEW test.v AS SELECT * FROM t;
DROP DATABASE  DROP DATABASE drops all tables in the database and deletes the  database. Be very careful with this statement!  Syntax DROP {DATABASE | SCHEMA} [IF EXISTS] db_name Example Drop database test;
DROP index  indexname DROP INDEX drops the index named index_name from the table  DROP INDEX  index_name  ON  tbl_name   DROP TABLE  removes one or more tables.  DROP TABLE [IF EXISTS] tbl_name [, tbl_name] ...
RENAME TABLE RENAME TABLE current_db.tbl_name TO other_db.tbl_name; ALTER TABLE You can rename a column using a CHANGE old_col_name new_col_name column_definition clause.  ALTER TABLE t1 CHANGE a b INTEGER; ALTER TABLE `t` ENGINE = MYISAM
INSERT UPDATE  DELETE  SELECT  TRUNCATE Data Manipulation Statements
Syntax INSERT INTO table_name VALUES (value1, value2, value3,...) INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) Example INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')  The INSERT INTO statement is used to insert a new row in a table.  INSERT
The UPDATE statement is used to update existing records in a table.  UPDATE UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Example UPDATE Persons SET Address='Nissestien 67', City='Sandnes' WHERE LastName='Tjessem' AND FirstName='Jakob'  Syntax
The DELETE statement is used to delete rows in a table.  DELETE  Syntax DELETE FROM table_name WHERE some_column=some_value Example DELETE FROM Persons WHERE LastName='Tjessem' AND FirstName='Jakob'
The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. SELECT column_name(s) FROM table_name [WHERE where_condition] SELECT Syntax SELECT LastName,FirstName FROM Persons  Example
Delete the records inside the table.  TRUNCATE TRUNCATE TABLE table_name  Syntax TRUNCATE TABLE Persons Example
Utility Statements  DESCRIBE  EXPLAIN  HELP  USE
DESCRIBE provides information about the columns in a table. The EXPLAIN statement can be used either as a synonym for DESCRIBE  Syntax {DESCRIBE | DESC} tbl_name [col_name] Example Describe test; DESCRIBE
Example HELP ‘select'  The HELP statement returns online information from the MySQL Reference manual. HELP ' search_string '  USE db_name The USE db_name statement tells MySQL to use the db_name database as the default (current) database for subsequent statements. The database remains the default until the end of the session or another USE statement is issued:  HELP Syntax
Create a database on the mysql. create database [databasename]; List all databases on the mysql. show databases; Switch to a database. use [db name]; To see all the tables in the db. show tables; To see database's field formats. describe [table name];
To delete a db. drop database [database name]; To delete a table. drop table [table name]; Show all data in a table. SELECT * FROM [table name]; Returns the columns and column information pertaining to the designated table. show columns from [table name];
Show certain selected rows with the value "whatever". SELECT * FROM [table name] WHERE [field name] = "whatever"; Show all records containing the name "Bob" AND the phone number '3444444'. SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444'; Show all records starting with the letters 'bob' AND the phone number '3444444'. SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444'; Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5. SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;
Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field. SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number; Show unique records. SELECT DISTINCT [column name] FROM [table name]; Show selected records sorted in an ascending (asc) or descending (desc). SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC; Return number of rows. SELECT COUNT(*) FROM [table name];
Change a users password from MySQL prompt. Login as root. Set the password. Update privs.  SET PASSWORD FOR 'user'@'hostname'= PASSWORD('passwordhere');  Joining Tables With our tables now separated by entity, we join the tables together in our SELECT queries and other statements to retrieve and manipulate related data. When joining tables, there are a variety of JOIN syntaxes available.  Typically developers use the INNER JOIN and OUTER JOIN syntaxes.
INNER JOIN Author Table Author_ID First_Name Last_name 1   Chad   Russell 2   Jon   Stephens 3   Mike   Hillyer Book_Author  ISBN   Author_ID 1590593324 1 1590593324 2
An INNER JOIN query returns one row for each pair or matching rows in the tables being joined. Take our Author and Book_Author tables as an example: SELECT First_Name, Last_Name, ISBN FROM Author INNER JOIN Book_Author ON Author.Author_ID = Book_Author.Author_ID; First_Name  Last_Name  ISBN  Chad  Russell  1590593324  Jon  Stephens  1590593324
OUTER JOIN   The third author in the Author table is missing because there are no corresponding rows in the Book_Author table. When we need at least one row in the result set for every row in a given table, regardless of matching rows, we use an OUTER JOIN query. There are three variations of the OUTER JOIN syntax: LEFT OUTER JOIN, RIGHT OUTER JOIN..  The syntax used determines which table will be fully represented. A LEFT OUTER JOIN returns one row for each row in the table specified on the left side of the LEFT OUTER JOIN clause. The opposite is true for the RIGHT OUTER JOIN clause.
In each case, a row of NULL values is substituted when a matching row is not present. The following is an example of a LEFT OUTER JOIN: SELECT First_Name, Last_Name, ISBNFROM Author LEFT OUTER JOIN Book_Author ON Author.Author_ID = Book_Author.Author_ID;  First_Name  Last_Name  ISBN  Chad  Russell  1590593324  Jon  Stephens  1590593324  Mike  Hillyer  NULL  The third author is returned in this example, with a NULL value for the ISBN column, indicating that there are no matching rows in the Book_Author table.
quit  (\q) Quit mysql. go  (\g) Send command to mysql server. exit  (\q) Exit mysql. Same as quit. prompt  (\R) Change your mysql prompt. status  (\s) Get status information from the server. use  (\u) Use another database. Takes database name as argument. exit  (\q) Exit mysql. Same as quit. ?  (\?) Synonym for `help'. help  (\h) Display this help. mysql Commands
SHOW ENGINES SHOW DATABASES SHOW STATUS SHOW TABLES SHOW TABLE STATUS USE DATABASES

More Related Content

PPTX
List and Dictionary in python
PPTX
HTML/CSS/java Script/Jquery
PPT
Sql operators & functions 3
PDF
Python programming : List and tuples
PPTX
sql function(ppt)
PPT
SQL : introduction
PPT
Introduction to sql
PPT
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
List and Dictionary in python
HTML/CSS/java Script/Jquery
Sql operators & functions 3
Python programming : List and tuples
sql function(ppt)
SQL : introduction
Introduction to sql
Views, Triggers, Functions, Stored Procedures, Indexing and Joins

What's hot (20)

PPTX
SQL Queries Information
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
DOCX
Top 40 sql queries for testers
PPT
Introduction to-sql
PPT
Mysql
PDF
MySQL: Indexing for Better Performance
PPTX
Sql commands
PPT
MySQL Functions
PPTX
Sql queries presentation
DOCX
CSC 433 Sample normalization SQL Question
PDF
Introduction to SQL..pdf
PDF
[APJ] Common Table Expressions (CTEs) in SQL
 
PDF
Data manipulation language
PPTX
Sql(structured query language)
PPT
PPTX
Html form tag
PDF
SQL Overview
PDF
Sql tutorial
PPTX
SQL Queries Information
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Top 40 sql queries for testers
Introduction to-sql
Mysql
MySQL: Indexing for Better Performance
Sql commands
MySQL Functions
Sql queries presentation
CSC 433 Sample normalization SQL Question
Introduction to SQL..pdf
[APJ] Common Table Expressions (CTEs) in SQL
 
Data manipulation language
Sql(structured query language)
Html form tag
SQL Overview
Sql tutorial
Ad

Viewers also liked (12)

PPTX
Chapter 6 database normalisation
PDF
Add edit delete in Codeigniter in PHP
PDF
Normalisation student summary
PPT
Codeigniter
PPTX
NORMALIZATION - BIS 1204: Data and Information Management I
PDF
Introduction to MVC of CodeIgniter 2.1.x
PPT
MS Access and Database Fundamentals
PPT
Introduction to Algorithms
PPT
Introduction to microsoft access
PDF
MSU Food Fraud Initiative Emering Issues & New Frontiers for FDA Regulation_2014
PPTX
Algorithms and Flowcharts
PPT
Entity relationship diagram (erd)
Chapter 6 database normalisation
Add edit delete in Codeigniter in PHP
Normalisation student summary
Codeigniter
NORMALIZATION - BIS 1204: Data and Information Management I
Introduction to MVC of CodeIgniter 2.1.x
MS Access and Database Fundamentals
Introduction to Algorithms
Introduction to microsoft access
MSU Food Fraud Initiative Emering Issues & New Frontiers for FDA Regulation_2014
Algorithms and Flowcharts
Entity relationship diagram (erd)
Ad

Similar to Mysql Statments (20)

PPTX
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
PPTX
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
PPTX
PPTX
SQL Tutorial for Beginners
PPT
MY SQL
PPTX
SQL PPT.pptx
PPT
PPTX
SQL : Structured Query Language
PPT
PPTX
MySQL Essential Training
PPTX
Oraclesql
ODP
Mysql1
PPT
ODP
PPTX
Creating database using sql commands
PPTX
DBMS and SQL(structured query language) .pptx
PDF
full detailled SQL notesquestion bank (1).pdf
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
SQL Tutorial for Beginners
MY SQL
SQL PPT.pptx
SQL : Structured Query Language
MySQL Essential Training
Oraclesql
Mysql1
Creating database using sql commands
DBMS and SQL(structured query language) .pptx
full detailled SQL notesquestion bank (1).pdf

More from SHC (20)

PPT
Perform brute force
 
PPT
AJAX ASP.Net
 
PDF
C++ plus data structures, 3rd edition (2003)
 
PDF
Inside Asp.Net Web Matrix
 
PDF
V Pro Bp08505 Phase Iii Edited
 
DOC
V Pro Bp08505 Phase Iii Edited
 
DOC
V Pro Bp08505 Phase Ii Edited
 
PDF
Intel® V Pro™ Technology
 
PPT
XForms with Linux
 
PPT
XForms
 
PPT
Rails
 
PPT
Call
 
PPTX
Action Mailer
 
PPT
Ruby Security
 
PPTX
Web Services
 
PDF
Pragmatic Agile Web Development With Rails.3rd Edition.2009
 
PPT
Ruby Basics
 
PPT
Ruby Installation
 
PPT
Mysql Fun
 
PPT
Mysql
 
Perform brute force
 
AJAX ASP.Net
 
C++ plus data structures, 3rd edition (2003)
 
Inside Asp.Net Web Matrix
 
V Pro Bp08505 Phase Iii Edited
 
V Pro Bp08505 Phase Iii Edited
 
V Pro Bp08505 Phase Ii Edited
 
Intel® V Pro™ Technology
 
XForms with Linux
 
XForms
 
Rails
 
Call
 
Action Mailer
 
Ruby Security
 
Web Services
 
Pragmatic Agile Web Development With Rails.3rd Edition.2009
 
Ruby Basics
 
Ruby Installation
 
Mysql Fun
 
Mysql
 

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation theory and applications.pdf
PDF
Machine learning based COVID-19 study performance prediction
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectroscopy.pptx food analysis technology
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
20250228 LYD VKU AI Blended-Learning.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
Empathic Computing: Creating Shared Understanding
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Cloud computing and distributed systems.
Encapsulation theory and applications.pdf
Machine learning based COVID-19 study performance prediction

Mysql Statments

  • 1. Statement and Syntax Data Definition Statements CREATE DATABASE CREATE TABLE CREATE INDEX CREATE VIEW DROP DATABASE DROP TABLE DROP INDEX DROP VIEW ALTER TABLE RENAME TABLE
  • 2. CREATE DATABASE CREATE DATABASE creates a database with the given name. Syntax CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name Example CREATE schema test1; CREATE database test1;
  • 3. CREATE TABLE creates a table with the given name. You must have the CREATE privilege for the table. Syntax CREATE TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_options] Example CREATE TABLE `test`.`students` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT , `name` VARCHAR( 30 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB
  • 4. CREATE INDEX creates a index with the given name. Syntax CREATE INDEX index_name ON tbl_name (index_col_name,...) Example create index tt on students(name)
  • 5. CREATE VIEW The CREATE VIEW statement creates a new view, or replaces an existing one if the OR REPLACE clause is given. Syntax CREATE [OR REPLACE] VIEW view_name [(column_list)] AS select_statement Example CREATE VIEW test.v AS SELECT * FROM t;
  • 6. DROP DATABASE DROP DATABASE drops all tables in the database and deletes the database. Be very careful with this statement! Syntax DROP {DATABASE | SCHEMA} [IF EXISTS] db_name Example Drop database test;
  • 7. DROP index indexname DROP INDEX drops the index named index_name from the table DROP INDEX index_name ON tbl_name DROP TABLE removes one or more tables. DROP TABLE [IF EXISTS] tbl_name [, tbl_name] ...
  • 8. RENAME TABLE RENAME TABLE current_db.tbl_name TO other_db.tbl_name; ALTER TABLE You can rename a column using a CHANGE old_col_name new_col_name column_definition clause. ALTER TABLE t1 CHANGE a b INTEGER; ALTER TABLE `t` ENGINE = MYISAM
  • 9. INSERT UPDATE DELETE SELECT TRUNCATE Data Manipulation Statements
  • 10. Syntax INSERT INTO table_name VALUES (value1, value2, value3,...) INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) Example INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger') The INSERT INTO statement is used to insert a new row in a table. INSERT
  • 11. The UPDATE statement is used to update existing records in a table. UPDATE UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Example UPDATE Persons SET Address='Nissestien 67', City='Sandnes' WHERE LastName='Tjessem' AND FirstName='Jakob' Syntax
  • 12. The DELETE statement is used to delete rows in a table. DELETE Syntax DELETE FROM table_name WHERE some_column=some_value Example DELETE FROM Persons WHERE LastName='Tjessem' AND FirstName='Jakob'
  • 13. The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. SELECT column_name(s) FROM table_name [WHERE where_condition] SELECT Syntax SELECT LastName,FirstName FROM Persons Example
  • 14. Delete the records inside the table. TRUNCATE TRUNCATE TABLE table_name Syntax TRUNCATE TABLE Persons Example
  • 15. Utility Statements DESCRIBE EXPLAIN HELP USE
  • 16. DESCRIBE provides information about the columns in a table. The EXPLAIN statement can be used either as a synonym for DESCRIBE Syntax {DESCRIBE | DESC} tbl_name [col_name] Example Describe test; DESCRIBE
  • 17. Example HELP ‘select' The HELP statement returns online information from the MySQL Reference manual. HELP ' search_string ' USE db_name The USE db_name statement tells MySQL to use the db_name database as the default (current) database for subsequent statements. The database remains the default until the end of the session or another USE statement is issued: HELP Syntax
  • 18. Create a database on the mysql. create database [databasename]; List all databases on the mysql. show databases; Switch to a database. use [db name]; To see all the tables in the db. show tables; To see database's field formats. describe [table name];
  • 19. To delete a db. drop database [database name]; To delete a table. drop table [table name]; Show all data in a table. SELECT * FROM [table name]; Returns the columns and column information pertaining to the designated table. show columns from [table name];
  • 20. Show certain selected rows with the value "whatever". SELECT * FROM [table name] WHERE [field name] = "whatever"; Show all records containing the name "Bob" AND the phone number '3444444'. SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444'; Show all records starting with the letters 'bob' AND the phone number '3444444'. SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444'; Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5. SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;
  • 21. Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field. SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number; Show unique records. SELECT DISTINCT [column name] FROM [table name]; Show selected records sorted in an ascending (asc) or descending (desc). SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC; Return number of rows. SELECT COUNT(*) FROM [table name];
  • 22. Change a users password from MySQL prompt. Login as root. Set the password. Update privs. SET PASSWORD FOR 'user'@'hostname'= PASSWORD('passwordhere'); Joining Tables With our tables now separated by entity, we join the tables together in our SELECT queries and other statements to retrieve and manipulate related data. When joining tables, there are a variety of JOIN syntaxes available. Typically developers use the INNER JOIN and OUTER JOIN syntaxes.
  • 23. INNER JOIN Author Table Author_ID First_Name Last_name 1 Chad Russell 2 Jon Stephens 3 Mike Hillyer Book_Author ISBN Author_ID 1590593324 1 1590593324 2
  • 24. An INNER JOIN query returns one row for each pair or matching rows in the tables being joined. Take our Author and Book_Author tables as an example: SELECT First_Name, Last_Name, ISBN FROM Author INNER JOIN Book_Author ON Author.Author_ID = Book_Author.Author_ID; First_Name Last_Name ISBN Chad Russell 1590593324 Jon Stephens 1590593324
  • 25. OUTER JOIN The third author in the Author table is missing because there are no corresponding rows in the Book_Author table. When we need at least one row in the result set for every row in a given table, regardless of matching rows, we use an OUTER JOIN query. There are three variations of the OUTER JOIN syntax: LEFT OUTER JOIN, RIGHT OUTER JOIN.. The syntax used determines which table will be fully represented. A LEFT OUTER JOIN returns one row for each row in the table specified on the left side of the LEFT OUTER JOIN clause. The opposite is true for the RIGHT OUTER JOIN clause.
  • 26. In each case, a row of NULL values is substituted when a matching row is not present. The following is an example of a LEFT OUTER JOIN: SELECT First_Name, Last_Name, ISBNFROM Author LEFT OUTER JOIN Book_Author ON Author.Author_ID = Book_Author.Author_ID; First_Name Last_Name ISBN Chad Russell 1590593324 Jon Stephens 1590593324 Mike Hillyer NULL The third author is returned in this example, with a NULL value for the ISBN column, indicating that there are no matching rows in the Book_Author table.
  • 27. quit (\q) Quit mysql. go (\g) Send command to mysql server. exit (\q) Exit mysql. Same as quit. prompt (\R) Change your mysql prompt. status (\s) Get status information from the server. use (\u) Use another database. Takes database name as argument. exit (\q) Exit mysql. Same as quit. ? (\?) Synonym for `help'. help (\h) Display this help. mysql Commands
  • 28. SHOW ENGINES SHOW DATABASES SHOW STATUS SHOW TABLES SHOW TABLE STATUS USE DATABASES