Friday, August 4, 2023

MySQL CREATE DATABASE Statement

 MySQL CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a new SQL database.

Syntax

CREATE DATABASE databasename;

CREATE DATABASE Example

The following SQL statement creates a database called “testDB”:

Example

CREATE DATABASE testDB;

MySQL DROP DATABASE Statement

The DROP DATABASE statement is used to drop an existing SQL database.

Syntax

DROP DATABASE databasename;

Note: Be careful before dropping a database. Deleting a database will result in loss of complete information stored in the database

DROP DATABASE Example

The following SQL statement drops the existing database “testDB”:

Example

DROP DATABASE testDB;

Tip: Make sure you have admin privilege before dropping any database. Once a database is dropped, you can check it in the list of databases with the following SQL command: SHOW DATABASES;

MySQL Workbench

It is a visual database designing or GUI tool used to work with database architects, developers, and Database Administrators. This visual tool supports SQL development, data modeling, data migration, and comprehensive administration tools for server configuration, user administration, backup, and many more. It allows us to create new physical data models, E-R diagrams, and SQL development (run queries, etc.).

To create a new database using this tool, we first need to launch theMYSQL WORKBENCH and log in using the username and password that you want. It will show the following screen:

Now do the following steps for database creation:

1. Go to the Navigation tab and click on the Schema menu. Here, we can see all the previously created databases. If we want to create a new database, right-click under the Schema menu and select Create Schema or click the database icon (red rectangle), as shown in the following screen.

2. The new Schema window screen open. Enter the new database name (for example, employeedb) and use default character set and collation. Now, click on the Apply button as shown in the screen below:

3. A new popup window appears. Click on the Apply button.

4. A new popup screen appears. Click on the Finish button to complete the database creation.

5. After successful database creation, we can see new databases in the Schema menu. If we do not see this, click on the refresh icon into the Schema menu.

6. We can see more information about the database by selecting the database and click on the ‘i’ icon. The information window displays several options, like Table, Triggers, Indexes, Users, and many more.

7. MySQL Workbench does not provide an option to rename the database name, but we can create, update, and delete the table and data rows from the database.

Thursday, August 3, 2023

MySQL Operators

                           MySQL Operators

Arithmetic Operators in MySQL

The MySQL Arithmetic Operators are used to perform Arithmetic operations on column data such as Addition, Subtraction, Multiplication, Division, and Modulus. The following table shows you the list of available MySQL Arithmetic operators.

In this article, we show you how to use these MySQL Arithmetic Operators with examples.

MySQL Arithmetic Operators Examples

The following are the list of example that helps you understand these operators to perform arithmetic operations.

MySQL Addition Operator

The MySQL Addition Operator is useful to add values. The following statements show you how to add values in MySQL. Within the third statement, we used the number in string format. However, MySQL converts them into an integer and returns the output of addition.

SELECT 10 + 2;

SELECT 10 + 20, 20 + 30;

SELECT '1990' + '200', '2005' + '2200';

MySQL Subtraction Operator

The MySQL Subtract Operator is used to subtract one value from the other.

SELECT 10 - 2;

SELECT 20 - 5, 30 - 46;

SELECT '2150' - '1550', 1200 - '200';

MySQL Multiplication Operator

The MySQL Multiplication Operator is used to Multiply one value with the other. Below MYSQL query shows you the same

SELECT 10 * 2;

SELECT 5 * 3, 15 * 5;

SELECT 21 * '3', '250' * 5;

MySQL Division Operator

The MySQL Division Operator Divides one value with the other. The examples below show you the same.

SELECT 10 / 2;

SELECT 2 / 3, 3 / 5;

SELECT 250 / '19', '1225' / 2, 12 / 0;

MySQL DIV

In MySQL, you can use this DIV to perform division on integer values.

SELECT 20 DIV 3;

SELECT 5 DIV 2, -40 DIV 3, -100 DIV -7;

