How to Create Multiple Primary and Select Statement in Sql Server


In this Article, I am going to explain about what is primary key,what is an identity and how to Create Multiple Primary and Different Select Statement in SQL Server . I have post source which is given below.

I have explain here How to Create Multiple Primary Key in a table using script and Design mode .I have attached some Images for references but identity column we can use one filed only.



1.What is Primary Key?




1.It cannot allow Null values.
2.It cannot allow Duplicate values.
3.It cannot allow space


2. What is Indentity




1. We can use Identity(1,1) means our table primary key starting 1 and seeds increment value 1
we can modify them for ex : 101,1



How to Create 2 Primary Key in a table using Sql script

CREATE TABLE [dbo].[Table2](
[EmpId] [int] IDENTITY(1,1) NOT NULL,
[EmpName] [varchar](50) NULL,
[EmpNo] [varchar](30) NULL,
[DeptId] [int] NOT NULL,
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
(
[EmpId] ASC,
[DeptId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


Query to insert records to database:

Insert into Table2 values ('Asp.net',1001,1)
Insert into Table2 values ('Vb.net',1002,2)
Insert into Table2 values ('C#.net',1003,3)


Query to list of all the records in a particular database:

SELECT * from Table2
SELECT * from Table2 where 1=1



Query to list of all the records in a particular database With Depends in Department Id:

SELECT * from Table2 where DeptId=2
SELECT * from Table2 where DeptId>=1
SELECT * from Table2 where DeptId<>0



Query to list of all the records in a particular database With Depends in EmpId:

SELECT * from Table2 where EmpId in(1)
SELECT * from Table2 where EmpId Not in(1)


Query to list of all the records in a particular database With Depends in EmpName:

SELECT * from Table2 where EmpName='Asp.net'
SELECT * from Table2 where EmpName<>'Asp.net'


I have attached Some Images given below


Attachments

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: