MySQL
Working with Tables and SQL
Commands
Learning Objectives
By the end of this lesson, you will be able to:
Learn to modify and delete a SQL table
Comprehend the table components in MySQL
Learn all the SQL statements and clauses with their syntaxes
Become acquainted with all the SQL keys and categorize
them
Understand how to write and use the SQL keys
Learning Objectives
By the end of this lesson, you will be able to:
Learn how to update and delete databases in SQL
Become acquainted with SQL commands and table
manipulation
Create full functional tables in the database
Learn Join and its types with syntaxes
Understand the significate of DELETE and UPDATE keywords
A Day in the Life of a Full Stack Developer
You are hired as a full-stack developer in an organization and have
been assigned to an application development project. You being the
owner of the project, have decided to manage the source code of the
project and maintain each change that happens in the code.
To do so, you would need a version control system, that is, Git or
GitHub. You explore more about Git in detail and help your team
members on working with Git.
Modifying and Deleting a SQL Table
UPDATE
The UPDATE statement is used to modify the existing records in the table.
Syntax:
UPDATE name_of_table
SET column1 = value1, column2 =
value2, ...
WHERE condition;
DELETE
The DELETE statement is used to delete a SQL table.
Syntax:
DELETE FROM name_of_table WHERE condition;
ALTER
The ALTER statement is used for adding, deleting, or modifying columns in an existing table.
Syntax:
ALTER TABLE name_of_table ADD column_name
datatype;
DROP TABLE
The DROP TABLE is used to delete an existing table in a database.
Syntax:
DROP TABLE name_of_table
Modifying and Deleting a SQL Database
Modifying a SQL Database
ALTER is used to change the features of a database.
Syntax:
ALTER privilege is required by the ALTER statement on the database.
ALTER {DATABASE | SCHEMA} database_name
alter_option …
alter_option: {
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
| [DEFAULT] ENCRYPTION [=] {'Y' | 'N'}
| READ ONLY [=] {DEFAULT | 0 | 1}
}
Deleting a Database
DROP DATABASE statement is used to delete an existing database.
Syntax:
DROP DATABASE database_name;
SQL Commands
MySQL Select
It is used for selecting data from a database.
The returned data is stored in a result table.
Syntax:
SELECT column1,
column2, ...
FROM name_of_table;
MySQL Where Clause
This is used to filter out the results.
It allows to specify selection criteria to select the required records.
Syntax:
SELECT field1, field2,...fieldN name_of_table1,
name_of_table2...
[WHERE condition1 [AND [OR]] condition2.....
MySQL AND/OR Operator
It is used for selecting data from a database.
The returned data is stored in a result table.
A AND B
Syntax:
TRUE FALSE NULL
TRUE TRUE FALSE NULL
FALSE FALSE FALSE FALSE
NULL NULL FALSE NULL
It is used for selecting data from a database.
MySQL AND/OR Operator
A or B
Syntax:
TRUE FALSE NULL
TRUE TRUE TRUE TRUE
FALSE TRUE FALSE NULL
NULL TRUE NULL NULL
MySQL OrderBy Keyword
It is used to sort the outcome in ascending or descending order.
Syntax:
SELECT column1, column2, ...
FROM name_of_table
ORDER BY column1, column2, ... ASC|
DESC;
MySQL GroupBy
Used for collecting data from multiple records and grouping the output.
Syntax:
SELECT expression_a, expression_b, ...
expression_n,
aggregate_function (expression)
FROM tables
[WHERE conditions]
GROUP BY expression_a, expression_b, ...
expression_n;
This is mostly used in SELECT statement along with SUM, COUNT, MAX, etc.
MySQL Having
SELECT expression_a, expression_b, ...
expression_n,
aggregate_function (expression)
FROM tables
[WHERE conditions]
GROUP BY expression_a, expression_b, ...
expression_n
HAVING condition;
Used by the GROUP clause. It returns the rows where the condition is
TRUE.
Syntax:
MySQL Keys
MySQL Keys
MySQL offers four keys:
MySQL
Keys
Unique
Key
Composite
Key
Primary
Key
Foreign
Key
Unique Key
Unique Key
The syntax to create a unique key in table is:
The syntax to insert multiple unique key into a table is:
If a unique constraint is not specified, SQL gives a name for that
column.
CREATE TABLE table_name(
col1 data_type,
col2 data_type UNIQUE,
...
);
CREATE TABLE table_name(
col1 column_definition,
col2 column_definition,
...
[CONSTRAINT constraint_name]
UNIQUE(col_name(s))
);
Composite Keys
A composite key permits you to distinguish every row of the
table interestingly
It is framed by more than one column
MySQL ensure the uniqueness of the columns when the
columns are consolidated
This key recognizes with more than one property
extraordinarily
A segment of a composite key can have various information
types
Composite Keys
Primary Keys
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY
(ID,LastName)
);
Syntax:
It helps to uniquely recognize each record in a table.
It contains unique values and not NULL values.
Foreign Keys
It is used to prevent actions that would impact connections between
tables.
Foreign Key Parent Key
Child Table Referenced or
Parent Table
Foreign Keys
The syntax to create a Foreign Key on the "ProductId" column when the
"Orders" table is created is:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
ProductID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (ProductId) REFERENCES
Persons(ProductId)
);
MySQL Distinct Keyword
The SELECT DISTINCT statement is used to return different values.
SELECT DISTINCT column1, column2, ...
FROM name_of_table;
Syntax:
MySQL Insert into Keyword
The MySQL Insert statement is used to insert single or multiple records into a table.
INSERT INTO table
(column1, column2, ... )
VALUES
(expression1, expression2, ... ),
(expression1, expression2, ... ),
...;
Syntax:
MySQL Get Last Inserted ID
The LAST_INSERT_ID() function returns the AUTO_INCREMENT ID of the last
row that is added or updated in a table.
LAST_INSERT_ID(expression)
Syntax:
MySQL Insert Multiple Records Command
This is used to insert multiple rows into a table.
LAST_INSERT_ID(exINSERT INTO name_of_table
(column_list)
VALUES
(value_list_1),
(value_list_2),
...
(value_list_n);
pression)
Syntax:
MySQL Joins
MySQL Joins
A Join statement is used to merge the rows from two or more tables.
Different types of JOINS are
(INNER) JOIN LEFT (OUTER) JOIN RIGHT (OUTER) JOIN FULL (OUTER) JOIN SELF JOIN
Inner Join
It returns records that have the same values in both the tables.
The keyword takes the records with matching values in both the tables.
SELECT column_name_(s)
FROM table1
INNER JOIN table2
ON table1.column_name =
table2.column_name;
Syntax:
Left Join
It returns the same records from the right table and all records from the left table.
It shows 0 records from the right side if no match is found.
SELECT column_name_(s)
FROM table1
LEFT JOIN table2
ON table1.column_name =
table2.column_name;
Syntax:
Right Join
It returns the same records from the left table and all records from the right table.
It shows 0 records from the left side if no match is found.
SELECT column_name_(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name =
table2.column_name;
Syntax:
Full Join
It returns all records when the value is the same, either on the right or left side.
Syntax:
SELECT column_name_(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Self Join
It is a regular join where the table is joined with itself.
Syntax:
SELECT column_name_(s)
FROM table1 T1, table1 T2
WHERE condition;
MySQL Delete and Update Records
DELETE Keyword
It is used to delete the current records from a table.
Syntax:
DELETE FROM table_name WHERE
[condition];
UPDATE Keyword
It is used to update the current records from a table.
It helps to modify the values of one or multiple columns or rows.
The syntax is:
UPDATE [LOW_PRIORITY] [IGNORE] table_name
SET
column_name1 = expr1,
column_name2 = expr2,
...
[WHERE condition];
Key Takeaways
The UPDATE statement is used to modify the existing records in
the table.
ALTER is used to change the features of a database.
A composite key permits you to distinguish every row of the table
interestingly.
The MySQL Insert statement is used to insert single or multiple
records into a table.
A Join statement is used to merge the rows from two or more
tables.
DELETE keyword is used to delete the current records from a
table.
Thank You

Lesson 2_Working_with_Tables_and_SQL_Commands.pptx

  • 1.
  • 2.
    Working with Tablesand SQL Commands
  • 3.
    Learning Objectives By theend of this lesson, you will be able to: Learn to modify and delete a SQL table Comprehend the table components in MySQL Learn all the SQL statements and clauses with their syntaxes Become acquainted with all the SQL keys and categorize them Understand how to write and use the SQL keys
  • 4.
    Learning Objectives By theend of this lesson, you will be able to: Learn how to update and delete databases in SQL Become acquainted with SQL commands and table manipulation Create full functional tables in the database Learn Join and its types with syntaxes Understand the significate of DELETE and UPDATE keywords
  • 5.
    A Day inthe Life of a Full Stack Developer You are hired as a full-stack developer in an organization and have been assigned to an application development project. You being the owner of the project, have decided to manage the source code of the project and maintain each change that happens in the code. To do so, you would need a version control system, that is, Git or GitHub. You explore more about Git in detail and help your team members on working with Git.
  • 6.
  • 7.
    UPDATE The UPDATE statementis used to modify the existing records in the table. Syntax: UPDATE name_of_table SET column1 = value1, column2 = value2, ... WHERE condition;
  • 8.
    DELETE The DELETE statementis used to delete a SQL table. Syntax: DELETE FROM name_of_table WHERE condition;
  • 9.
    ALTER The ALTER statementis used for adding, deleting, or modifying columns in an existing table. Syntax: ALTER TABLE name_of_table ADD column_name datatype;
  • 10.
    DROP TABLE The DROPTABLE is used to delete an existing table in a database. Syntax: DROP TABLE name_of_table
  • 11.
    Modifying and Deletinga SQL Database
  • 12.
    Modifying a SQLDatabase ALTER is used to change the features of a database. Syntax: ALTER privilege is required by the ALTER statement on the database. ALTER {DATABASE | SCHEMA} database_name alter_option … alter_option: { [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name | [DEFAULT] ENCRYPTION [=] {'Y' | 'N'} | READ ONLY [=] {DEFAULT | 0 | 1} }
  • 13.
    Deleting a Database DROPDATABASE statement is used to delete an existing database. Syntax: DROP DATABASE database_name;
  • 14.
  • 15.
    MySQL Select It isused for selecting data from a database. The returned data is stored in a result table. Syntax: SELECT column1, column2, ... FROM name_of_table;
  • 16.
    MySQL Where Clause Thisis used to filter out the results. It allows to specify selection criteria to select the required records. Syntax: SELECT field1, field2,...fieldN name_of_table1, name_of_table2... [WHERE condition1 [AND [OR]] condition2.....
  • 17.
    MySQL AND/OR Operator Itis used for selecting data from a database. The returned data is stored in a result table. A AND B Syntax: TRUE FALSE NULL TRUE TRUE FALSE NULL FALSE FALSE FALSE FALSE NULL NULL FALSE NULL
  • 18.
    It is usedfor selecting data from a database. MySQL AND/OR Operator A or B Syntax: TRUE FALSE NULL TRUE TRUE TRUE TRUE FALSE TRUE FALSE NULL NULL TRUE NULL NULL
  • 19.
    MySQL OrderBy Keyword Itis used to sort the outcome in ascending or descending order. Syntax: SELECT column1, column2, ... FROM name_of_table ORDER BY column1, column2, ... ASC| DESC;
  • 20.
    MySQL GroupBy Used forcollecting data from multiple records and grouping the output. Syntax: SELECT expression_a, expression_b, ... expression_n, aggregate_function (expression) FROM tables [WHERE conditions] GROUP BY expression_a, expression_b, ... expression_n; This is mostly used in SELECT statement along with SUM, COUNT, MAX, etc.
  • 21.
    MySQL Having SELECT expression_a,expression_b, ... expression_n, aggregate_function (expression) FROM tables [WHERE conditions] GROUP BY expression_a, expression_b, ... expression_n HAVING condition; Used by the GROUP clause. It returns the rows where the condition is TRUE. Syntax:
  • 22.
  • 23.
    MySQL Keys MySQL offersfour keys: MySQL Keys Unique Key Composite Key Primary Key Foreign Key
  • 24.
  • 25.
    Unique Key The syntaxto create a unique key in table is: The syntax to insert multiple unique key into a table is: If a unique constraint is not specified, SQL gives a name for that column. CREATE TABLE table_name( col1 data_type, col2 data_type UNIQUE, ... ); CREATE TABLE table_name( col1 column_definition, col2 column_definition, ... [CONSTRAINT constraint_name] UNIQUE(col_name(s)) );
  • 26.
    Composite Keys A compositekey permits you to distinguish every row of the table interestingly It is framed by more than one column MySQL ensure the uniqueness of the columns when the columns are consolidated This key recognizes with more than one property extraordinarily A segment of a composite key can have various information types
  • 27.
  • 28.
    Primary Keys CREATE TABLEPersons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, CONSTRAINT PK_Person PRIMARY KEY (ID,LastName) ); Syntax: It helps to uniquely recognize each record in a table. It contains unique values and not NULL values.
  • 29.
    Foreign Keys It isused to prevent actions that would impact connections between tables. Foreign Key Parent Key Child Table Referenced or Parent Table
  • 30.
    Foreign Keys The syntaxto create a Foreign Key on the "ProductId" column when the "Orders" table is created is: CREATE TABLE Orders ( OrderID int NOT NULL, OrderNumber int NOT NULL, ProductID int, PRIMARY KEY (OrderID), FOREIGN KEY (ProductId) REFERENCES Persons(ProductId) );
  • 31.
    MySQL Distinct Keyword TheSELECT DISTINCT statement is used to return different values. SELECT DISTINCT column1, column2, ... FROM name_of_table; Syntax:
  • 32.
    MySQL Insert intoKeyword The MySQL Insert statement is used to insert single or multiple records into a table. INSERT INTO table (column1, column2, ... ) VALUES (expression1, expression2, ... ), (expression1, expression2, ... ), ...; Syntax:
  • 33.
    MySQL Get LastInserted ID The LAST_INSERT_ID() function returns the AUTO_INCREMENT ID of the last row that is added or updated in a table. LAST_INSERT_ID(expression) Syntax:
  • 34.
    MySQL Insert MultipleRecords Command This is used to insert multiple rows into a table. LAST_INSERT_ID(exINSERT INTO name_of_table (column_list) VALUES (value_list_1), (value_list_2), ... (value_list_n); pression) Syntax:
  • 35.
  • 36.
    MySQL Joins A Joinstatement is used to merge the rows from two or more tables. Different types of JOINS are (INNER) JOIN LEFT (OUTER) JOIN RIGHT (OUTER) JOIN FULL (OUTER) JOIN SELF JOIN
  • 37.
    Inner Join It returnsrecords that have the same values in both the tables. The keyword takes the records with matching values in both the tables. SELECT column_name_(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name; Syntax:
  • 38.
    Left Join It returnsthe same records from the right table and all records from the left table. It shows 0 records from the right side if no match is found. SELECT column_name_(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name; Syntax:
  • 39.
    Right Join It returnsthe same records from the left table and all records from the right table. It shows 0 records from the left side if no match is found. SELECT column_name_(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name; Syntax:
  • 40.
    Full Join It returnsall records when the value is the same, either on the right or left side. Syntax: SELECT column_name_(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name = table2.column_name WHERE condition;
  • 41.
    Self Join It isa regular join where the table is joined with itself. Syntax: SELECT column_name_(s) FROM table1 T1, table1 T2 WHERE condition;
  • 42.
    MySQL Delete andUpdate Records
  • 43.
    DELETE Keyword It isused to delete the current records from a table. Syntax: DELETE FROM table_name WHERE [condition];
  • 44.
    UPDATE Keyword It isused to update the current records from a table. It helps to modify the values of one or multiple columns or rows. The syntax is: UPDATE [LOW_PRIORITY] [IGNORE] table_name SET column_name1 = expr1, column_name2 = expr2, ... [WHERE condition];
  • 45.
    Key Takeaways The UPDATEstatement is used to modify the existing records in the table. ALTER is used to change the features of a database. A composite key permits you to distinguish every row of the table interestingly. The MySQL Insert statement is used to insert single or multiple records into a table. A Join statement is used to merge the rows from two or more tables. DELETE keyword is used to delete the current records from a table.
  • 46.

Editor's Notes

  • #24 In MySQL, there is a single field or combination of fields that make sure all values that are going to be stored into the column are unique, which means the column cannot store the same values. For example, the name and class of students in the "student-info" table. MySQL allows us to use more than one column with UNIQUE values in the table. It accepts a null value, but MySQL allows only a null value. It makes sure the integrity of columns stores different values. It helps in preventing the records from storing the same values in the column. It stores only different values to maintain the integrity of the database. It works with a foreign key in maintaining the distinctiveness of the table and contains a null value.
  • #26 A composite key in MySQL is a mixture of at least two columns in a table that permits us to distinguish every row of the table interestingly. It is a kind of candidate key that is framed by more than one column. MySQL ensures the uniqueness of the columns just when they are consolidated. If they are taken exclusively, the uniqueness cannot be kept up with. A composite key is helpful when the table requires to recognize each record with more than one property extraordinarily. A segment used in the composite key can have various information types. In this way, there isn't a need to have a similar information type for the segments to make a composite key in MySQL.
  • #27 Composite key can be added in two different ways: Create statement Alter statement