SQL Basics 2

Source: Internet
Author: User

Sub-query
Stand-alone sub-query
correlated subqueries in (SQL) exists (SQL)


Connection Query
Cross joins and,
Inner link INNER JOIN
Outer link LEFT OUTER join
Right outer join

Cartesian product on for screening


Views View
Query Statement Encapsulation
Create View name creation
As
Statement.....
ALTER VIEW name modification
As
Statement.....
Benefits of the View 5 tables 2 views can be divided into database user view permissions to ensure security
Views do not store data if an index is created on the view then the indexed view will have data
The query for a view is unordered, and the sort is not a result set.

TSQL Programming
--tsql programming
--Declaring variables
DECLARE @n int=1001
--Assigned value
Set @n=100 the first type
Select @[email protected]+1 the second type
Print @n

Set @n = (select count (*) from tblstudent) no problem.
Select @n=count (*) from tblstudent no problem


Set @n = (select Tsage from Tblstudent) when a subquery returns more than one worth of error
Select @count =tsage from Tblstudent This assigns the last value to the variable

Transaction
BEGIN Tran
declare @num int = 100
SELECT * FROM Tblstudent
If @num = 100
Rollback
Else
Commit

Stored Procedures
Excellent:
Faster execution-all compiled
Allows modular programming-similar method multiplexing
Safety--Prevent injection
Reduce network traffic-the name of the value transfer stored procedure
Disadvantages:
Put too much business logic in the database

Stored procedures for the database system beginning with the XP SP
Execute stored procedure exec sp_database Execute stored procedure

CREATE PROCEDURE Usp_aaa
@param int=0 Parameters
As
Begin
SELECT * FROM Tblarea
End
EXEC usp_aaa


Trigger
A trigger is a special type of stored procedure that cannot pass a parameter event trigger call
Inserted table and deleted table
Inserted table when insert or update is used
Deleted table is used when delete or update

After trigger ex post trigger
Instead OF trigger substitution triggers


Create Trigger Tri_delete on Tblclass
After delete
As
Begin
--To back up the record you just deleted to another table
INSERT INTO Tblclassdelete
Select Tclassname,tclassdesc from deleted
End


Delete Tblclass where tclassid=2

SELECT * FROM Tblclass
SELECT * FROM Tblclassdelete

SQL Foundation 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.