update tblEmployee
set FullName = 'James Bond'
where id=1;
update tblEmployee
----> We specify the update keyword followed by the table name of the table to modify.set FullName = 'James Bond'
----> We specify the set keyword followed by the column name and its value to set.where id=1
----> The where clause is used to limit the amount of records modified. After all we don't want employees to be named James Bond. :)Create function ReturnArea(@length int,@width int)
Returns int
as
Begin
Return @length * @width
End
Go
ReturnArea
----> Name of the function specified by its creator @length int, @width int
) ----> A function can take input from the user to do processing using it. Inputs of a function are called parameters. @length
----> Name of the parameter using which the function would perform calculations. Returns int
----> the keyword specifies that the function would provide the user with a integer value Begin
----> Start doing calculations Return @length * @width
----> Calculation to do and value to return End
----> Stop doing calculations (stop using computer memory)
2.Execute the query.
Executing the scalar function.
1.Type:
select dbo.ReturnArea(2,6)
dbo.
----> accessing the functions stored in the database using the keyword dbo followed by . ReturnArea
----> Name of the function we want to access. (2,6)
----> length and width parameters of the function; please note the order in which parameters are supplied matters. Create function GetEmployees()
returns table
as
return
select * from tblEmployee
returns table
----> return one or many records
1. Type
Create proc spGetEmp
as
select * from tblEmployee
go
proc
----> procedurespGetEmp
----> procedure name (sp stands for stored procedure)select * from tblEmployee
----> procedure to dogo
----> optional2. Execute query.
1.Type:
Exec spGetEmp
----> Exec followed by the stored procedure name
1. Type:
Create Proc spGetEmployee
@Name varchar(50)
as
select *
from tblEmployee
where FullName = @Name
@Name varchar(50)
----> input parameter containing a set of most 50 characters and at least 1 character.exec spGetEmployee 'Jhon Cena'
'Jhon Cena'
----> Argument passed to the method's name parameter Select Case (FullName)
When 'James Bond' Then 'Ironman'
Else 'N/A'
End As 'Full_Name'
from tblEmployee
Case (FullName)
----> Column name those fields would be evaluated.'James Bond'
temporarily Then change it to 'Ironman'
Else 'N/A'
----> Else change the column's fields names to N/A. End As 'Full_Name'
----> Make column name more readable temporarily. Id int NOT NULL PRIMARY KEY
CREATE TABLE tblPersons
(
Id int NOT NULL PRIMARY KEY,
Name varchar(255),
Address varchar(255),
City varchar(255)
)
NOT NULL
----> A primary key column cannot contain NULL
values (blanks). The not null
keyword ensures that the table does not allow the insertion of null
values. PRIMARY KEY
----> The PRIMARY KEY constraint uniquely identifies each record in a database table. CREATE TABLE tblOrders
(
OrderId int NOT NULL PRIMARY KEY,
OrderNo int,
Person_Id int FOREIGN KEY REFERENCES tblPersons(Id)
)
Person_Id FOREIGN KEY REFERENCES tblPersons(Id)
----> The FOREIGN KEY
(FK) from table Orders references the Primary key
(PK) from the tblPerson table. FOREIGN KEY
constraint prevents invalid data from being inserted into the foreign key
column, since the value being inserted needs to be one of the values contained in the table's Primary Key
column it references. CREATE TABLE tblProduct
(
ID int IDENTITY(1,1) PRIMARY KEY,
Name varchar(255) NOT NULL,
Manufacturer varchar(255)
)
IDENTITY(1,1)
----> increment id by 1 every time, a new record is inserted.insert into tblProduct
(Name ,Manufacturer)
Values('Microsoft Windows 10','Microsoft')
Drop table
Drop table tblProduct
Drop table
keyword deletes the table those named is specified after the table keyword
Drop column
ALTER TABLE tblEmployee
Drop COLUMN age
Drop COLUMN age
----> column name of the column to deleteselect FullName as 'All_Names' from tblEmployee
union all
select Name from tblPersons
Union
----> Combine the fields having common data type from two or more tables togetherSELECT tblOrders.OrderId,tblOrders.Person_Id, tblPersons.Name
FROM tblOrders
JOIN tblPersons
ON tblOrders.Person_Id=tblPersons.Id
tblOrders.OrderId
----> the table names "tblOrders" and "tblPersons" are specified to differentiate between 2 similar column names from the 2 tables. JOIN tblPersons
----> tblPersons is the table to be joined with the table named tblOrders thus it is specified after the JOIN
keyword ON tblOrders.Person_Id=tblPersons.Id
----> The specification of the foreign key
and primary key
relationship between the 2 tables is required. JOIN
clause returns all rows from two or more tables where a join condition is met. Create view vGetEmp
as
Select * from tblEmployee
Create view vGetEmp
----> View name.Select * from tblEmployee
----> Saving a query for later use.CREATE TABLE tblCars
(
CarId int Primary key identity(1,1) NOT NULL,
Manucfacturer varchar(50) DEFAULT 'Unknown'
)
DEFAULT 'Unknown'
----> The value 'Unknown' automatically inserted when if no other value is specified for the Manucfacturer column on record insertion.Download link: T-SQL_Advance_Tutorial.docx |