Saturday, August 5, 2023

MySQL CREATE TABLE Statement

 MySQL CREATE TABLE Statement

The CREATE TABLE statement is used to create a new table in a database.

Syntax

CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

The column parameters specify the names of the columns of the table.

The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).

MySQL CREATE TABLE Example

The following example creates a table called “Customers” that contains five columns: CustomerID, CustomerName, ContactName, and Country:

Example

CREATE TABLE Customers (
CustomerID int,
CustomerName varchar(255),
ContactName int(10),
Country varchar(255)
);

The CustomerID column is of type int and will hold an integer.

The CustomerName, ContactName, and Country columns are of type varchar and will hold characters, and the maximum length for these fields is 255 characters.

MySQL INSERT INTO TABLE Example

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

Example

INSERT INTO Customers (CustomerID, CustomerName, ContactName, Country)
VALUES (1, 'Alfreds Futterkiste', 'Maria Anders', 'Germany');

By using the above step we can insert more data into it and after the data insertion table looks like as shown below:

And if you want to gain more knowledge on the topic insert into you can learn here https://medium.com/@bhavithach8/mysql-insert-into-statement-8cdc0670681a

Create Table Using Another Table

A copy of an existing table can also be created using CREATE TABLE.

The new table gets the same column definitions. All columns or specific columns can be selected.

If you create a new table using an existing table, the new table will be filled with the existing values from the old table.

Syntax

CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name
WHERE ....;

The following SQL creates a new table called “DuplicateTables” (which is a copy of the “Customers” table):

Example

CREATE TABLE DuplicateTable As
SELECT customername, contactname
FROM customers;

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;

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