Microsoft SQL Server Language
Now we will learn some basic things about Microsoft SQL server language. So let’s start.
Microsoft SQL Server Language
Basically there are four (4) types of SQL language mentioned below –
- DDL(Data Definition Language) – CREATE, ALTER, DROP, TRUNCATE
- DML(Data Manipulation Language) – SELECT, INSERT, UPDATE, DELETE
- DCL(Data Control Language) – ALTER PASSWORD, GRANT, REVOKE, CREATE SYNONYM
- TCL(Transaction Control Language) – COMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION
- DQL(Data Query Language) – SELECT
- DAC(Data Administration Commands) – START AUDIT, STOP AUDIT
- DDL: DDL is an abbreviation of Data Definition Language. It is used to create and modify the structure of database objects in a database, such as the creation or the deletion of a table, Index, and Views.
Example: CREATE, ALTER, DROP and TRUNCATE statements
- CREATE statement: Create a statement that is used to create a new database or a table.
Note: Money data type takes up to 4 decimal point, Varchar takes variable length and Char takes Fixed length.
Creating a Duplicate table:
1) Right click on table—->Script table as—->Create To—->New Query Editor Windows Then Copy Table Script and Execute it
- ALTER statement: Alter statement is used to modify table structure.
1) Syntax to add column:
ALTER TABLE Customers ADD Age CHAR(2)
2) Syntax to alter/modify column data type:
ALTER TABLE <> ALTER COLUMN <> <>
3) Syntax to delete/drop column:
ALTER TABLE <> DROP COLUMN <>
- DROP statement: Drop statement is used to remove table from Database.
DROP TABLE <>
Renaming Table name or Column Name:
Syntax to rename column name:
EXEC sp_rename'<>.<>’, <>, ‘COLUMN’;
Syntax to rename table name:
EXEC sp_rename <, <>;
- TRUNCATE statement: Truncate statement is used to remove all records from a table permanently even resets the identity columns.
TRUNCATE TABLE <>
- DML: DML is an abbreviation of Data Manipulation Language. It is used to retrieve, store, modify and delete data in a database.
Example: SELECT, INSERT, UPDATE and DELETE statements
Note: SQL Server run auto commit for every DML statements but if we want to control this then we need to execute “Begin Tran” or we can change this behavior. By default SQL Server Management Studio is in a Auto commit mode, which means whenever a transaction is executed then that is committed by default. If we want to disable Auto commit mode in SSMSL, then follow below steps:
1. Connect to SQL Server using SSMS
2. From the Menu bar, select Tools –> Options
3. Select Query Execution –> SQL Server –> ANSI
4. Then check the check box SET IMPLICIT_TRANSACTIONS
5. Click on OK
Run the bellow query as needed.
- SELECT statement: Select statement is used to retrieve records from a table.
SELECT * FROM <table_name> WHERE [col1/col2/col3….] = values
- INSERT statement: Insert statement is used to insert new records in a table.
INSERT INTO <table_name> [Col1,col2,col3,….] VALUES (value1, value2,….)
WHERE [col1, col2,….]
in above is optional
SELECT [* or Col_List] INTO <> FROM <>
INSERT INTO <table_name> [col_List] SELECT [*/Col_List] FROM <>
INSERT using DEFAULT values keyword:
INSERT INTO<table_name> DEFAULT VALUES
- UPDATE statement: Update statement used to change existing records data in a table.
UPDATE <table_name> SET col1=expr1, col2=expr2,… [WHERE condition]
UPDATE <table_name> SET col1=(SUBQUERY) [WHERE condition]
- DELETE statement: Delete statement is used to remove records or rows from a table.
DELETE FROM <table_name> [WHERE condition]
As discussed earlier that in SQL Server run auto commit for every DML statements but if we want to control this then we need to execute “Begin Tran”
- DCL: DCL is an abbreviation of Data Control Language. It is used to create roles, permissions and referential integrity as well it is used to control access of a database to secure it.
Example: ALTER PASSWORD, GRANT, REVOKE and CREATE SYNONYM statements
- TCL: TCL is an abbreviation of Transaction Control Language. It is used to manage different transactions occurring within a database.
Example: COMMIT, ROLLBACK, SAVEPOINT and SET TRANSACTION statements
- DQL: DQL is an abbreviation of Data Query Language. It is used to retrieve data only from database table.
Example: SELECT statement
- DAC: DAC is an abbreviation of Data Administration Commands. It is used to allow the user to perform audits and perform analysis on operations within the database.
Example: START AUDIT and STOP AUDIT statements
Difference between DELETE and TRUNCATE table statements:
1. DELETE is a DML command but TRUNCATE is a DDL command.
2. We can use WHERE CLAUSE while using DELETE command but it cannot use in TRUNCATE command.
3. DELETE is slower in performance than TRUNCATE, because it maintains logs for every record.
4. We can rollback data in DELETE but can’t be rollback data in TRUNCATE because TRUNCATE removes the records permanently.
5. TRUNCATE resets the identity counter but DELETE doesn’t.
6. A trigger doesn’t fire in case of TRUNCATE but Triggers can be fire in case of DELETE command.
We will discuss upcoming topics in next part. So please take care of you.
Assuming this tutorial post has well acquainted aspiring to the complete requirement. If you want to download this tutorial in PDF format file, then please click here to download PDF.
Also requesting, if you have any ideas or suggestions for me or you need some important blogs/Videos or guidelines related to SQL Server, Excel, WordPress, SEO, Website technologies then please feel free to write me on below comment section or you can go through the Contact Page. I will definitely come back to you with your desired requirements.
Hope this tutorial post was helpful for you and you liked very well, I am happy to serve you better. If you liked really this tutorial post, please don’t forget to share on social media like – Facebook, Whatsapp, LinkedIn, Twitter, Instagram, etc.