What is a database? MySQL- Some common database query statements

Tram Ho

1. What is a database?

Database (Database) , abbreviated as Database or DB, is a collection of Data that is logically related to each other. A database is a structured collection of related data stored in computers. A database is designed, built and stored with a defined purpose, such as storage, data access for applications or users.

Database table : A database usually consists of one or more tables (tables). Each table is identified by a name (eg Employee). The table contains columns (colum), records – row, which is the data of the table

2. Some database management systems?

Database management system (Database Management System) can be understood as a system designed to manage a certain amount of data automatically and orderly. These management actions include editing, deleting, saving information, and searching (retrieving information) in a certain data group.

Some popular database management systems today :

SQL server : is a database management system of Microsoft, and MySQL is an open source database management system that can run on many platforms such as Linux, WinXP … According to many people, Microsoft’s SQL Server is stronger. Better security than MySQL.

In the previous article, I introduced the basic MySql, along with how to create a database, the most popular database management system in the world, favored by developers.

Oracle database management system: is the most powerful database management system, running on all platforms.

SQlite: SQLite is a complete, compact relational database system, which can be installed inside other applications. SQLite is written below in the C programming language.

MongoDB: MongoDB is an open source and a document using NoSQL mechanism to query, it is written in C ++ language.

PostgreSql : PostgreSQL is also a database management system which supports very well in storing spatial data. PostgreSQL combined with the Postgis module allows users to effectively store spatial data layers.

3. Some commonly used database queries

First, you should know the basic knowledge of MySql.

3.1 SQL DISTINCT

► The SELECT DISTINCT statement is used to return only different values.

► In a table, 1 column can contain many duplicate values ​​and sometimes you just want to get a list of unique values.

► DISTINCT keyword can be used to return only unique unique values.

► Command structure:

SELECT DISTINCT column_name, column_name FROM table_name;

For example:

SELECT DISTINCT City FROM Customers;

=> The statement returns all ̀City results in the Customers table without duplicating any results from each other

3.2 SQL Where

► Where is a command to limit the search. The extra element in this query is WHERE to determine if a row has a certain property.

► Command structure:

WHERE <column1> <operator> <column2>

For example: SELECT * FROM Customers WHERE country = ‘USA’;

=> Returned results are limited to country condition = ‘USA’

3.3 SQL And Or

► AND and OR concatenate two or more conditions in the WHERE clause together.

► AND will display 1 line (result) if All conditions are met. The OR operator displays a single or multiple lines (result) if Any condition is satisfied

For example:

SELECT * FROM Persons

WHERE FirstName = ‘Lan’ AND LastName = ‘Pham’;

Results returned:

► Use OR to find people with the last name Lan or last name Nguyen

For example: SELECT * FROM Persons WHERE FirstName = ‘Lan’ AND LastName = ‘Nguyen’;

Results returned:

3.4 SQL Count

► Syntax: SELECT COUNT (first_name) FROM table_name

► COUNT (*): returns the number of selected rows in the table.

Count entire rows in 1 tables:

SELECT COUNT (*) FROM Table name

► For example:

SELECT COUNT (*) FROM Persons;

=> The result is 3

The following command will return the number of people older than 20:

► For example:

SELECT COUNT (*) FROM Persons WHERE Age> 20;

=> The result is 2 because of the Age> 20 condition

3.5 SQL ORDER BY

► The ORDER BY keyword is used to sort a set of records in a SELECT statement by one or more criteria.

► The keyword ORDER BY sorts the default records by ascending. To sort descending we use the keyword DESC.

► ORDER BY syntax

SELECT column_name, column_name

FROM table_name

ORDER BY column_name, column_name ASC | DESC;

► For example:

SELECT Persons.ID , Persons.name

FROM Persons

ORDER BY Persons.ID ASC;

=> Output results sorted in ascending order by ID

3.6 SQL GROUP BY

► Collection functions (such as SUM) usually need more functionality of the GROUP BY clause.

► The GROUP BY clause … was added to SQL because set functions (like SUM) return a set of values ​​in the column each time they are called, and without GROUP BY we cannot calculate them. is the sum of the values ​​for each individual group in the column.

► The syntax of GROUP BY is as follows:

SELECT table_name, SUM (first_name) FROM table_name GROUP BY column_name

► For example:

SELECT Company, SUM (Amount)

FROM Sales

GROUP BY Company;

=> The results will be grouped by Company: there are 3 results

3.7 SQL HAVING

► Having is a conditional statement of Group by.

► The HAVING clause … was added to SQL because the WHERE clause is not applicable to aggregation functions (such as SUM). Without HAVING, it is impossible to test conditions with set functions.

► The syntax of HAVING is as follows:

SELECT name_name, SUM (name_name)

FROM table_name

GROUP BY name_columns

HAVING SUM (name_column) condition values

► For example:

SELECT Company, SUM (Amount)

FROM Sales

GROUP BY Company

HAVING SUM (Amount)> 10000;

=> The results will be grouped by Conditional Company Amount> 10000: there are 2 results

Conclude

This article only hopes to help you understand more about the database, some database management systems, and frequently used queries when manipulating databases in MySQL. You need to learn more to be able to understand more deeply, combine commands together to practice well the statements in MySQL and effectively apply it to your work. You can refer to the Website in the reference link below to be able to learn and practice the best way!

References:

https://www.w3schools.com/sql/default.asp

https://blog.webico.vn/quan-tri-co-du-lieu-la-gi-cac-quan-tri-co-du-lieu-pho-bien-nhat-hien-nay/

Share the news now

Source : Viblo