QuXiangjie / Study-Review

自己欠缺的还太多了,希望通过总结每天的学习内容,整理每天的思绪来丰富自己的知识库。我想成为一名优秀的金融数据分析师,并行发展技术与商业业务。博客内容为:数理统计、财务业务、Python(数据分析及可视化)、Excel(数据分析)、SQL、英文
0 stars 0 forks source link

SQL Summary #1

Open QuXiangjie opened 6 months ago

QuXiangjie commented 6 months ago

SQL Select

Select
From
Where
Group by
Having
Order by

Table Operations

Table Creation

Attribution name ,Type name ,(Not Null),(Primary Key)

CREATE TABLE Employee
(
EmpID Integer Not Null primary key,
Last_Name Varchar(15),
Address Varchar(30)
)

Table Changing and Removing Tables


ALTER TABLE table_name
ADD column_name datatype;

--以下是例子
ALTER TABLE Customers
ADD Email varchar(255);

--------------------------

ALTER TABLE table_name
DROP COLUMN column_name;

--以下是例子
ALTER TABLE Customers
DROP COLUMN Email;

--------------------------

--Name changes
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;

-- Data type changes
ALTER TABLE table_name
ALTER COLUMN column_name datatype;  (SQL Server / MS Access)
DROP TABLE table_name;

Data row operation

Delete Statement

--Delete certain rows

Delete from tablename 
where state='HI'

--Delete all rows
Delete from customer_T

Update Statement

Update Tablename
Set PositionNumber=44
where SID=400;

Select * from part
Update part
Set Price=24.95
where aprt_num='AT94'

Insert Statement

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

If you are adding all the colunmns, you can use this statement

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

SQL Comparison Operatiors

Operator is used in a WHERE clause to search for a specified pattern in a column.

Like

The percent sign % represents zero, one, or multiple characters The underscore sign _ represents one, single character

Others

image

Build in Function

image

Join

image

Subquery

The operator must be a standard comparison operator (=,<>,!=,>,>=,< or <=)

Select column name
from table name
where column name operator ANY
  (Select....)
image

Transaction Intergrity

Functions: Routines that return values and take input parameters Procedures: Routines that do not return values and can take input or output parmeters. Triggers: Routines that execute in response to a database event(Insert, update or delete)

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;