SlideShare a Scribd company logo
7SQL SERVER: CUSTOMIZINGTHE DATABASE DESIGN
Customizing a databaseThe 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:  Adding a primary key
  Adding a foreign key
  Adding Constraints
  Unique Constraint
  Not Null Constraint
  Check ConstraintManaging Data IntegrityData integrity controls of the SQL Server 2008, help to guarantee the accuracy, validity, and correctness of data stored. They are a set of rules which can be defined by the programmer, so as to decide if a given data is valid for the system or not.Now, let us see more about them.
Primary KeysWhat is a Primary Key:A Primary key is a field/attribute in a table which is used to uniquely identify a recordEg: Consider a dream databaseHere, 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 KeyAdding 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 keyCreate table dreamtable( dreamnumberintprimary key, dream varchar(10), dreamdate date, dreamtime time, dreamtypevarchar(10)) ;
Redefining primary keyNow, 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 keyIn 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 tablesThe 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 KeysForeign Key:A Foreign key is a field/attribute in one table which is used as a primary key in another tableEg: Consider a dream databaseEvery 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 TableNotice that foreign key entries can repeat (where-as primary key entries can’t!)Foreign KeyPrimary KeyLuck TableRefer-ences
Foreign KeysIn 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 tablesThe 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 APrimary Key of ATable BField A1Foreign Key of BField B1<< B1 references A1 Alter table B add foreign key(B1) references A(A1) ;
Not Null ConstraintIn 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 ConstraintIn 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.
Ad

More Related Content

What's hot (16)

Oracle: DML
Oracle: DMLOracle: DML
Oracle: DML
DataminingTools Inc
 
Bootcamp sql fundamentals crud_part3
Bootcamp   sql fundamentals   crud_part3Bootcamp   sql fundamentals   crud_part3
Bootcamp sql fundamentals crud_part3
varunbhatt23
 
SQL Quick Reference Card
SQL Quick Reference CardSQL Quick Reference Card
SQL Quick Reference Card
Techcanvass
 
SQL
SQLSQL
SQL
Jerin John
 
Learn plsql
Learn plsqlLearn plsql
Learn plsql
Iulian Avram
 
Sql basics
Sql basicsSql basics
Sql basics
Kumar
 
Bootcamp sql fundamentals bootcamp_part1
Bootcamp   sql fundamentals  bootcamp_part1Bootcamp   sql fundamentals  bootcamp_part1
Bootcamp sql fundamentals bootcamp_part1
varunbhatt23
 
Oracle: PLSQL Commands
Oracle: PLSQL CommandsOracle: PLSQL Commands
Oracle: PLSQL Commands
DataminingTools Inc
 
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
Al-Mamun Sarkar
 
How sqlite works
How sqlite worksHow sqlite works
How sqlite works
Vikas Bansal
 
Bootcamp sql fundamental
Bootcamp sql fundamentalBootcamp sql fundamental
Bootcamp sql fundamental
varunbhatt23
 
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
Hemant Suthar
 
Sql create table statement
Sql create table statementSql create table statement
Sql create table statement
Vivek Singh
 
Creating a database
Creating a databaseCreating a database
Creating a database
Rahul Gupta
 
Chapter08
Chapter08Chapter08
Chapter08
sasa_eldoby
 
MY SQL
MY SQLMY SQL
MY SQL
sundar
 
Bootcamp sql fundamentals crud_part3
Bootcamp   sql fundamentals   crud_part3Bootcamp   sql fundamentals   crud_part3
Bootcamp sql fundamentals crud_part3
varunbhatt23
 
SQL Quick Reference Card
SQL Quick Reference CardSQL Quick Reference Card
SQL Quick Reference Card
Techcanvass
 
Sql basics
Sql basicsSql basics
Sql basics
Kumar
 
Bootcamp sql fundamentals bootcamp_part1
Bootcamp   sql fundamentals  bootcamp_part1Bootcamp   sql fundamentals  bootcamp_part1
Bootcamp sql fundamentals bootcamp_part1
varunbhatt23
 
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
Al-Mamun Sarkar
 
Bootcamp sql fundamental
Bootcamp sql fundamentalBootcamp sql fundamental
Bootcamp sql fundamental
varunbhatt23
 
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
Hemant Suthar
 
