Monday, July 10, 2023

MySQL UPDATE Statement

    MySQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!

Demo Database

Below is a selection from the “Employees” table in the sample database:

UPDATE Table

The following SQL statement updates the first Employees(EmployeeID = 1) with a new contact person and a new city.

Example

UPDATE Employees
SET LastName = 'Davolio', City = 'Seattle'
WHERE EmployeeID = 1;

UPDATE Multiple Records

It is the WHERE clause that determines how many records will be updated.

The following SQL statement will update the City to “London” for all records where City is “Seattle”:

Example

UPDATE Employees
SET City = 'London'
WHERE City = 'Seattle';

Sunday, July 9, 2023

MySQL NULL Values

                    MySQL NULL Values

What is a NULL Value?

A field with a NULL value is a field with no value.

If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value.

Note:

A NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is one that has been left blank during record creation!

How to Test for NULL Values?

It is not possible to test for NULL values with comparison operators, such as =, <, or <>.

We will have to use the IS NULL and IS NOT NULL operators instead.

IS NULL Syntax

SELECT column_names
FROM table_name
WHERE column_name IS NULL;

IS NOT NULL Syntax

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

Demo Database

Below is a selection from the “Employees” table in the sample database:

Tip: Always use IS NULL to look for NULL values.

The IS NOT NULL Operator

The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).

The following SQL lists all Employees with a value in the “Address” field:

Example

SELECT LastName, FirstName, Address
FROM Employees
WHERE Address IS NOT NULL;

Saturday, July 8, 2023

MySQL INSERT INTO Statement

 MySQL INSERT INTO 

Statement

The INSERT INTO statement is used to insert new records in a table.

INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two ways:

  1. Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the INSER INTO syntax would be as follows:

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

Demo Database

Below is a selection from the “Employees” table in the sample database:

INSERT INTO Example

The following SQL statement inserts a new record in the “Employees” table:

Example

INSERT INTO Employees(LastName, FirstName, Title, Address, City)
VALUES ('Harry', 'singh', 'Business Manager', '7 rue Nationale', 'Strasbourg');

Did you notice that we did not insert any number into the EmployeeID field?
The EmployeeID column is an auto_increment field and will be generated automatically when a new record is inserted into the table.

Insert Data Only in Specified Columns

It is also possible to only insert data in specific columns.

The following SQL statement will insert a new record, but only insert data in the “LastName”, “City”, columns (EmployeeID will be updated automatically):

Example

INSERT INTO Employees(LastName, City)
VALUES ('Andrew', 'London');


Friday, July 7, 2023

MySQL ORDER BY Keyword

    MySQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

ORDER BY Syntax

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Demo Database

Below is a selection from the “Employees” table in the database:

Example

SELECT * FROM Employees
ORDER BY City;

ORDER BY DESC Example

The following SQL statement selects all employers from the “Employees” table, sorted DESCENDING by the “City” column:

Example

SELECT * FROM Employees
ORDER BY City DESC;

ORDER BY Several Columns Example

The following SQL statement selects all employers from the “Employees” table, sorted by the “City” and the “LastName” column. This means that it orders by City, but if some rows have the same City, it orders them by LastName:

Example

SELECT * FROM Employees
ORDER BY City, LastName;

ORDER BY Several Columns Example 2

The following SQL statement selects all employers from the “Employees” table, sorted ascending by the “City” and descending by the “LastName” column:

Example

SELECT * FROM Employees
ORDER BY City ASC, LastName DESC;

Thursday, July 6, 2023

MySQL AND, OR and NOT Operators

          MySQL AND,OR and NOT   Operators

The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one condition:

  • The AND operator displays a record if all the conditions separated by AND are TRUE.
  • The OR operator displays a record if any of the conditions separated by OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

OR Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

NOT Syntax

SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

AND Example

The following SQL statement selects all fields from “Employees” where City is “London” AND FirstName is “Nancy”:

SELECT * FROM Employees
WHERE City = 'London' AND FirstName = 'Nancy';

OR Example

The following SQL statement selects all fields from “Employees” where City is “London” OR “Auburn”:

SELECT * FROM Employees
WHERE City = 'London' OR City = 'Auburn';

The following SQL statement selects all fields from “Employees” where City is “London” OR “Auburn”:

SELECT * FROM Employees
WHERE City = 'London' OR City = 'Auburn';

NOT Example

The following SQL statement selects all fields from “Employees” where City is NOT “London”:

SELECT * From Employees
WHERE NOT City = 'London';

Combining AND, OR and NOT

You can also combine the AND, OR and NOT operators.

The following SQL statement selects all fields from “Employees” where FirstName is “Andrew” AND City must be “London” OR “Tacoma” (use parenthesis to form complex expressions):

SELECT * FROM Employees
WHERE FirstName = 'Andrew' AND (City = 'London' OR City = 'Tacoma');

The following SQL statement selects all fields from “Employees” where City is NOT “London” and NOT “Auburn”:

SELECT * FROM Customers
WHERE NOT City = 'London' AND NOT City = 'Auburn';

Wednesday, July 5, 2023

The MySQL WHERE Clause

    The MySQL WHERE Clause

The WHERE clause is used to filter records.

It is used to extract only those records that fulfill a specified condition.

WHERE Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Note: The WHERE clause is not only used in SELECT statements, it is also used in UPDATE, DELETE, etc.!

Demo Database

Below is a selection from the “Employees” table in the sample database:

WHERE Clause Example

The following SQL statement selects all the Employees from “London”:

SELECT * FROM Employees
WHERE City = 'London';

Text Fields vs. Numeric Fields

SQL requires single quotes around text values (most database systems will also allow double quotes).

However, numeric fields should not be enclosed in quotes:

SELECT * FROM Employees
WHERE EmployeeID = 1;

Operators in The WHERE Clause

The following operators can be used in the WHERE clause:

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...