SQL Concepts-1

              In this blog we are going to understand the important and basic concepts of SQL...

Q. How to create a SQL server database connection and new database?

1. Firstly open SSMS and connect with server also ensure SQL Server(MSSQLSERVER) is in running mode -


2. Create a new database -

                                                          

3. Create a table -

// U need to refresh whenever u add a new table

         


Q. What is primary key and foreign key in SQL server?

Primary Key : 
  • Primary key is a column having unique record.
  • Primary key cannot have NULL values.
  • It is used to uniquely identify records in relational database.
  • Only one primary key is allowed in a table.
  • When we delete or update records, data integrity happens.
Foreign Key : 
  • Foreign key is a column in database which provides a link between two tables for accessing data.
  • It contains duplicate records.
  • More than one foreign key are allowed in table.
  • It contains NULL values.
  • It refers to the field in table which is primary key of another table.
  • A foreign key can be used to match column with primary key in parent table.

Q. How can i create primary key and foreign key relationship?
  • Example :
  1.  Set primary key to both Id column in parent(tblCustomer) as well as child(tblCustomer1) table :
 

      2. Right click on foreign key and then Relationships >> Add >> Table and Columns :

 

 
Q. How can i create one to many relationship?
  • The relationship is association between one or more tables that are created using join statements.
  • Primarily their are 3 types of relationships : One-to-One, One-to-Many, Many-to-Many.
  1. One-to-One : A row in a table associated with a row in another table is called as one-to-one relationship.
  2. One-to-Many :  A row in a table associated with any number of rows in another table is called as one-to-many relationship.
  3. Many-to-Many :  Multiple rows in a table associated with a multiple rows in another table is called as many-to-many relationship.
  • One-to-Many relationship is created same as primary-to-foreign key relationship.
  • Example : Here for Id=2 two addresses inserted.



Q. What is identity column?

  • Identity column is a column used to automatically generate key values based on provided starting point(seed) and increment it.
  • The value in an identity column is created by server.
  • User cannot generally insert value into identity column.
  • For identity column firstly we need to set identity specification as yes - 
                  Right click >> properties >> identity specification >> is identity >> yes
 


Thank You!!!

For more understanding watch below video :





Comments

Popular posts from this blog

Day 3 : JavaScript

ASP .NET Session Management