SELECT '220' DIV 3, 400 DIV '65';

MySQL % Operator [Modulus]

The MySQL Modulus Operator or MOD function returns the remainder of the division.

SELECT 10 % 3;

SELECT 257 MOD 9, MOD(222, 19);

SELECT '2345' MOD 23;

Wednesday, August 2, 2023

MySQL Comments

                    MySQL Comments

Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements.

Single Line Comments

Single line comments start with — .

Any text between — and the end of the line will be ignored (will not be executed).

The following example uses a single-line comment as an explanation:

Example

-- Select all:
SELECT * FROM Customers;

The following example uses a single-line comment to ignore the end of a line:

Example

SELECT * FROM Customers -- WHERE City='Berlin';

The following example uses a single-line comment to ignore a statement:

Example

-- SELECT * FROM Customers;
SELECT * FROM Products;

Multi-line Comments

Multi-line comments start with /* and end with */.

Any text between /* and */ will be ignored.

The following example uses a multi-line comment as an explanation:

Example

/*Select all the columns
of all the records
in the Customers table:*/

SELECT * FROM Customers;

The following example uses a multi-line comment to ignore many statements:

Example

/*SELECT * FROM Customers;
SELECT * FROM Products;
SELECT * FROM Orders;
SELECT * FROM Categories;*/

SELECT * FROM Suppliers;

To ignore just a part of a statement, also use the /* */ comment.

The following example uses a comment to ignore part of a line:

Example

SELECT CustomerName, /*City,*/ Country FROM Customers;

The following example uses a comment to ignore part of a statement:

Example

SELECT * FROM Customers WHERE (CustomerName LIKE 'L%'
OR CustomerName LIKE 'R%' /*OR CustomerName LIKE 'S%'
OR CustomerName LIKE 'T%'*/
OR CustomerName LIKE 'W%')
AND Country='USA'
ORDER BY CustomerName;

Tuesday, August 1, 2023

MySQL NULL Functions

            MySQL NULL Functions

NULL functions are provided to perform operations on NULL values that are stored in our database tables.

A NULL value is like a placeholder in the database when we have data that is missing or the required data is not available. It is a flexible value that is not part of any practical data type and can be put in the column of any data type, like string, int, varchar etc.

Following are the various features of a NULL value −

  • The NULL value is different from a zero value or a field that contains a space. A record with a NULL value is one that has been left blank during record creation.
  • The NULL value assists us in removing ambiguity from data. Also, a NULL value is beneficial to maintain the uniform datatype across the column.

If a user entered their DOB in the column of their mobile number, ambiguity could arise when contact was required. To overcome this, we have to check the data before insertion and update any data that is not of the date datatype with a NULL function.

MySQL IFNULL() and COALESCE() Functions

Look at the following “Products” table:

Products

Suppose that the “UnitsOnOrder” column is optional, and may contain NULL values.

Look at the following SELECT statement:

SELECT ProductName, UnitPrice * (UnitsInStock + UnitsOnOrder)
FROM Products;

In the example above, if any of the “UnitsOnOrder” values are NULL, the result will be NULL.

MySQL IFNULL() Function

The MySQL IFNULL() function lets you return an alternative value if an expression is NULL.

The example below returns 0 if the value is NULL:

SELECT ProductName, UnitPrice * (UnitsInStock + IFNULL(UnitsOnOrder, 0))
FROM Products;

MySQL COALESCE() Function

Or we can use the COALESCE() function, like this:

SELECT ProductName, UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0))
FROM Products;

Monday, July 31, 2023

MySQL CASE Statement

       MySQL CASE Statement

The CASE statement goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause.

If there is no ELSE part and no conditions are true, it returns NULL.

CASE Syntax

Demo Database

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

OrderDetails

MySQL CASE Examples

The following SQL goes through conditions and returns a value when the first condition is met:

Example

The following SQL will order the customers by City. However, if City is NULL, then order by Country:

Example

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...