Basic SQL syntax
SQL has a set of rules and instructions called Syntax (syntax). This article will quickly list for you the basic SQL syntax.
One thing you need to remember here is that SQL is not case sensitive, meaning INSERT
and insert
is the same, but usually we capitalize it clearly. A little different in MySQL, table names are case sensitive. Therefore, if you are working with MySQL, you must provide the exact table name as it is (in upper or lower case) in the database.
You can write SQL commands on the same line or span multiple lines.
Almost all database operations are performed through SQL commands. Below is a list of SQL commands syntax. Specifically, what these commands do and how to use them, we will learn more about in the following articles.
All SQL commands begin with a certain keyword such as SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, CREATE, USE, SHOW and all commands end with a semicolon (;). Sign ; Used to separate SQL commands when multiple commands are called at the same time.
Some of the most important SQL commands
- SELECT - extracts data from the database
- UPDATE - updates data in the database
- DELETE - deletes data from the database
- INSERT INTO - inserts new data into the database
- CREATE DATABASE - creates a new database
- ALTER DATABASE - modify the database
- CREATE TABLE - creates a new table
- ALTER TABLE - modifies a table
- DROP TABLE - deletes the table
- CREATE INDEX - creates index (search key)
- DROP INDEX - deletes an index
Different syntaxes in SQL
SELECT command in SQL
SELECT cot1, cot2, cot3,. cotN FROM ten_bang;
DISTINCT clause in SQL
SELECT DISTINCT cot1, cot2, cot3,.cotN FROM ten_bang;
WHERE clause in SQL
SELECT cot1, cot2, cot3,. cotN FROM ten_bang WHERE DIEU_KIEN;
AND/OR clause in SQL
SELECT cot1, cot2, cot3,. cotN FROM ten_bang WHERE DIEU_KIEN_1 {AND|OR} DIEU_KIEN_2;
IN clause in SQL
SELECT cot1, cot2, cot3,. cotN FROM ten_bang WHERE ten_cot IN (giatri_1, giatri_2,.giatri_N);
BETWEEN clause in SQL
SELECT cot1, cot2, cot3,. cotN FROM ten_bang WHERE ten_cot BETWEEN giatri_1 AND giatri_2;
LIKE clause in SQL
SELECT cot1, cot2, cot3,. cotN FROM ten_bang WHERE ten_cot LIKE { PATTERN };
ORDER BY clause in SQL
SELECT cot1, cot2, cot3,. cotN FROM ten_bang WHERE DIEU_KIEN ORDER BY ten_cot {ASC|DESC};
GROUP BY clause in SQL
SELECT SUM(ten_cot) FROM ten_bang WHERE DIEU_KIEN GROUP BY ten_cot;
COUNT clause in SQL
SELECT COUNT(ten_cot) FROM ten_bang WHERE DIEU_KIEN;
HAVING clause in SQL
SELECT SUM(ten_cot) FROM ten_bang WHERE DIEU_KIEN GROUP BY ten_cot HAVING (dieu kien la ham so hoc);
CREATE TABLE command in SQL
CREATE TABLE ten_bang( cot1 kieu_du_lieu, cot2 kieu_du_lieu, cot3 kieu_du_lieu, . cotN kieu_du_lieu, PRIMARY KEY(mot hoac nhieu cot) );
DROP TABLE command in SQL
DROP TABLE ten_bang;
CREATE INDEX command in SQL
CREATE UNIQUE INDEX ten_chi_muc ON ten_bang ( cot1, cot2,.cotN);
DROP INDEX command in SQL
ALTER TABLE ten_bang DROP INDEX ten_chi_muc;
DESC command in SQL
DESC ten_bang;
TRUNCATE TABLE command in SQL
TRUNCATE TABLE ten_bang;
ALTER TABLE command in SQL
ALTER TABLE ten_bang {ADD|DROP|MODIFY} ten_cot {kieu_du_lieu};
ALTER TABLE (Rename) command in SQL
ALTER TABLE ten_bang RENAME TO ten_bang_moi;
INSERT INTO command in SQL
INSERT INTO ten_bang( cot1, cot2,.cotN) VALUES ( giatri_1, giatri_2,.giatri_N);
UPDATE command in SQL
UPDATE ten_bang SET cot1 = giatri_1, cot2 = giatri_2,.cotN=giatri_N [ WHERE DIEU_KIEN ];
DELETE command in SQL
DELETE FROM ten_bang WHERE {DIEU_KIEN};
CREATE DATABASE command in SQL
CREATE DATABASE ten_database;
DROP DATABASE command in SQL
DROP DATABASE ten_database;
USE command in SQL
USE ten_database;
COMMIT command in SQL
COMMIT;
ROLLBACK command in SQL
ROLLBACK;
Above is the basic command syntax in SQL. In the next article we will learn about data types in SQL.
You should read it
- Del command in Windows
- How to use the command history function in Command Prompt
- Set command in Windows
- Cmd command in Windows
- Bitsadmin getdisplayname and bitsadmin geterror command in Windows
- The echo command in Windows
- Fc command in Windows
- Command at in Windows
- Doskey command in Windows
- 10 useful commands in Windows you should know
- How to use the which command in Linux
- The command reg add in Windows