SQL, or Structured Query Language, is the standard language for manipulating databases. It is a declarative language that allows the creation, manipulation and consultation of data stored in Relational Databases. In this text, we will cover four basic SQL commands: SELECT, INSERT, UPDATE and DELETE.
SELECT
The SELECT command is used to select data from one or more tables. The basic syntax is:
SELECT column1, column2, ..., columnN FROM table_name;
If you want to select all columns, you can use the asterisk (*) character instead of listing all columns. For example:
SELECT * FROM table_name;
You can also use the WHERE clause to filter the results. For example:
SELECT * FROM table_name WHERE column = 'value';
INSERT
The INSERT command is used to insert new records into a table. The basic syntax is:
INSERT INTO table_name (column1, column2, ..., columnN) VALUES (value1, value2, ..., valueN);
For example, if you have a table called 'customers' with columns 'name', 'email' and 'phone', you could insert a new customer as follows:
INSERT INTO customers (name, email, phone) VALUES ('John', 'joao@email.com', '123456789');
UPDATE
The UPDATE command is used to update existing records in a table. The basic syntax is:
UPDATE tablename SET column1 = value1, column2 = value2, ..., columnN = valueN WHERE condition;
For example, if you want to update the email for customer 'John' in the 'customers' table, you could do the following:
UPDATE clients SET email = 'new@email.com' WHERE name = 'John';
It is very important to always use the WHERE clause when using the UPDATE command. If you don't use it, all table records will be updated!
DELETE
The DELETE command is used to delete existing records from a table. The basic syntax is:
DELETE FROM table_name WHERE condition;
For example, if you wanted to delete the customer 'John' from the 'customers' table, you could do the following:
DELETE FROM customers WHERE name = 'John';
As with the UPDATE command, it is very important to always use the WHERE clause when using the DELETE command. If you don't use it, all table records will be deleted!
In summary, SELECT, INSERT, UPDATE and DELETE are basic SQL commands that allow data manipulation in Relational Databases. They are the basis for any database operation and are indispensable for any software developer or database administrator.