SlideShare une entreprise Scribd logo
Basic SQL Commands
examples from
Beginning MySQL
21CSC205P - DATABASE
MANAGEMENT SYSTEMS
UNIT -3
SQL Commands
Constraints, Joins, Set Operations
Sub-Queries, Views, PL-SQL
Triggers, Cursors
Case Study: Implement all the queries using
SQL, PL-SQL, Cursor and Trigger
Starting MySQL
On the course server enter the command
mysql
You should then see the MySQL prompt
mysql>
To end your MySQL session use the quit command
mysql> quit;
Creating the database
• CREATE DATABASE <database name>;
• CREATE DATABASE username;
• On the course server you have only been
granted permission to create a database
whose name is your username.
Using a database
• USE <database name>;
• USE username;
• DROP <database name>;
• DROP username;
Deleting a database
• DROP DATABASE [IF EXISTS] <databasename>;
• DROP DATABASE username;
• This deletes the database and all tables and
contents. Use with caution.
Create Table
Backus Naur Form (BNF) Notation
<table definition>::=
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] <table name>
(<table element> [{, <table element>}...])
[<table option> [<table option>...]]
<table element>::=
<column definition>
| {[CONSTRAINT <constraint name>] PRIMARY KEY
(<column name> [{, <column name>}...])}
| {[CONSTRAINT <constraint name>] FOREIGN KEY [<index name>]
(<column name> [{, <column name>}...]) <reference definition>}
| {[CONSTRAINT <constraint name>] UNIQUE [INDEX] [<index name>]
(<column name> [{, <column name>}...])}
| {{INDEX | KEY} [<index name>] (<column name> [{, <column name>}...])}
| {FULLTEXT [INDEX] [<index name>] (<column name> [{, <column name>}...])}
Create Table (cont)
Backus Naur Form (BNF) Notation
• <column definition>::=
• <column name> <type> [NOT NULL | NULL] [DEFAULT <value>] [AUTO_INCREMENT]
• [PRIMARY KEY] [COMMENT '<string>'] [<reference definition>]
• <type>::=
• <numeric data type>
• | <string data type>
• | <data/time data type>
• <reference definition>::=
• REFERENCES <table name> [(<column name> [{, <column name>}...])]
• [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT }]
• [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT }]
• [MATCH FULL | MATCH PARTIAL]
• <table option>::=
• {ENGINE = {BDB | MEMORY | ISAM | INNODB | MERGE | MYISAM}}
• | <additional table options>
Basic MySQL Data Types
Integer Real Text
TINYINT FLOAT CHAR (<length>)
SMALLINT DOUBLE VARCHAR (<maxlength>)
MEDIUMINT DOUBLE PRECISION
INT REAL
INTEGER DECIMAL
BIGINT DEC
NUMERIC
FIXED
Create Table
Example
CREATE TABLE Parts
(
PartID INT NOT NULL,
PartName VARCHAR(40) NOT NULL,
CatID INT NOT NULL,
PRIMARY KEY (PartID)
);
Special Note
• If you are using Putty you can copy & paste
the SQL commands from the PowerPoint
slides into MySQL.
TABLE Parts
PartID PartName CatiID
Inserting elements
Backus Naur Form (BNF) Notation
<insert statement>::=
INSERT [LOW_PRIORITY | DELAYED] [IGNORE] [INTO]
{<values option> | <set option> | <select option>}
<values option>::=
<table name> [(<column name> [{, <column name>}...])]
VALUES ({<expression> | DEFAULT} [{, {<expression> | DEFAULT}}...])
[{, ({<expression> | DEFAULT} [{, {<expression> | DEFAULT}}...])}...]
<set option>::=
<table name>
SET <column name>={<expression> | DEFAULT}
[{, <column name>={<expression> | DEFAULT}}...]
<select option>::=
<table name> [(<column name> [{, <column name>}...])]
<select statement>
Inserting elements
INSERT INTO Parts
(PartID, PartName, CatID)
VALUES
(1001,'Guy wire assembly',503),
(1002,'Magnet',504);
INSERT INTO Parts
VALUES
(1003,'Regulator',505);
TABLE Parts
PartID PartName CatiID
1001 Guy wire assembly 503
1002 Magnet 504
1003 Regulator 505
1004 Brushes 504
1006 Generator 506
1006 Dump load system 506
1007 Power assembly 501
Select Statement
Backus Naur Form (BNF) Notation
<select statement>::=
SELECT
[<select option> [<select option>...]]
{* | <select list>}
[<export definition>]
[
FROM <table reference> [{, <table reference>}...]
[WHERE <expression> [{<operator> <expression>}...]]
[GROUP BY <group by definition>]
[HAVING <expression> [{<operator> <expression>}...]]
[ORDER BY <order by definition>]
[LIMIT [<offset>,] <row count>]
[PROCEDURE <procedure name> [(<argument> [{, <argument>}...])]]
[{FOR UPDATE} | {LOCK IN SHARE MODE}]
]
Select Statement (cont)
Backus Naur Form (BNF) Notation
<select option>::=
{ALL | DISTINCT | DISTINCTROW}
| HIGH_PRIORITY
| {SQL_BIG_RESULT | SQL_SMALL_RESULT}
| SQL_BUFFER_RESULT
| {SQL_CACHE | SQL_NO_CACHE}
| SQL_CALC_FOUND_ROWS
| STRAIGHT_JOIN
<select list>::=
{<column name> | <expression>} [[AS] <alias>]
[{, {<column name> | <expression>} [[AS] <alias>]}...]
<export definition>::=
INTO OUTFILE '<filename>' [<export option> [<export option>]]
| INTO DUMPFILE '<filename>'
<export option>::=
{FIELDS
[TERMINATED BY '<value>']
[[OPTIONALLY] ENCLOSED BY '<value>']
[ESCAPED BY '<value>']}
| {LINES
SELECT Examples
SELECT * FROM Parts;
SELECT PartID, PartName FROM Parts;
SELECT PartID, PartName FROM Parts
WHERE
CatiID = 504;
Joining Tables with SELECT
Backus Naur Form (BNF) Notation
<select statement>::=
SELECT
[<select option> [<select option>...]]
{* | <select list>}
[
FROM {<table reference> | <join definition>}
[WHERE <expression> [{<operator> <expression>}...]]
[GROUP BY <group by definition>]
[HAVING <expression> [{<operator> <expression>}...]]
[ORDER BY <order by definition>]
[LIMIT [<offset>,] <row count>]
]
<join definition>::=
{<table reference>, <table reference> [{, <table reference>}...]}
| {<table reference> [INNER | CROSS ] JOIN <table reference> [<join condition>]}
| {<table reference> STRAIGHT_JOIN <table reference>}
| {<table reference> LEFT [OUTER] JOIN <table reference> [<join condition>]}
| {<table reference> RIGHT [OUTER] JOIN <table reference> [<join condition>]}
| {<table reference> NATURAL [{LEFT | RIGHT} [OUTER]] JOIN <table reference>}
<table reference>::=
<table name> [[AS] <alias>]
[{USE | IGNORE | FORCE} INDEX <index name> [{, <index name>}...]]
<join condition>::=
ON <expression> [{<operator> <expression>}...]
| USING (<column> [{, <column>}...])
Create Books Table
CREATE TABLE Books
(
BookID SMALLINT NOT NULL PRIMARY KEY,
BookTitle VARCHAR(60) NOT NULL,
Copyright YEAR NOT NULL
);
Create Example Tables
• Books
• Authors
• AuthorBook
Insert data into Books
INSERT INTO Books
VALUES (12786, 'Letters to a Young Poet', 1934),
(13331, 'Winesburg, Ohio', 1919),
(14356, 'Hell's Angels', 1966),
(15729, 'Black Elk Speaks', 1932),
(16284, 'Noncomformity', 1996),
(17695, 'A Confederacy of Dunces', 1980),
(19264, 'Postcards', 1992),
(19354, 'The Shipping News', 1993);
Create Authors Table
CREATE TABLE Authors
(
AuthID SMALLINT NOT NULL PRIMARY KEY,
AuthFN VARCHAR(20),
AuthMN VARCHAR(20),
AuthLN VARCHAR(20)
);
Insert data into Books
INSERT INTO Authors
VALUES (1006, 'Hunter', 'S.', 'Thompson'),
(1007, 'Joyce', 'Carol', 'Oates'),
(1008, 'Black', NULL, 'Elk'),
(1009, 'Rainer', 'Maria', 'Rilke'),
(1010, 'John', 'Kennedy', 'Toole'),
(1011, 'John', 'G.', 'Neihardt'),
(1012, 'Annie', NULL, 'Proulx'),
(1013, 'Alan', NULL, 'Watts'),
(1014, 'Nelson', NULL, 'Algren');
Create AuthorBook Table
CREATE TABLE AuthorBook
(
AuthID SMALLINT NOT NULL,
BookID SMALLINT NOT NULL,
PRIMARY KEY (AuthID, BookID),
FOREIGN KEY (AuthID) REFERENCES Authors
(AuthID),
FOREIGN KEY (BookID) REFERENCES Books (BookID)
);
Insert Data into AuthorBook
INSERT INTO AuthorBook
VALUES (1006, 14356), (1008, 15729),
(1009, 12786), (1010, 17695),
(1011, 15729), (1012, 19264),
(1012, 19354), (1014, 16284);
Basic Join
SELECT BookTitle, Copyright, Authors.AuthID
FROM Books, AuthorBook, Authors
WHERE
Books.BookID=AuthorBook.BookID
AND
AuthorBook.AuthID=Authors.AuthID
ORDER BY Books.BookTitle;
Basic Join
SELECT BookTitle, Copyright, Authors.AuthID
FROM Books, AuthorBook, Authors
ORDER BY BookTitle;
What happens when we leave off the WHERE
clause?
Basic Join
SELECT BookTitle, Copyright, AuthID
FROM Books AS b, AuthorBook AS ab
WHERE b.BookID=ab.BookID
ORDER BY BookTitle;

Contenu connexe

Similaire à Unit_III_SQL-MySQL-Commands-Basic.pptx usefull

Similaire à Unit_III_SQL-MySQL-Commands-Basic.pptx usefull (20)

Introducing ms sql_server_updated
Introducing ms sql_server_updatedIntroducing ms sql_server_updated
Introducing ms sql_server_updated
 
A Tour to MySQL Commands
A Tour to MySQL CommandsA Tour to MySQL Commands
A Tour to MySQL Commands
 
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-note
 
Les09
Les09Les09
Les09
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
MYSQL
MYSQLMYSQL
MYSQL
 
Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tables
 
My sql1
My sql1My sql1
My sql1
 
MySQL
MySQLMySQL
MySQL
 
Less08 Schema
Less08 SchemaLess08 Schema
Less08 Schema
 
New tsql features
New tsql featuresNew tsql features
New tsql features
 
Introduction databases and MYSQL
Introduction databases and MYSQLIntroduction databases and MYSQL
Introduction databases and MYSQL
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction database
 
SQL WORKSHOP::Lecture 10
SQL WORKSHOP::Lecture 10SQL WORKSHOP::Lecture 10
SQL WORKSHOP::Lecture 10
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Les10
Les10Les10
Les10
 
My sql presentation
My sql presentationMy sql presentation
My sql presentation
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 

Dernier

Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17Celine George
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryEugene Lysak
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxheathfieldcps1
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfDr. M. Kumaresan Hort.
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxCeline George
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointELaRue0
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesashishpaul799
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 

Dernier (20)

Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 

Unit_III_SQL-MySQL-Commands-Basic.pptx usefull

  • 1. Basic SQL Commands examples from Beginning MySQL
  • 2. 21CSC205P - DATABASE MANAGEMENT SYSTEMS UNIT -3 SQL Commands Constraints, Joins, Set Operations Sub-Queries, Views, PL-SQL Triggers, Cursors Case Study: Implement all the queries using SQL, PL-SQL, Cursor and Trigger
  • 3. Starting MySQL On the course server enter the command mysql You should then see the MySQL prompt mysql> To end your MySQL session use the quit command mysql> quit;
  • 4. Creating the database • CREATE DATABASE <database name>; • CREATE DATABASE username; • On the course server you have only been granted permission to create a database whose name is your username.
  • 5. Using a database • USE <database name>; • USE username; • DROP <database name>; • DROP username;
  • 6. Deleting a database • DROP DATABASE [IF EXISTS] <databasename>; • DROP DATABASE username; • This deletes the database and all tables and contents. Use with caution.
  • 7. Create Table Backus Naur Form (BNF) Notation <table definition>::= CREATE [TEMPORARY] TABLE [IF NOT EXISTS] <table name> (<table element> [{, <table element>}...]) [<table option> [<table option>...]] <table element>::= <column definition> | {[CONSTRAINT <constraint name>] PRIMARY KEY (<column name> [{, <column name>}...])} | {[CONSTRAINT <constraint name>] FOREIGN KEY [<index name>] (<column name> [{, <column name>}...]) <reference definition>} | {[CONSTRAINT <constraint name>] UNIQUE [INDEX] [<index name>] (<column name> [{, <column name>}...])} | {{INDEX | KEY} [<index name>] (<column name> [{, <column name>}...])} | {FULLTEXT [INDEX] [<index name>] (<column name> [{, <column name>}...])}
  • 8. Create Table (cont) Backus Naur Form (BNF) Notation • <column definition>::= • <column name> <type> [NOT NULL | NULL] [DEFAULT <value>] [AUTO_INCREMENT] • [PRIMARY KEY] [COMMENT '<string>'] [<reference definition>] • <type>::= • <numeric data type> • | <string data type> • | <data/time data type> • <reference definition>::= • REFERENCES <table name> [(<column name> [{, <column name>}...])] • [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT }] • [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT }] • [MATCH FULL | MATCH PARTIAL] • <table option>::= • {ENGINE = {BDB | MEMORY | ISAM | INNODB | MERGE | MYISAM}} • | <additional table options>
  • 9. Basic MySQL Data Types Integer Real Text TINYINT FLOAT CHAR (<length>) SMALLINT DOUBLE VARCHAR (<maxlength>) MEDIUMINT DOUBLE PRECISION INT REAL INTEGER DECIMAL BIGINT DEC NUMERIC FIXED
  • 10. Create Table Example CREATE TABLE Parts ( PartID INT NOT NULL, PartName VARCHAR(40) NOT NULL, CatID INT NOT NULL, PRIMARY KEY (PartID) );
  • 11. Special Note • If you are using Putty you can copy & paste the SQL commands from the PowerPoint slides into MySQL.
  • 13. Inserting elements Backus Naur Form (BNF) Notation <insert statement>::= INSERT [LOW_PRIORITY | DELAYED] [IGNORE] [INTO] {<values option> | <set option> | <select option>} <values option>::= <table name> [(<column name> [{, <column name>}...])] VALUES ({<expression> | DEFAULT} [{, {<expression> | DEFAULT}}...]) [{, ({<expression> | DEFAULT} [{, {<expression> | DEFAULT}}...])}...] <set option>::= <table name> SET <column name>={<expression> | DEFAULT} [{, <column name>={<expression> | DEFAULT}}...] <select option>::= <table name> [(<column name> [{, <column name>}...])] <select statement>
  • 14. Inserting elements INSERT INTO Parts (PartID, PartName, CatID) VALUES (1001,'Guy wire assembly',503), (1002,'Magnet',504); INSERT INTO Parts VALUES (1003,'Regulator',505);
  • 15. TABLE Parts PartID PartName CatiID 1001 Guy wire assembly 503 1002 Magnet 504 1003 Regulator 505 1004 Brushes 504 1006 Generator 506 1006 Dump load system 506 1007 Power assembly 501
  • 16. Select Statement Backus Naur Form (BNF) Notation <select statement>::= SELECT [<select option> [<select option>...]] {* | <select list>} [<export definition>] [ FROM <table reference> [{, <table reference>}...] [WHERE <expression> [{<operator> <expression>}...]] [GROUP BY <group by definition>] [HAVING <expression> [{<operator> <expression>}...]] [ORDER BY <order by definition>] [LIMIT [<offset>,] <row count>] [PROCEDURE <procedure name> [(<argument> [{, <argument>}...])]] [{FOR UPDATE} | {LOCK IN SHARE MODE}] ]
  • 17. Select Statement (cont) Backus Naur Form (BNF) Notation <select option>::= {ALL | DISTINCT | DISTINCTROW} | HIGH_PRIORITY | {SQL_BIG_RESULT | SQL_SMALL_RESULT} | SQL_BUFFER_RESULT | {SQL_CACHE | SQL_NO_CACHE} | SQL_CALC_FOUND_ROWS | STRAIGHT_JOIN <select list>::= {<column name> | <expression>} [[AS] <alias>] [{, {<column name> | <expression>} [[AS] <alias>]}...] <export definition>::= INTO OUTFILE '<filename>' [<export option> [<export option>]] | INTO DUMPFILE '<filename>' <export option>::= {FIELDS [TERMINATED BY '<value>'] [[OPTIONALLY] ENCLOSED BY '<value>'] [ESCAPED BY '<value>']} | {LINES
  • 18. SELECT Examples SELECT * FROM Parts; SELECT PartID, PartName FROM Parts; SELECT PartID, PartName FROM Parts WHERE CatiID = 504;
  • 19. Joining Tables with SELECT Backus Naur Form (BNF) Notation <select statement>::= SELECT [<select option> [<select option>...]] {* | <select list>} [ FROM {<table reference> | <join definition>} [WHERE <expression> [{<operator> <expression>}...]] [GROUP BY <group by definition>] [HAVING <expression> [{<operator> <expression>}...]] [ORDER BY <order by definition>] [LIMIT [<offset>,] <row count>] ] <join definition>::= {<table reference>, <table reference> [{, <table reference>}...]} | {<table reference> [INNER | CROSS ] JOIN <table reference> [<join condition>]} | {<table reference> STRAIGHT_JOIN <table reference>} | {<table reference> LEFT [OUTER] JOIN <table reference> [<join condition>]} | {<table reference> RIGHT [OUTER] JOIN <table reference> [<join condition>]} | {<table reference> NATURAL [{LEFT | RIGHT} [OUTER]] JOIN <table reference>} <table reference>::= <table name> [[AS] <alias>] [{USE | IGNORE | FORCE} INDEX <index name> [{, <index name>}...]] <join condition>::= ON <expression> [{<operator> <expression>}...] | USING (<column> [{, <column>}...])
  • 20. Create Books Table CREATE TABLE Books ( BookID SMALLINT NOT NULL PRIMARY KEY, BookTitle VARCHAR(60) NOT NULL, Copyright YEAR NOT NULL );
  • 21. Create Example Tables • Books • Authors • AuthorBook
  • 22. Insert data into Books INSERT INTO Books VALUES (12786, 'Letters to a Young Poet', 1934), (13331, 'Winesburg, Ohio', 1919), (14356, 'Hell's Angels', 1966), (15729, 'Black Elk Speaks', 1932), (16284, 'Noncomformity', 1996), (17695, 'A Confederacy of Dunces', 1980), (19264, 'Postcards', 1992), (19354, 'The Shipping News', 1993);
  • 23. Create Authors Table CREATE TABLE Authors ( AuthID SMALLINT NOT NULL PRIMARY KEY, AuthFN VARCHAR(20), AuthMN VARCHAR(20), AuthLN VARCHAR(20) );
  • 24. Insert data into Books INSERT INTO Authors VALUES (1006, 'Hunter', 'S.', 'Thompson'), (1007, 'Joyce', 'Carol', 'Oates'), (1008, 'Black', NULL, 'Elk'), (1009, 'Rainer', 'Maria', 'Rilke'), (1010, 'John', 'Kennedy', 'Toole'), (1011, 'John', 'G.', 'Neihardt'), (1012, 'Annie', NULL, 'Proulx'), (1013, 'Alan', NULL, 'Watts'), (1014, 'Nelson', NULL, 'Algren');
  • 25. Create AuthorBook Table CREATE TABLE AuthorBook ( AuthID SMALLINT NOT NULL, BookID SMALLINT NOT NULL, PRIMARY KEY (AuthID, BookID), FOREIGN KEY (AuthID) REFERENCES Authors (AuthID), FOREIGN KEY (BookID) REFERENCES Books (BookID) );
  • 26. Insert Data into AuthorBook INSERT INTO AuthorBook VALUES (1006, 14356), (1008, 15729), (1009, 12786), (1010, 17695), (1011, 15729), (1012, 19264), (1012, 19354), (1014, 16284);
  • 27. Basic Join SELECT BookTitle, Copyright, Authors.AuthID FROM Books, AuthorBook, Authors WHERE Books.BookID=AuthorBook.BookID AND AuthorBook.AuthID=Authors.AuthID ORDER BY Books.BookTitle;
  • 28. Basic Join SELECT BookTitle, Copyright, Authors.AuthID FROM Books, AuthorBook, Authors ORDER BY BookTitle; What happens when we leave off the WHERE clause?
  • 29. Basic Join SELECT BookTitle, Copyright, AuthID FROM Books AS b, AuthorBook AS ab WHERE b.BookID=ab.BookID ORDER BY BookTitle;