SlideShare a Scribd company logo
1 of 17
7 SQL SERVER: CUSTOMIZINGTHE DATABASE DESIGN
Customizing a database The Power of a DBMS lies in the control that it offers the user over the data lying underneath. By customizing the structure of the data, a programmer can optimize the efficiency of the system in a specific implementation. A Basic customization feature is controlling the type of data that is stored in a database table. Some of the such basic customizations are: ,[object Object]
  Adding a foreign key
  Adding Constraints
  Unique Constraint
  Not Null Constraint
  Check Constraint,[object Object]
Primary Keys What is a Primary Key: A Primary key is a field/attribute in a table which is used to uniquely identify a record Eg: Consider a dream database Here, a particular record can be uniquely located using the DreamNumber and hence, it is taken as the Primary key. If there are more than fields, eligible of being the primary key, the decision of choosing one among them lies with the DB designer
Primary Key Adding a Primary Key: A Table must have only one primary key. The Primary key must be defined during the creation of the table.  Syntax: Create table <tableName> ( <fieldName1><field1Type>  primary key, ..); Example: Consider the creation of the following table. The field ā€˜Dream Numberā€™ is  to be designated as the primary key Create table dreamtable( dreamnumberintprimary key, dream varchar(10), dreamdate date, dreamtime time, dreamtypevarchar(10)) ;
Redefining primary key Now, let us take a step back and redefine the primary key that we have learnt already. The definition that we learnt was: A Primary key is a field which lets us to uniquely identify a record in a table. This definition may sound intuitive, but in real word cases, a single field may not be enough to identify a record in a table. In such cases, a collection (two or more) of fields are used as a primary key. Consider the following example of a hospitalā€™s patient table:
Redefining primary key In this case, it would be possible to locate a record uniquely only with information of more than one field values. Example: PatientID, Date of Admisssion and Time of Admission can be combined to form a primary key. The SQL Command to do this is via a constraint: create table <tableName> (<field names>,ā€¦, constraint <constraintName> primarykey(<filedName1>,<fieldName2>,..) ); Example: create table Patients(patientidvarchar(10), patientnamevarchar(20), dateofadmission date, timeofadmission time, issue varchar(20),constraint patientpkeysprimary key(patientid, dateofadmission, timeofadmission) );
Adding primary keys to existing tables The SQL Server allows primary keys to be added to an existing table.  Syntax: ALTER TABLE <tableName>     ADD CONSTRAINT <constraintName>      PRIMARY KEY (<FieldName>); Example: Consider the patient database. The command to define the primary key after creating the table is: Alter table Patients add constraint patientpkeysprimary key(patientid, dateofadmission, timeofadmission) );
Foreign Keys Foreign Key: A Foreign key is a field/attribute in one table which is used as a primary key in another table Eg: Consider a dream database Every foreign key value must be present as a primary key in the referenced table. Eg: A dream number ā€˜3ā€™ isnā€™t possible in ā€˜Luck Tableā€™ unless such a dream number exists in the ā€˜Dream Tableā€™ Dream Table Notice that foreign key entries can repeat (where-as primary key entries canā€™t!) Foreign Key Primary Key Luck Table Refer-ences
Foreign Keys In SQL Server, a foreign key is created as follows: Create table <Table2_Name> (<filedName1> foreign key references <Table1_Name> (Table1FieldName)); In the example that we just considered, the dreamNumberof the Luck Table is using the value of the primary Key dreamNumberfrom Dream Table. Hence, it is a foreign Key. The SQL statement to effect this is: Create table lucktable (dreamnumberintforeign key references dream(dreamnumber), luck varchar(15), predictor varchar(20)); NOTE: It is essential to create the table which is being referenced by the foreign key before creating the foreign key itself.
Adding foreign keys to existing tables The SQL Server allows foreign keys to be added to an existing table.  Syntax: ALTER TABLE <tableName>  ADD FOREIGN KEY (<fieldName>)       REFERENCES <referencedTable>(<referencedFieldName>); Example: Table B has an attribute B1.  B1 references to the primary key A1 of table A. Now, the command to add B1 as a foreign key of B is as follows: Table A Primary Key of A Table B Field A1 Foreign Key of B Field B1 << B1 references A1  Alter table B add foreign key(B1) references A(A1) ;
Not Null Constraint In feeding data into a database, it lies with the user to feed data for a field or assign a null value. But sometimes, the value of the field may be highly valuable for data processing. In such places, we may prevent a ā€˜nullā€™ from being assigned to a field by using the not null constraint.  Eg: Consider  a customer table. Here, the name of the customer is very important. So, it can be made to be not null. Note: CutomerID being a primary key, inherently, can never be null. Create table customer(customeridint primary key, name varchar(10) not null, address varchar(30)); An attempt to insert a null value into a not-null field, will be flagged as an error.
Unique Constraint In SQL Server, it is possible to designate a field to be unique, .i.e., the field will not accept duplicate values.  Primary keys are inherently unique in nature. Eg: Consider  a customer table. Here, the credit card number of the customers will be unique. Create table customer(customeridint primary key, name varchar(10) not null, creditcardnumbervarchar(20) unique); An attempt to insert a credit card number that already exists in the table will be flagged as an error.

