DEV Community

Zeppa
Zeppa

Posted on

A Simple Intro to SQL

What is a Database?

A database is a collection of information. It helps organize and keep information easily accessible.

What is a Database Management System?

It is common for people to use the term 'database' to refer to a database management system (DBMS). However, The DBMS is actually a software package that is responsible for defining, manipulating, and managing information in a database.

What is SQL?

SQL is the acronym for Structure Query Language. It is a language used to manage data. It supports relational databases. These type of database models consists of tables with columns and rows. The data can then be modified using the INSERT, SELECT, UPDATE, and DELETE commands.

What is MySQL?

MySQL is a relational database management system (RDBMS). A relational database structure allows the identification and access of data in relation to another in the database. More specifically, these relationships are logical connections between different tables.

MySQL is used by many popular websites, such as Facebook, Twitter, and YouTube.

INSERT

The INSERT statement is used to insert new records in the database. If adding values for all columns of the table, make sure the order of the values are in the same order as the column in the table.

INSERT INTO table_name
VALUES (column1_value, column2_value, . . . );

However, the statement can also specify the column nales and the values to be inserted.

INSERT INTO table_name (column1, column2, . . . )
VALUES (column1_value, column2_value, . . . );

SELECT

The SELECT statement is used to select records from a database. The data retrieved is stored in a table. One simple statement selects all columns from the table.

SELECT * FROM table_name;

The columns can also be specified to retrieve only the data needed.

SELECT column1, column2, . . .
FROM table_name;

The JOIN clause is used to combine tables, based on their relationship. There are four different kinds of JOINs: JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. While JOIN is an INNER JOIN, the other three are OUTER JOINs.

Use an INNER JOIN when only the matching values of two or more tables are desired.

SELECT A.column1, A.column2, B.column3, B.column4 . . .
FROM tableA A
INNER JOIN tableB B
ON A.column = B.column;

Use a LEFT JOIN when records from the left table are desired. The left table is the one specified in the FROM clause. If there is no match in the right table, the result will be NULL.

SELECT A.column1, A.column2, B.column3, B.column4 . . .
FROM tableA A
LEFT JOIN tableB B
ON A.column = B.column;

Alternatively, use a RIGHT JOIN when records from the right table are desired. The right table is the one specified in the JOIN clause. If there is no match in the left table, the result will be NULL.

SELECT A.column1, A.column2, B.column3, B.column4 . . .
FROM tableA A
RIGHT JOIN tableB B
ON A.column = B.column;

The FULL JOIN is used to return all records when there is a matching record in either table.

SELECT A.column1, A.column2, B.column3, B.column4 . . .
FROM tableA A
FULL JOIN tableB B
ON A.column = B.column;

Since the resulting data of a SELECT statement can potentially return large data, the WHERE clause is used to specify a condition.

UPDATE

The UPDATE statement modifies the existing data in a table. Since the UPDATE statement can modify all records in a database, it is necessary to include a WHERE clause, which specifies the records to be updated.

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

DELETE

The DELETE statement is used to delete existing data in a table. Like the UPDATE statement, a WHERE clause identifies the records to be deleted. If all the WHERE clause is omitted, all records of the database will be deleted.

DELETE FROM table_name
WHERE condition;

CONCLUSION

The INSERT, SELECT, UPDATE, and DELETE are the CRUD (Create, Read, Update, Delete) SQL equivalents.

Top comments (0)