In: Computer Science
On SQL explain Views, Union All, Union, Truncate, Delete. And how we create Accounts in SQL Server?
Views:
Views provides encapsulation like it hides the original name of
table. In view one can use more than one tables from database and
add some sql functions, joins, where condition to get desire
output. Basically it has rows and columns just like real table.
Example:
CREATE VIEW view_name AS
SELECT [column1], [column2] FROM table_name WHERE condition
You can also update and drop views.
Union All:
This combines result set of two or more select statments with
duplication of rows. To use Union All you need to select same
number of columns with same datatype and columns are specified in
same order.
Example:
SELECT [Name],[Gender] FROM CITY1_TABLE WHERE [AGE] >18
UNION ALL
SELECT [Name],[Gender] FROM CITY2_TABLE WHERE [AGE] >18
Union:
This combines result set of two or more select statements with
distinct row. It never returns duplicate rows. To use Union you
need to select same number of columns with same datatype and
columns are specified in same order.
Example:
SELECT [Name][Age] FROM CITY1_TABLE where condition
UNION
SELECT [Name],[Age] FROM CITY2_TABLE where condition
Truncate:
Truncate command will delete data present in table but it does not
delete the table itself.
Example:
TRUNCATE TABLE table_name
Delete:
Delete command will delete existind records in table.
Example:
DELETE FROM table_name WHERE condition
Create Account in SQL Server:
To create Login:
Navigate to Security -> Logins -> New Login and enter Login
name, Password, Confirm Password and click OK to save.
or using query:
CREATE LOGIN login_name WITH PASSWORD = 'password';