Sql create table statement
Sql create table statementSql create table statement
Sql create table statement
Vivek Singh
 
Creating a database
Creating a databaseCreating a database
Creating a database
Rahul Gupta
 

Viewers also liked (20)

Data base design
Data base designData base design
Data base design
Shashwat Shriparv
 
From Prototype to Production: A Crash Course in Hardware
From Prototype to Production: A Crash Course in HardwareFrom Prototype to Production: A Crash Course in Hardware
From Prototype to Production: A Crash Course in Hardware
HoyaMaxa
 
Pitch Perfect: An Entrepreneur's Guide
Pitch Perfect: An Entrepreneur's GuidePitch Perfect: An Entrepreneur's Guide
Pitch Perfect: An Entrepreneur's Guide
HoyaMaxa
 
Numerical methods for 2 d heat transfer
Numerical methods for 2 d heat transferNumerical methods for 2 d heat transfer
Numerical methods for 2 d heat transfer
Arun Sarasan
 
R: Apply Functions
R: Apply FunctionsR: Apply Functions
R: Apply Functions
DataminingTools Inc
 
Eugene SRTS Program
Eugene SRTS ProgramEugene SRTS Program
Eugene SRTS Program
Eugene SRTS
 
Data Applied: Clustering
Data Applied: ClusteringData Applied: Clustering
Data Applied: Clustering
DataminingTools Inc
 
RapidMiner: Nested Subprocesses
RapidMiner:   Nested SubprocessesRapidMiner:   Nested Subprocesses
RapidMiner: Nested Subprocesses
DataminingTools Inc
 
Classification
ClassificationClassification
Classification
DataminingTools Inc
 
Anime
AnimeAnime
Anime
Yarex Mussa Gonzalez
 
Cinnamonhotel saigon 2013_01
Cinnamonhotel saigon 2013_01Cinnamonhotel saigon 2013_01
Cinnamonhotel saigon 2013_01
cinnamonhotel
 
Test
TestTest
Test
spencer shanks
 
Apresentação Red Advisers
Apresentação Red AdvisersApresentação Red Advisers
Apresentação Red Advisers
mezkita
 
Excel Datamining Addin Intermediate
Excel Datamining Addin IntermediateExcel Datamining Addin Intermediate
Excel Datamining Addin Intermediate
DataminingTools Inc
 
Presentazione oroblu
Presentazione orobluPresentazione oroblu
Presentazione oroblu
robyroby65
 
Classification Continued
Classification ContinuedClassification Continued
Classification Continued
DataminingTools Inc
 
Control Statements in Matlab
Control Statements in  MatlabControl Statements in  Matlab
Control Statements in Matlab
DataminingTools Inc
 
Jive Clearspace Best#2598 C8
Jive  Clearspace  Best#2598 C8Jive  Clearspace  Best#2598 C8
Jive Clearspace Best#2598 C8
mrshamilton1b
 
From Prototype to Production: A Crash Course in Hardware
From Prototype to Production: A Crash Course in HardwareFrom Prototype to Production: A Crash Course in Hardware
From Prototype to Production: A Crash Course in Hardware
HoyaMaxa
 
Pitch Perfect: An Entrepreneur's Guide
Pitch Perfect: An Entrepreneur's GuidePitch Perfect: An Entrepreneur's Guide
Pitch Perfect: An Entrepreneur's Guide
HoyaMaxa
 
Numerical methods for 2 d heat transfer
Numerical methods for 2 d heat transferNumerical methods for 2 d heat transfer
Numerical methods for 2 d heat transfer
Arun Sarasan
 
Eugene SRTS Program
Eugene SRTS ProgramEugene SRTS Program
Eugene SRTS Program
Eugene SRTS
 
Cinnamonhotel saigon 2013_01
Cinnamonhotel saigon 2013_01Cinnamonhotel saigon 2013_01
Cinnamonhotel saigon 2013_01
cinnamonhotel
 
Apresentação Red Advisers
Apresentação Red AdvisersApresentação Red Advisers
Apresentação Red Advisers
mezkita
 
Excel Datamining Addin Intermediate
Excel Datamining Addin IntermediateExcel Datamining Addin Intermediate
Excel Datamining Addin Intermediate
DataminingTools Inc
 
Presentazione oroblu
Presentazione orobluPresentazione oroblu
Presentazione oroblu
robyroby65
 