More Related Content

What's hot

Bootcamp sql fundamentals crud_part3
Bootcamp   sql fundamentals   crud_part3Bootcamp   sql fundamentals   crud_part3
Bootcamp sql fundamentals crud_part3varunbhatt23
Ā 
SQL Quick Reference Card
SQL Quick Reference CardSQL Quick Reference Card
SQL Quick Reference CardTechcanvass
Ā 
Sql basics
Sql basicsSql basics
Sql basicsKumar
Ā 
Bootcamp sql fundamentals bootcamp_part1
Bootcamp   sql fundamentals  bootcamp_part1Bootcamp   sql fundamentals  bootcamp_part1
Bootcamp sql fundamentals bootcamp_part1varunbhatt23
Ā 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteAl-Mamun Sarkar
Ā 
How sqlite works
How sqlite worksHow sqlite works
How sqlite worksVikas Bansal
Ā 
Bootcamp sql fundamental
Bootcamp sql fundamentalBootcamp sql fundamental
Bootcamp sql fundamentalvarunbhatt23
Ā 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in databaseHemant Suthar
Ā 
Sql create table statement
Sql create table statementSql create table statement
Sql create table statementVivek Singh
Ā 
Creating a database
Creating a databaseCreating a database
Creating a databaseRahul Gupta
Ā 
MY SQL
MY SQLMY SQL
MY SQLsundar
Ā 

What's hot (16)

Oracle: DML
Oracle: DMLOracle: DML
Oracle: DML
Ā 
Bootcamp sql fundamentals crud_part3
Bootcamp   sql fundamentals   crud_part3Bootcamp   sql fundamentals   crud_part3
Bootcamp sql fundamentals crud_part3
Ā 
SQL Quick Reference Card
SQL Quick Reference CardSQL Quick Reference Card
SQL Quick Reference Card
Ā 
SQL
SQLSQL
SQL
Ā 
Learn plsql
Learn plsqlLearn plsql
Learn plsql
Ā 
Sql basics
Sql basicsSql basics
Sql basics
Ā 
Bootcamp sql fundamentals bootcamp_part1
Bootcamp   sql fundamentals  bootcamp_part1Bootcamp   sql fundamentals  bootcamp_part1
Bootcamp sql fundamentals bootcamp_part1
Ā 
Oracle: PLSQL Commands
Oracle: PLSQL CommandsOracle: PLSQL Commands
Oracle: PLSQL Commands
Ā 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
Ā 
How sqlite works
How sqlite worksHow sqlite works
How sqlite works
Ā 
Bootcamp sql fundamental
Bootcamp sql fundamentalBootcamp sql fundamental
Bootcamp sql fundamental
Ā 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in database
Ā 
Sql create table statement
Sql create table statementSql create table statement
Sql create table statement
Ā 
Creating a database
Creating a databaseCreating a database
Creating a database
Ā 
Chapter08
Chapter08Chapter08
Chapter08
Ā 
MY SQL
MY SQLMY SQL
MY SQL
Ā 

