| T-SQL Tutorial for beginners |
|
This article is supposed to help the people who faced sql server the first time. If you are already experienced in writing simple t-sql queries you will probably find this article useless and boring. But if you have only a vague understanding about what sql actually is then this article is written exactly for you. T-SQL or Transact-SQL is an extension of ANSI SQL developed by Microsoft and used by its sql server versions (SQL Server 6.5 which was mostly inherited from Sybase, SQL Server 7, SQL Server 2000, SQL Server 2005 and finally SQL Server 2008). Extension of ANSI SQL means that t-sql supports all the syntax and structures of ANSI SQL and extends them in some areas. In every new version of sql server Microsoft extends and enhances t-sql functionality. This t-sql tutorial mostly describes ANSI SQL 92 compliant operators of t-sql thus it may be considered as ANSI SQL tutorial.Executing T-SQLTo execute t-sql statements you will need a sql server instance and sql server tools. Even if you have access to a production sql server instance I recommend that you install a sql server instance locally to have your own test/development environment. For example you may install sql server 2008 enterprise evaluation edition or install SQL Server 2008 Express with Tools that can be downloaded from here absolutely for free. After you installed sql server tools and sql server engine go to Start -> Programs -> Microsoft SQL Server 2005 -> SQL Server Management Studio or Start -> Programs -> Microsoft SQL Server 2008 -> SQL Server Management Studio depending on the version of sql server you installed. You will be prompted to enter Server name and Authentication parameters Enter the server name, slash and name of the instance you have just installed or only server name for default instance. If everything is OK you will see Object Explorer pane on the left side of your SQL Server Management Studio (SSMS). Now click New Query button.
After that you will see New query editor window, where you can actually write you t-sql scripts. Using SQL Server Management StudioYou see that after you clicked a New query button SQL Editor window showed up. If you write simple query for instance select 1 and click Execute you will see the results tab. This way you can execute any t-sql statement and see the results. T-SQL operatorsBasically ANSI SQL and particulary transact-sql consists of DDL and DML operators. DDL - data definition language and DML - data manipulation language. Main DML operators are insert, update, delete, select and main DDL operators are create, alter and drop. A complete valid set of t-sql statements is called t-sql script. T-SQL statement is an implementation of t-sql operator usage. For instance There is t-sql operator INSERT. You can make use of this operator in a t-sql statement - insert mytable (a,b) values (1,2). A set of t-sql statements is a t-sql script: insert mytable (a,b) values (1,2) insert mytable (a,b) values (2,3) T-SQL DDL operatorsAll the data of a database is stored in table structures. And all tables are stored within databases. DDL t-sql operator to create a database is CREATE DATABASE. Let's create our own database and use it in the future CREATE DATABASE Tutorial If you copy this statement to your sql editor in SSMS and press execute you will see the message in the Messages panel Command(s) completed successfully. Now we need to switch our connection context to new created database (our context database is still master - see it in left top corner). We can either choose the database from drop down list or use t-sql operator USE for this purpose. USE Tutorial Again copy and execute and see that context database changed. By the way SSMS provides a great ability to execute only selected part of the script that is entered to sql editor. This means if you have copied multiple statements to the t-sql editor and want to execute only one of them (or execute them one by one) you don't need to delete all unwanted statements instead you only need to select the necessary statement and press execute. DDL operator to create a table is CREATE TABLE and the example of its syntax is as following create table mytable ( Copy and execute. Thus we created a table. A bit of explanation: The gold rule when creating a table is table must have a PRIMARY KEY. The silver rule is that the table must have a clustered index. The identity property of a column allows auto incrementing and auto assigning values. INT , FLOAT, VARCHAR, DECIMAL are all t-sql datatypes, suppose they are self-descriptive. T-SQL DML operatorsAs it was said before - main DML t-sql operators are INSERT UPDATE DELETE and SELECT, let's use them insert mytable (intdata, chardata, number, value) values (1,'test',65.4, 567.45)
T-SQL system stored procedures and views To be continued and extended.
Comments (0) |