Showing posts with label Primary Key. Show all posts
Showing posts with label Primary Key. Show all posts

Tables and other objects related to tables.

Good morning friends..In last article we saw how to create a table in database using T-SQL script as well as using management studio. Now lets talk few more objects related to tables. We will talk about few properties those can be set for columns in a table while creating the table. The properties we are going to talk about are as following:-

1)Primary Key
2)Foreign Key
3)Check Constraint
4)Default Constraint
5)Identity Column
6)Not Null Column
7)Unique Key Constraint
8)Computed Column
9)Collation 

There are lot of other properties available while creating a table but as a developer point of view, there are enough to know about. Lets take each property one by one.

Primary Key:- I hope everybody who's background is (IT, CS), would be knowing that primary key is a column or a group of column which uniquely identify each row of the table called primary key. we will learn how to create a primary key on a column or a group of column in the table, before we move ahead, let me one important aspect of primary key column, Primary key column doesn't contain any null value. It means that if we want to create a primary key on column it should not allow a null value to be inserted in that particular column.



In the above image you can see that I have expanded the tables folder in test database and right clicked on table employee that resulted into a menu with few options. Now I will select the design menu as we are going to design the table employee.



when we click on design menu the above window appears, Now we will select EID column and right click on it to make it as a primary key of employee table, another menu will appear with option "set primary key" as an option. You can see in the above image there are three columns "column name", "data type", "allow nulls" and the value of "allow nulls" is checked, it means that this column will accept null values in it. But as I said earlier, Primary key column cannot accept null values in it. Hence, as soon as you click on "set primary key" option, it SQL Server Management Studio automatically unchecked the value of "allow nulls" column.



You can see in the above image that "allow nulls" column is unchecked now. Lets see other properties of column as shown in the above image. I am going to expand the column properties window as shown in the above image so that you can have a clear look.


You need to select the column for which you want to set the properties, I have selected "Name" column and below is the property window for "Name" Column. Allow Null property is marked with "Yes", it means this column will allow null values, You can change it to "No" if you do not want it to accept null values.

Default Value or binding(Default Constraint) :- Default value property is blank, it means there is no default constraint defined on it and no default value assigned to "Name" Column in employee table. You can put a default value 'neeraj' or whatever value you want to see in the column to be entered by default.

Collation :- Collation property is showing value "<database default>" it means that this column will have same collation that database has.Collation is very important aspect in SQL Server. Two columns with different collation cannot be join together. I will talk about collation in detail after this article.

Is Identity :- Value for this column is "No" it means that "Name" Column will not have identity value. This value is only enabled for Int, bigint, tinyint, (Numeric, decimal with decimal factor 0). As "name" column is a varchar field, hence this option is not enabled.

What is Identity Column:- If we set a column as a identity column, we need not to insert any value to this column, it will be automatically filled while inserting a record to the table.

Identity Increment :- You you have set the column property Is identity =Yes, you can also set the incremental value for each record to be inserted.
Example :- Suppose I have set the identity increment value =5 and identity seed=10 then, if  I insert three records in the table. lets see what would be the outcome of the select statement on employee table.



You can see the result, first value for EID is 10, Identity Seed defines what would be the starting value of the identity column for first record. Identity Increment defines what would be increment value per record, hence 10 +5(increment value)=15 , then next value 20 and so on.


Note:- Increment value and Identity seed are not mandatory fields, if we do not define any values for these properties, it will start with value 1 and incremental value will also be 1.

I hope you will like this article, few properties are still left to talk about. we will talk about rest of the properties of the table in my next article. Till then take care and have a wonderful weekend.

Working with database objects and meta data stored in SQL Server

Good Morning friends.. In my article that I posted on 19th Jan 2014, I mentioned that we would talk about database objects and meta data stored in SQL Server in my next article. But I could not write that article about the database objects and in the mean time, I wrote few interesting articles those are quite popular topics in SQL Sever. So today I am going to continue from the last topic where we stopped(creation of user database).

As a database developer you will be working on following objects.

  • Table
  • Primary Key
  • Foreign Key
  • Unique Key
  • Default Constraint
  • Procedure
  • Function
  • Trigger
  • Views
  • Indexes
We will talk about all objects, mentioned above one by one.

Table :- Table is an object where information is stored in form of rows and columns in SQL Server.

Creating database table using T-SQL script.

Create table employee(Eid int,Name Varchar(100))




If we paste the highlighted script into the management studio window and click on "Execute" button(encircled with red color in the above image), a table with name "Employee" will be created in the database. If you see left side of the execute button you will see the database name selected as "Test". It means that the table will be created in the "Test" database. You can select any already created database from the list. This time I have chosen "Test".


Now lets See how to create a table using management studio.




For creating table using management studio, you need to expand the databases node that is right side in the above image and you find the test database that you have already created in past.Now you need to expand the "Test" node as well and in that node you will find tables node, just right click on the tables node. Now a menu will appear with option new table, you need to click on new table menu. As soon as you click on new table menu, a new type window will be opened in front of you.


Now lets have a look on the below snapshot.




In the above image, you can see three columns..

1)Column Name :- This column contain the names of the column that you have in your table.
2)Data type :- Data type of each column.
3)Allow Nulls :- You can specify if your column will contain null values or not.



I have created two columns in my table, now I want to save this table in SQL Server, To save this table into the database, we need to click on floppy icon, encircled by red color.




As soon as you click on save button(Floppy icon), a dialogue box will appear in front of you where we will enter the name of the table, I have put my table name as "Department", just click on "OK" button of the dialog box and the table will be created in the database.


I hope after this article you will be able to create a table using management studio as well as through T-SQL script. In the next article I will talk about few more things related to tables that will give you more clarity. We will also talk about data types and different properties available to set while creating the table. Till then take care and have a wonderful day.