Similar to MS SQL SERVER: Customizing Your D Base Design

Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating databaseMukesh Tekwani
Ā 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developerAhsan Kabir
Ā 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
Ā 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sqlVARSHAKUMARI49
Ā 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of dataDimara Hakim
Ā 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL UsedTheVerse1
Ā 
Sql 2006
Sql 2006Sql 2006
Sql 2006Cathie101
Ā 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...SakkaravarthiS1
Ā 
Sql commands
Sql commandsSql commands
Sql commandsPooja Dixit
Ā 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database ModelsPrithwis Mukerjee
Ā 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database ModelsPrithwis Mukerjee
Ā 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
Ā 
Unit 2 Chap 4 SQL DDL.pptx
Unit 2 Chap 4 SQL DDL.pptxUnit 2 Chap 4 SQL DDL.pptx
Unit 2 Chap 4 SQL DDL.pptxPetroJoe
Ā 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)Ehtisham Ali
Ā 

Similar to MS SQL SERVER: Customizing Your D Base Design (20)

Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
Ā 
Integrity and security
Integrity and securityIntegrity and security
Integrity and security
Ā 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developer
Ā 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Ā 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
Ā 
Sql wksht-2
Sql wksht-2Sql wksht-2
Sql wksht-2
Ā 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
Ā 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
Ā 
Sql 2006
Sql 2006Sql 2006
Sql 2006
Ā 
Ankit
AnkitAnkit
Ankit
Ā 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Ā 
Sql commands
Sql commandsSql commands
Sql commands
Ā 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
Ā 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
Ā 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Ā 
Unit 2 Chap 4 SQL DDL.pptx
Unit 2 Chap 4 SQL DDL.pptxUnit 2 Chap 4 SQL DDL.pptx
Unit 2 Chap 4 SQL DDL.pptx
Ā 
Assignment#07
Assignment#07Assignment#07
Assignment#07
Ā 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
Ā 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
Ā 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
Ā 

More from sqlserver content

MS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining toolsMS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining toolssqlserver content
Ā 
MS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data miningMS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data miningsqlserver content
Ā 
MS SQL SERVER: Programming sql server data mining
MS SQL SERVER:  Programming sql server data miningMS SQL SERVER:  Programming sql server data mining
MS SQL SERVER: Programming sql server data miningsqlserver content
Ā 
MS SQL SERVER: Olap cubes and data mining
MS SQL SERVER:  Olap cubes and data miningMS SQL SERVER:  Olap cubes and data mining
MS SQL SERVER: Olap cubes and data miningsqlserver content
Ā 
MS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithmMS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithmsqlserver content
Ā 
MS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rulesMS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rulessqlserver content
Ā 
MS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regressionMS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regressionsqlserver content
Ā 
MS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithmMS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithmsqlserver content
Ā 
MS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithmMS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithmsqlserver content
Ā 
MS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmxMS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmxsqlserver content
Ā 
MS Sql Server: Reporting models
MS Sql Server: Reporting modelsMS Sql Server: Reporting models
MS Sql Server: Reporting modelssqlserver content
Ā 
MS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating dataMS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating datasqlserver content
Ā 
MS Sql Server: Reporting introduction
MS Sql Server: Reporting introductionMS Sql Server: Reporting introduction
MS Sql Server: Reporting introductionsqlserver content
Ā 
MS Sql Server: Reporting basics
MS Sql  Server: Reporting basicsMS Sql  Server: Reporting basics
MS Sql Server: Reporting basicssqlserver content
Ā 
MS Sql Server: Datamining Introduction
MS Sql Server: Datamining IntroductionMS Sql Server: Datamining Introduction
MS Sql Server: Datamining Introductionsqlserver content
Ā 
MS Sql Server: Business Intelligence
MS Sql Server: Business IntelligenceMS Sql Server: Business Intelligence
MS Sql Server: Business Intelligencesqlserver content
Ā 
MS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into DatabaseMS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into Databasesqlserver content
Ā 
MS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With FunctionsMS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With Functionssqlserver content
Ā 
MS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A DatabaseMS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A Databasesqlserver content
Ā 
MS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base DesignMS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base Designsqlserver content
Ā 

More from sqlserver content (20)