Jive Clearspace Best#2598 C8
Jive  Clearspace  Best#2598 C8Jive  Clearspace  Best#2598 C8
Jive Clearspace Best#2598 C8
mrshamilton1b
 
Ad

Similar to MS Sql Server: Customizing Your Data Base Design (20)

Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
Mukesh Tekwani
 
Integrity and security
Integrity and securityIntegrity and security
Integrity and security
Surendra Karki Chettri
 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developer
Ahsan Kabir
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Belle Wx
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
VARSHAKUMARI49
 
Sql wksht-2
Sql wksht-2Sql wksht-2
Sql wksht-2
Mukesh Tekwani
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
Dimara 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 Used
TheVerse1
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
Cathie101
 
Ankit
AnkitAnkit
Ankit
Ankit Dubey
 
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
 
Database Management Systems s-sql-ddl.pdf
Database Management Systems s-sql-ddl.pdfDatabase Management Systems s-sql-ddl.pdf
Database Management Systems s-sql-ddl.pdf
sankarchv
 
DBMS_sql-ddl_V1.pdf890 ddl dml dql TCL DCL
DBMS_sql-ddl_V1.pdf890 ddl dml dql TCL DCLDBMS_sql-ddl_V1.pdf890 ddl dml dql TCL DCL
DBMS_sql-ddl_V1.pdf890 ddl dml dql TCL DCL
sankarchv
 
Sql commands
Sql commandsSql commands
Sql commands
Pooja Dixit
 
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATIONSQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
deeptanshudas100
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
Prithwis Mukerjee
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
Prithwis Mukerjee
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 
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
PetroJoe
 
Assignment#07
Assignment#07Assignment#07
Assignment#07
Sunita Milind Dol
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
Mukesh Tekwani
 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developer
Ahsan Kabir
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Belle Wx
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
Dimara 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 Used
TheVerse1
 
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
 
Database Management Systems s-sql-ddl.pdf
Database Management Systems s-sql-ddl.pdfDatabase Management Systems s-sql-ddl.pdf
Database Management Systems s-sql-ddl.pdf
sankarchv
 
DBMS_sql-ddl_V1.pdf890 ddl dml dql TCL DCL
DBMS_sql-ddl_V1.pdf890 ddl dml dql TCL DCLDBMS_sql-ddl_V1.pdf890 ddl dml dql TCL DCL
DBMS_sql-ddl_V1.pdf890 ddl dml dql TCL DCL
sankarchv
 
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATIONSQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
deeptanshudas100
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
Prithwis Mukerjee
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
Prithwis Mukerjee
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 
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
PetroJoe
 
Ad

More from DataminingTools Inc (20)

Terminology Machine Learning
Terminology Machine LearningTerminology Machine Learning
Terminology Machine Learning
DataminingTools Inc
 
Techniques Machine Learning
Techniques Machine LearningTechniques Machine Learning
Techniques Machine Learning
DataminingTools Inc
 
Machine learning Introduction
Machine learning IntroductionMachine learning Introduction
Machine learning Introduction
DataminingTools Inc
 
Areas of machine leanring
Areas of machine leanringAreas of machine leanring
Areas of machine leanring
DataminingTools Inc
 
AI: Planning and AI
AI: Planning and AIAI: Planning and AI
AI: Planning and AI
DataminingTools Inc
 
AI: Logic in AI 2
AI: Logic in AI 2AI: Logic in AI 2
AI: Logic in AI 2
DataminingTools Inc
 
AI: Logic in AI
AI: Logic in AIAI: Logic in AI
AI: Logic in AI
DataminingTools Inc
 
AI: Learning in AI 2
AI: Learning in AI 2AI: Learning in AI 2
AI: Learning in AI 2
DataminingTools Inc
 
AI: Learning in AI
AI: Learning in AI AI: Learning in AI
AI: Learning in AI
DataminingTools Inc
 
AI: Introduction to artificial intelligence
AI: Introduction to artificial intelligenceAI: Introduction to artificial intelligence
AI: Introduction to artificial intelligence
DataminingTools Inc
 
AI: Belief Networks
AI: Belief NetworksAI: Belief Networks
AI: Belief Networks
DataminingTools Inc
 
AI: AI & Searching
AI: AI & SearchingAI: AI & Searching
AI: AI & Searching
DataminingTools Inc
 
