SQL Server

Posted on 09/01/2020

SQL Server CREATE DATABASE

The CREATE DATABASE statement creates a new database. The following shows the minimal syntax of the CREATE DATABASE statement:

CREATE DATABASE database_name;

SQL Server DROP DATABASE

DROP DATABASE [ IF EXISTS ]
    database_name 
    [,database_name2,...];
Cannot drop database "database_name" because it is currently in use.

SQL Server CREATE TABLE

CREATE TABLE [database_name.][schema_name.]table_name (
    pk_column data_type PRIMARY KEY,
    column_1 data_type NOT NULL,
    column_2 data_type,
    ...,
    table_constraints
);

Example:

USE [QUANLYBANHANG]
GO

CREATE TABLE [dbo].[nhanvien] (
    MaNV INT PRIMARY KEY IDENTITY (1, 1),
    HoNV VARCHAR (50) NOT NULL,
    TenNV VARCHAR (50) NOT NULL,
    Phone VARCHAR(20)
);

SQL Server DROP TABLE

Sometimes, you want to remove a table that is no longer in use. To do this, you use the following DROP TABLE statement:

DROP TABLE [IF EXISTS] [database_name.][schema_name.]table_name;

SQL Server SELECT

SELECT
    select_list
FROM
    schema_name.table_name;
SELECT
    city,
    COUNT (*)
FROM
    sales.customers
WHERE
    state = 'CA'
GROUP BY
    city
ORDER BY
    city;
SELECT
    city,
    COUNT (*)
FROM
    sales.customers
WHERE
    state = 'CA'
GROUP BY
    city
HAVING
    COUNT (*) > 10
ORDER BY
    city;

1. SQL Syntax

1.1. SELECT Statement

SELECT column1, column2....columnN
FROM   table_name;

1.2. DISTINCT Clause

SELECT DISTINCT column1, column2....columnN
FROM   table_name;

1.3. WHERE Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  CONDITION;

1.4. AND/OR Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  CONDITION-1 {AND|OR} CONDITION-2;

1.5. IN Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  column_name IN (val-1, val-2,...val-N);

1.6. BETWEEN Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  column_name BETWEEN val-1 AND val-2;

1.7. LIKE Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  column_name LIKE { PATTERN };

1.8. ORDER BY Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  CONDITION
ORDER BY column_name {ASC|DESC};

1.9. GROUP BY Clause

SELECT SUM(column_name)
FROM   table_name
WHERE  CONDITION
GROUP BY column_name;

1.10. COUNT Clause

SELECT COUNT(column_name)
FROM   table_name
WHERE  CONDITION;

1.11. HAVING Clause

SELECT SUM(column_name)
FROM   table_name
WHERE  CONDITION
GROUP BY column_name
HAVING (arithematic function condition);

1.12. CREATE TABLE Statement

CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

1.13. DROP TABLE Statement

DROP TABLE table_name;

1.14. CREATE INDEX Statement

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

1.15. DROP INDEX Statement

ALTER TABLE table_name
DROP INDEX index_name;

1.16. DESC Statement

DESC table_name;

1.17. TRUNCATE TABLE Statement

TRUNCATE TABLE table_name;

1.18. ALTER TABLE Statement

ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};

1.19. ALTER TABLE Statement (Rename)

ALTER TABLE table_name RENAME TO new_table_name;

1.20. INSERT INTO Statement

INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);

1.21. UPDATE Statement

UPDATE table_name
SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE  CONDITION ];

1.22. DELETE Statement

DELETE FROM table_name
WHERE  {CONDITION};

1.23. CREATE DATABASE Statement

CREATE DATABASE database_name;

1.24. DROP DATABASE Statement

DROP DATABASE database_name;

1.25. USE Statement

USE database_name;

1.26. COMMIT Statement

COMMIT;

1.27. ROLLBACK Statement

ROLLBACK;