MS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining toolsMS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining tools
Ā 
MS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data miningMS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data mining
Ā 
MS SQL SERVER: Programming sql server data mining
MS SQL SERVER:  Programming sql server data miningMS SQL SERVER:  Programming sql server data mining
MS SQL SERVER: Programming sql server data mining
Ā 
MS SQL SERVER: Olap cubes and data mining
MS SQL SERVER:  Olap cubes and data miningMS SQL SERVER:  Olap cubes and data mining
MS SQL SERVER: Olap cubes and data mining
Ā 
MS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithmMS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithm
Ā 
MS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rulesMS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rules
Ā 
MS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regressionMS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regression
Ā 
MS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithmMS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithm
Ā 
MS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithmMS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithm
Ā 
MS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmxMS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmx
Ā 
MS Sql Server: Reporting models
MS Sql Server: Reporting modelsMS Sql Server: Reporting models
MS Sql Server: Reporting models
Ā 
MS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating dataMS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating data
Ā 
MS Sql Server: Reporting introduction
MS Sql Server: Reporting introductionMS Sql Server: Reporting introduction
MS Sql Server: Reporting introduction
Ā 
MS Sql Server: Reporting basics
MS Sql  Server: Reporting basicsMS Sql  Server: Reporting basics
MS Sql Server: Reporting basics
Ā 
MS Sql Server: Datamining Introduction
MS Sql Server: Datamining IntroductionMS Sql Server: Datamining Introduction
MS Sql Server: Datamining Introduction
Ā 
MS Sql Server: Business Intelligence
MS Sql Server: Business IntelligenceMS Sql Server: Business Intelligence
MS Sql Server: Business Intelligence
Ā 
MS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into DatabaseMS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into Database
Ā 
MS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With FunctionsMS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With Functions
Ā 
MS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A DatabaseMS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A Database
Ā 
MS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base DesignMS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base Design
Ā 

Recently uploaded

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
Ā 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
Ā 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
Ā 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
Ā 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
Ā 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
Ā 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
Ā 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
Ā 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
Ā 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
Ā 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
Ā 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
Ā 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
Ā 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
Ā 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜RTylerCroy
Ā 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
Ā 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
Ā 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
Ā 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
Ā 