AI: AI & Problem Solving
AI: AI & Problem SolvingAI: AI & Problem Solving
AI: AI & Problem Solving
DataminingTools Inc
 
Data Mining: Text and web mining
Data Mining: Text and web miningData Mining: Text and web mining
Data Mining: Text and web mining
DataminingTools Inc
 
Data Mining: Outlier analysis
Data Mining: Outlier analysisData Mining: Outlier analysis
Data Mining: Outlier analysis
DataminingTools Inc
 
Data Mining: Mining stream time series and sequence data
Data Mining: Mining stream time series and sequence dataData Mining: Mining stream time series and sequence data
Data Mining: Mining stream time series and sequence data
DataminingTools Inc
 
Data Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlationsData Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlations
DataminingTools Inc
 
Data Mining: Graph mining and social network analysis
Data Mining: Graph mining and social network analysisData Mining: Graph mining and social network analysis
Data Mining: Graph mining and social network analysis
DataminingTools Inc
 
Data warehouse and olap technology
Data warehouse and olap technologyData warehouse and olap technology
Data warehouse and olap technology
DataminingTools Inc
 
Data Mining: Data processing
Data Mining: Data processingData Mining: Data processing
Data Mining: Data processing
DataminingTools Inc
 
AI: Introduction to artificial intelligence
AI: Introduction to artificial intelligenceAI: Introduction to artificial intelligence
AI: Introduction to artificial intelligence
DataminingTools Inc
 
Data Mining: Text and web mining
Data Mining: Text and web miningData Mining: Text and web mining
Data Mining: Text and web mining
DataminingTools Inc
 
Data Mining: Mining stream time series and sequence data
Data Mining: Mining stream time series and sequence dataData Mining: Mining stream time series and sequence data
Data Mining: Mining stream time series and sequence data
DataminingTools Inc
 
Data Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlationsData Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlations
DataminingTools Inc
 
Data Mining: Graph mining and social network analysis
Data Mining: Graph mining and social network analysisData Mining: Graph mining and social network analysis
Data Mining: Graph mining and social network analysis
DataminingTools Inc
 
Data warehouse and olap technology
Data warehouse and olap technologyData warehouse and olap technology
Data warehouse and olap technology
DataminingTools Inc
 

Recently uploaded (20)

Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à GenèveUiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPathCommunity
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient CareAn Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
Cyntexa
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à GenèveUiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPathCommunity
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient CareAn Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
Cyntexa
 

MS Sql Server: Customizing Your Data Base Design

  • 2. Customizing a databaseThe 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: Adding a primary key
  • 3. Adding a foreign key
  • 4. Adding Constraints
  • 5. Unique Constraint
  • 6. Not Null Constraint
  • 7. Check ConstraintManaging Data IntegrityData integrity controls of the SQL Server 2008, help to guarantee the accuracy, validity, and correctness of data stored. They are a set of rules which can be defined by the programmer, so as to decide if a given data is valid for the system or not.Now, let us see more about them.
  • 8. Primary KeysWhat is a Primary Key:A Primary key is a field/attribute in a table which is used to uniquely identify a recordEg: Consider a dream databaseHere, 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 KeyAdding 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 keyCreate table dreamtable( dreamnumberintprimary key, dream varchar(10), dreamdate date, dreamtime time, dreamtypevarchar(10)) ;
  • 10. Redefining primary keyNow, 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 keyIn 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 tablesThe 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 KeysForeign Key:A Foreign key is a field/attribute in one table which is used as a primary key in another tableEg: Consider a dream databaseEvery 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 TableNotice that foreign key entries can repeat (where-as primary key entries can’t!)Foreign KeyPrimary KeyLuck TableRefer-ences
  • 14. Foreign KeysIn 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 tablesThe 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 APrimary Key of ATable BField A1Foreign Key of BField B1<< B1 references A1 Alter table B add foreign key(B1) references A(A1) ;
  • 16. Not Null ConstraintIn 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 ConstraintIn 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 ConstraintA 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,100SQL 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 tableIt 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. Summary7. Customizing your database:Managing Data integrity
  • 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. Check ConstraintVisit more self help tutorialsPick a tutorial of your choice and browse through it at your own pace.The tutorials section is free, self-guiding and will not involve any additional support.Visit us at www.dataminingtools.net
  翻译: