Skip to content

Commit 47f9a68

Browse files
Update README.md
1 parent 2df86d8 commit 47f9a68

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,66 @@
11
# Brush up your knowledge on SQL
2-
3-
<b>What is the command used to fetch first 5 characters of the string?</b><br/>
2+
3+
<b>1. What is the command used to fetch first 5 characters of the string?</b><br/>
44
There are many ways to fetch first 5 characters of the string - <br/>
55
a) Select SUBSTRING(StudentName,1,5) as studentname from student <br/>
66
b) Select RIGHT(Studentname,5) as studentname from student
7+
<br/>
8+
<br/> <b>2. How to create a Foreign key </b>
9+
10+
11+
a). CREATE TABLE Orders (<br/>
12+
OrderID int NOT NULL PRIMARY KEY,<br/>
13+
OrderNumber int NOT NULL,<br/>
14+
PersonID int FOREIGN KEY REFERENCES Persons(PersonID) <br/>
15+
);<br/>
16+
17+
b). ALTER TABLE Orders
18+
ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
19+
20+
c) ALTER TABLE Orders
21+
ADD CONSTRAINT FK_PersonOrder
22+
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
23+
24+
<br/>
25+
26+
<b>3. Handle Exception in SQL</b><br/>
27+
<p>BEGIN TRY <br/>
28+
DECLARE @Mynum INT <br/>
29+
---- Divide by zero Error<br/>
30+
SET @Mynum = 10/0<br/>
31+
PRINT 'This will not execute'<br/>
32+
END TRY<br/>
33+
BEGIN CATCH<br/>
34+
SELECT @@Error, @@ROWCOUNT<br/>
35+
SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage;<br/>
36+
END CATCH;<br/>
37+
GO <p/><br/>
38+
39+
40+
<b>4. RaiseError()</b><br/>
41+
The RAISERROR statement allows you to generate your own error messages and return these messages back to the application using the same format as a system error or warning message generated by SQL Server Database Engine.<br/><b>Syntax : </b>RAISERROR ( { message_id | message_text | @local_variable }
42+
{ ,severity ,state }
43+
[ ,argument [ ,...n ] ] )
44+
[ WITH option [ ,...n ] ];
45+
46+
<br/>
47+
48+
<b>5. SQL Transactions</b> <br/>
49+
<pre class="notranslate">
50+
<code>
51+
BEGIN TRANSACTION T1
52+
INSERT INTO Customer VALUES (10, 'Code_10', 'Ramesh')
53+
INSERT INTO Customer VALUES (11, 'Code_11', 'Suresh')
54+
BEGIN TRANSACTION T2
55+
INSERT INTO Customer VALUES (12, 'Code_12', 'Priyanka')
56+
INSERT INTO Customer VALUES (13, 'Code_13', 'Preety')
57+
PRINT @@TRANCOUNT --** Here TRANCOUNT value 2**
58+
COMMIT TRANSACTION T2 -- This does not physically commit
59+
PRINT @@TRANCOUNT -- **Here TRANCOUNT value 1**
60+
COMMIT TRANSACTION T1 -- This does a physically commit
61+
PRINT @@TRANCOUNT --** Here TRANCOUNT value 0 **
62+
</code></pre>
63+
64+
65+
66+

0 commit comments

Comments
 (0)