Recently uploaded (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
Ā 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
Ā 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
Ā 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Ā 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Ā 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
Ā 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Ā 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
Ā 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
Ā 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
Ā 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
Ā 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Ā 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
Ā 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
Ā 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜
Ā 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
Ā 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
Ā 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
Ā 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
Ā 

MS SQL SERVER: Customizing Your D Base Design

  • 1. 7 SQL SERVER: CUSTOMIZINGTHE DATABASE DESIGN
  • 2.
  • 3. Adding a foreign key
  • 4. Adding Constraints
  • 5. Unique Constraint
  • 6. Not Null Constraint
  • 7.
  • 8. Primary Keys What is a Primary Key: A Primary key is a field/attribute in a table which is used to uniquely identify a record Eg: Consider a dream database Here, a particular record can be uniquely located using the DreamNumber and hence, it is taken as the Primary key. If there are more than fields, eligible of being the primary key, the decision of choosing one among them lies with the DB designer
  • 9. Primary Key Adding a Primary Key: A Table must have only one primary key. The Primary key must be defined during the creation of the table. Syntax: Create table <tableName> ( <fieldName1><field1Type> primary key, ..); Example: Consider the creation of the following table. The field ā€˜Dream Numberā€™ is to be designated as the primary key Create table dreamtable( dreamnumberintprimary key, dream varchar(10), dreamdate date, dreamtime time, dreamtypevarchar(10)) ;
  • 10. Redefining primary key Now, let us take a step back and redefine the primary key that we have learnt already. The definition that we learnt was: A Primary key is a field which lets us to uniquely identify a record in a table. This definition may sound intuitive, but in real word cases, a single field may not be enough to identify a record in a table. In such cases, a collection (two or more) of fields are used as a primary key. Consider the following example of a hospitalā€™s patient table:
  • 11. Redefining primary key In this case, it would be possible to locate a record uniquely only with information of more than one field values. Example: PatientID, Date of Admisssion and Time of Admission can be combined to form a primary key. The SQL Command to do this is via a constraint: create table <tableName> (<field names>,ā€¦, constraint <constraintName> primarykey(<filedName1>,<fieldName2>,..) ); Example: create table Patients(patientidvarchar(10), patientnamevarchar(20), dateofadmission date, timeofadmission time, issue varchar(20),constraint patientpkeysprimary key(patientid, dateofadmission, timeofadmission) );
  • 12. Adding primary keys to existing tables The SQL Server allows primary keys to be added to an existing table. Syntax: ALTER TABLE <tableName> ADD CONSTRAINT <constraintName> PRIMARY KEY (<FieldName>); Example: Consider the patient database. The command to define the primary key after creating the table is: Alter table Patients add constraint patientpkeysprimary key(patientid, dateofadmission, timeofadmission) );
  • 13. Foreign Keys Foreign Key: A Foreign key is a field/attribute in one table which is used as a primary key in another table Eg: Consider a dream database Every foreign key value must be present as a primary key in the referenced table. Eg: A dream number ā€˜3ā€™ isnā€™t possible in ā€˜Luck Tableā€™ unless such a dream number exists in the ā€˜Dream Tableā€™ Dream Table Notice that foreign key entries can repeat (where-as primary key entries canā€™t!) Foreign Key Primary Key Luck Table Refer-ences
  • 14. Foreign Keys In SQL Server, a foreign key is created as follows: Create table <Table2_Name> (<filedName1> foreign key references <Table1_Name> (Table1FieldName)); In the example that we just considered, the dreamNumberof the Luck Table is using the value of the primary Key dreamNumberfrom Dream Table. Hence, it is a foreign Key. The SQL statement to effect this is: Create table lucktable (dreamnumberintforeign key references dream(dreamnumber), luck varchar(15), predictor varchar(20)); NOTE: It is essential to create the table which is being referenced by the foreign key before creating the foreign key itself.
  • 15. Adding foreign keys to existing tables The SQL Server allows foreign keys to be added to an existing table. Syntax: ALTER TABLE <tableName> ADD FOREIGN KEY (<fieldName>) REFERENCES <referencedTable>(<referencedFieldName>); Example: Table B has an attribute B1. B1 references to the primary key A1 of table A. Now, the command to add B1 as a foreign key of B is as follows: Table A Primary Key of A Table B Field A1 Foreign Key of B Field B1 << B1 references A1 Alter table B add foreign key(B1) references A(A1) ;
  • 16. Not Null Constraint In feeding data into a database, it lies with the user to feed data for a field or assign a null value. But sometimes, the value of the field may be highly valuable for data processing. In such places, we may prevent a ā€˜nullā€™ from being assigned to a field by using the not null constraint. Eg: Consider a customer table. Here, the name of the customer is very important. So, it can be made to be not null. Note: CutomerID being a primary key, inherently, can never be null. Create table customer(customeridint primary key, name varchar(10) not null, address varchar(30)); An attempt to insert a null value into a not-null field, will be flagged as an error.
  • 17. Unique Constraint In SQL Server, it is possible to designate a field to be unique, .i.e., the field will not accept duplicate values. Primary keys are inherently unique in nature. Eg: Consider a customer table. Here, the credit card number of the customers will be unique. Create table customer(customeridint primary key, name varchar(10) not null, creditcardnumbervarchar(20) unique); An attempt to insert a credit card number that already exists in the table will be flagged as an error.
  • 18. Check Constraint A Check constraint is used to specify some rules for the data that can be stored for a field. A Check constraint can be defined while creating a table (with create table) or can be added to an existing table. While adding a check constraint to an existing table, the data that the table holds is checked against the constraint being defined. To prevent this checking, the nocheck command can be used. A Check constraint can be defined on a single column(field) or across multiple columns of the same table. fieldname can take values between 1 and 100. eg: 1,2,3,ā€¦,99,100 SQL Syntax: Create table <tableName> (<fieldname> check (fieldname > 100) ); Create table <tableName> (<fieldname> check (fieldname between 1 and 100) ); Create table <tableName> (<fieldname> check (fieldname like ā€˜[0-9][0-9][a-z][A-Z]ā€™) ); [0-9]: Denotes a single value in the range specified
  • 19. Adding Check Constraint to an existing table It is possible to add check constraints to existing tables using the alter table command: Syntax: ALTER TABLE <tableName> ADD CHECK (<fieldName> <condition>); Example: ALTER TABLE student ADD CHECK (cgpa Between 0 and 10 );
  • 20.
  • 23. Adding primary key while creating table
  • 24. Adding primary key to existing tables
  • 25. Multiple field ā€“ primary key
  • 28. Adding foreign key while creating table
  • 29. Adding foreign key to existing table
  • 32.