0% found this document useful (0 votes)
9 views

SOME-KNOWLEDGE-ABOUT-SQL-SERVER

Uploaded by

thanhvinh7903
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SOME-KNOWLEDGE-ABOUT-SQL-SERVER

Uploaded by

thanhvinh7903
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SOME KNOWLEDGE ABOUT SQL SERVER

1. Create a new database:


CREATE DATABASE <Database_name>
USES <Database name>
GO
2. Create a new table:
CREATE TABLE <Table_name>
(
<Column_name> <datatype> <constraint>

)
/*
NOTE: Each table must have only 1 primary key. A primary key can consist of single or
multiple column.
If the primary key consist of single column, you can write:
<Column_name> <datatype> PRIMARY KEY
If the primary key consist of multiple column, you must write:
CONSTRAINT PK_<Table_name> PRIMARY KEY <column 1, column 2,…>
*/
3. Add Value into the Table:
INSERT <Table_name> (<Column_name>) VALUES (<values>)
/*NOTE:
- The values inserted must match the datatype of column.
- If you want to add values for all the column of the table, you can write:
INSERT <Table_name> VALUES (<values>)
*/
4. Add/remove a column.
- Add a column into the table:
ALTER TABLE <Table_name>
ADD COLUMN <Column_name> <datatype>
- Remove a column
ALTER TABLE <Table_name>
DROP COLUMN <Column_name>
5. Change the value in a column
UPDATE <Table_name> SET <Column_name> = <values> WHERE <Condition>
6. Searching data from a table:
SELECT <Column_name> FROM <Table_name>
7. Searching data from 2 or more tables
SELECT <Column_name> FROM <Table_name 1>
(INNER/LEFT/RIGHT/FULL JOIN) <Table_name 2> ON <Condition>
(INNER/LEFT/RIGHT/FULL JOIN) <Table_name 2> ON <Condition>

8. Searching data with conditio
SELECT <Column_name> FROM <Table_name>
WHERE <Condition>
9. Select max/min value.
SELECT TOP <Number of record> <Column_name or statistical function> FROM
<Table_name>
ORDER BY <Column_name or statistical function > ASC (min) / DESC (max)
9. Some statistical functions:
COUNT (<Column_name>):
SUM (<Column_name>):
AVG (<Column_name>):
GETDATE (): Return date of system
DATEDIFF (<interval>, <date 1>, <date>): return the different between 2 date.
/* NOTE: When you user statistical function, the code is:
SELECT <Column_name or statistical function> FROM <Table_name>
GROUP BY <Columns which aren’t in statistical function>*/
10. Some datatypes in SQL:
INT: integer
FLOAT: real numver
CHAR (N): string which has EXACTLY N characters
VARCHAR (N): string which has NO MORE THAN N characters
NCHAR (N): string which has EXACTLY N characters and contain special characters
NVARCHAR (N): string which has NO MORE THAN N characters and contain special
characters
DATE: format: yyyy/mm/dd
DATETIME: format: yyyy/mm/dd hh:mm:ss
BIT: return 0 or 1
/* Note: When you add data into table
- If the datatype isn’t INT or FLOAT, the data must be between ‘ and ’
- If the datatype is NCHAR or NVACHAR, there must be the characters N before ‘ ’.
- If the datatype is DATE or DATETIME, you must enter correct format: yyyy/mm/đd (DATE)
or yyyy/mm/dd hh:mm:ss (DATETIME) */

You might also like