Sunday, August 20, 2023

Difference between MySQL and MongoDB

 Difference between MySQL and MongoDB

MySQL and MongoDB are the two most popular database used for the enterprise application. Although both databases are free and open-source, they also have a lot of differences. In this section, we are going to compare the differences between MySQL and MongoDB database system based on the various parameters.

What is MySQL?

MySQL is the popular database management system used for managing the relational database. It is open-source database software, which is supported by Oracle Company. It is fast, scalable, and easy to use database management system in comparison with Microsoft SQL Server and Oracle Database. It is commonly used with PHP scripts for creating powerful and dynamic server-side or web-based enterprise applications.

It is developed and supported by the Swedish Company, MySQL AB, and written in C and C++ programming languages. Many small and big companies use MySQL. MySQL supports many Operating Systems like Windows, Linux, MacOS, etc. with C, C++, and Java languages.

What is MongoDB?

MongoDB is an open-source, cross-platform, and document-oriented NoSQL database that provides high performance, a high volume of data storage, rich query language, and automatic scaling. It is written in C++ and developed and maintained by a company named 10gen. It is simple, easy to use and learn by the developers. It stores data in JSON-like format. MongoDB is designed to work on the concept of collection and document.

MongoDB supports many Operating Systems like Windows, Linux, MacOS, etc. with C, C++, PHP, Node.js, Python, Java, and Ruby languages. The main purpose of using the MongoDB database is its fast development features, big-data support, flexible deployment, and easy to use.

Let us see the following comparison chart to understand the essential differences between MySQL and MongoDB.

MySQL vs MongoDB

Saturday, August 19, 2023

MySQL Updating a View

                 MySQL Updating a View

A view can be updated with the CREATE OR REPLACE VIEW statement.

CREATE OR REPLACE VIEW Syntax

CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

The following SQL adds the “City” column to the “Brazil Customers” view:

Example

CREATE OR REPLACE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName, City
FROM Customers
WHERE Country = 'Brazil';

MySQL Dropping a View

A view is deleted with the DROP VIEW statement.

DROP VIEW Syntax

DROP VIEW view_name;

The following SQL drops the “Brazil Customers” view:

Example

DROP VIEW [Brazil Customers];

MySQL Views

                                        MySQL Views

MySQL CREATE VIEW Statement

In SQL, a view is a virtual table based on the result-set of an SQL statement.

A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.

You can add SQL statements and functions to a view and present the data as if the data were coming from one single table.

A view is created with the CREATE VIEW statement.

CREATE VIEW Syntax

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Note: A view always shows up-to-date data! The database engine recreates the view, every time a user queries it.

MySQL CREATE VIEW Examples

The following SQL creates a view that shows all customers from Brazil:

Example

CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = 'Brazil';

We can query the view above as follows:

Example

SELECT * FROM [Brazil Customers];

The following SQL creates a view that selects every product in the “Products” table with a price higher than the average price:

Example

CREATE VIEW [Products Above Average Price] AS
SELECT ProductName, Price
FROM Products
WHERE Price > (SELECT AVG(Price) FROM Products);

Thursday, August 17, 2023

MySQL Working With Dates

  MySQL Working With Dates

MySQL Dates

The most difficult part when working with dates is to be sure that the format of the date you are trying to insert, matches the format of the date column in the database.

As long as your data contains only the date portion, your queries will work as expected. However, if a time portion is involved, it gets more complicated.

MySQL Date Data Types

MySQL comes with the following data types for storing a date or a date/time value in the database:

  • DATE - format YYYY-MM-DD
  • DATETIME - format: YYYY-MM-DD HH:MI:SS
  • TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
  • YEAR - format YYYY or YY

Note: The date data type are set for a column when you create a new table in your database

Working with Dates

Look at the following table:

Orders Table

Orders

Now we want to select the records with an OrderDate of “2021–07–27” from the table above.

We use the following SELECT statement:

SELECT * FROM Orders WHERE OrderDate='2021-07-27'

Now, assume that the “Orders” table looks like this (notice the added time-component in the “OrderDate” column):

If we use the same SELECT statement as above:

SELECT * FROM Orders WHERE OrderDate='2008-11-11'
Orders

we will get no result! This is because the query is looking only for dates with no time portion.

Tip: To keep your queries simple and easy to maintain, do not use time-components in your dates, unless you have to!

Wednesday, August 16, 2023

MySQL AUTO INCREMENT Field

 MySQL AUTO INCREMENT Field

What is an AUTO INCREMENT Field?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table.

Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

MySQL AUTO_INCREMENT Keyword

MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature.

By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.

The following SQL statement defines the “Personid” column to be an auto-increment primary key field in the “Persons” table:

CREATE TABLE Persons (
Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);

To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:

ALTER TABLE Persons AUTO_INCREMENT=100;

When we insert a new record into the “Persons” table, we do NOT have to specify a value for the “Personid” column (a unique value will be added automatically):

INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen');

The SQL statement above would insert a new record into the “Persons” table. The “Personid” column would be assigned a unique value automatically. The “FirstName” column would be set to “Lars” and the “LastName” column would be set to “Monsen”.

MySQL CREATE INDEX Statement

 MySQL CREATE INDEX Statement

The CREATE INDEX statement is used to create indexes in tables.

Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries.

CREATE INDEX Syntax

Creates an index on a table. Duplicate values are allowed:

CREATE INDEX index_name
ON table_name (column1, column2, ...);

CREATE UNIQUE INDEX Syntax

Creates a unique index on a table. Duplicate values are not allowed:

CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ...);

MySQL CREATE INDEX Example

The SQL statement below creates an index named “idx_lastname” on the “LastName” column in the “Persons” table:

CREATE INDEX idx_lastname
ON Persons (LastName);

If you want to create an index on a combination of columns, you can list the column names within the parentheses, separated by commas:

CREATE INDEX idx_pname
ON Persons (LastName, FirstName);

DROP INDEX Statement

The DROP INDEX statement is used to delete an index in a table.

ALTER TABLE table_name
DROP INDEX index_name;

Monday, August 14, 2023

MySQL DEFAULT Constraint

  MySQL DEFAULT Constraint

The DEFAULT constraint is used to set a default value for a column.

The default value will be added to all new records, if no other value is specified.

DEFAULT on CREATE TABLE

The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is created:

The DEFAULT constraint can also be used to insert system values, by using functions like CURRENT_DATE():

DEFAULT on ALTER TABLE

To create a DEFAULT constraint on the "City" column when the table is already created, use the following SQL:

DROP a DEFAULT Constraint

To drop a DEFAULT constraint, use the following SQL:

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