Practical
Learning: Creating Relationships
|
|
- To complete our database with its tables, their primary keys, their
foreign keys, and some data in the tables, change the code in the query
window as follows:
-- =============================================
-- Database: KoloBank1
-- Author: FunctionX
-- Date Created: Monday 09 April 2007
-- =============================================
USE master
GO
-- Drop the database if it already exists
IF EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'KoloBank1'
)
DROP DATABASE KoloBank1
GO
CREATE DATABASE KoloBank1
ON PRIMARY
( NAME = DataRepository, FILENAME = 'C:\Microsoft SQL Server Database Development\KoloBank1.mdf')
LOG ON
( NAME = DataLog, FILENAME = 'C:\Microsoft SQL Server Database Development\KoloBank1.ldf');
GO
-- =========================================
-- Database: KoloBank1
-- Table: Locations
-- =========================================
USE KoloBank1
GO
IF OBJECT_ID(N'dbo.Locations', N'U') IS NOT NULL
DROP TABLE dbo.Locations
GO
CREATE TABLE Locations
(
LocationID int Identity(1,1) NOT NULL,
LocationCode nvarchar(10) NOT NULL,
Address nvarchar(120),
City nvarchar(50),
State nvarchar(50),
Notes nvarchar(max) NULL,
CONSTRAINT PK_Locations PRIMARY KEY (LocationID)
);
GO
-- =========================================
-- Database: KoloBank1
-- Table: AccountTypes
-- =========================================
USE KoloBank1
GO
IF OBJECT_ID(N'dbo.AccountTypes', N'U') IS NOT NULL
DROP TABLE dbo.AccountTypes
GO
CREATE TABLE AccountTypes
(
AccountTypeID int Identity(1,1) NOT NULL,
AccountType nvarchar(40) NOT NULL,
Notes nvarchar(max) NULL,
CONSTRAINT PK_AccountTypes PRIMARY KEY (AccountTypeID)
);
GO
-- =========================================
-- Database: KoloBank1
-- Table: Employees
-- =========================================
IF OBJECT_ID(N'dbo.Employees', N'U') IS NOT NULL
DROP TABLE dbo.Employees
GO
CREATE TABLE Employees
(
EmployeeID int identity(1,1) NOT NULL,
EmployeeNumber char(6),
FirstName nvarchar(32),
LastName nvarchar(32) NOT NULL,
Title nvarchar(50),
CanCreateNewAccount bit,
HourlySalary smallmoney,
EmailAddress nvarchar(100),
Username nvarchar(20),
Password nvarchar(20),
Notes nvarchar(max),
CONSTRAINT PK_Employees PRIMARY KEY (EmployeeID)
);
GO
-- =========================================
-- Database: KoloBank1
-- Table: Customers
-- =========================================
IF OBJECT_ID(N'dbo.Customers', N'U') IS NOT NULL
DROP TABLE dbo.Customers
GO
CREATE TABLE Customers
(
CustomerID int Identity(1,1) NOT NULL,
DateCreated date,
AccountNumber nvarchar(20),
AccountTypeID int Constraint FK_TypeOfAccount
References AccountTypes(AccountTypeID),
CustomerName nvarchar(50) NOT NULL,
DateUpdated date,
Notes nvarchar(max),
CONSTRAINT PK_Customers PRIMARY KEY (CustomerID)
);
GO
-- =========================================
-- Database: KoloBank1
-- Table: ChargeReasons
-- =========================================
IF OBJECT_ID(N'dbo.ChargeReasons', N'U') IS NOT NULL
DROP TABLE dbo.ChargeReasons
GO
-- =========================================
-- Database: KoloBank1
-- Table: Deposits
-- =========================================
IF OBJECT_ID(N'dbo.Deposits', N'U') IS NOT NULL
DROP TABLE dbo.Deposits
GO
CREATE TABLE Deposits
(
DepositID int identity(1, 1) NOT NULL,
LocationID int Constraint FK_DepositLocation
References Locations(LocationID) NOT NULL,
EmployeeID int Constraint FK_Clerk
References Employees(EmployeeID),
CustomerID int Constraint FK_Depositor
References Customers(CustomerID) NOT NULL,
DepositDate date NOT NULL,
DepositAmount money NOT NULL,
Notes nvarchar(max),
CONSTRAINT PK_Deposits PRIMARY KEY (DepositID)
);
GO
-- =========================================
-- Database: KoloBank1
-- Table: Withdrawals
-- =========================================
IF OBJECT_ID(N'dbo.Withdrawals', N'U') IS NOT NULL
DROP TABLE dbo.Withdrawals
GO
CREATE TABLE Withdrawals
(
WithdrawalID int identity(1, 1) NOT NULL,
LocationID int Constraint FK_WithdrawlLocation
References Locations(LocationID) NOT NULL,
EmployeeID int Constraint FK_ProcessedBy
References Employees(EmployeeID),
CustomerID int Constraint FK_CustomerAccount
References Customers(CustomerID) NOT NULL,
WithdrawalDate date NOT NULL,
WithdrawalAmount money NOT NULL,
WithdrawalSuccessful bit NOT NULL,
Notes nvarchar(max),
CONSTRAINT PK_Withdrawals PRIMARY KEY (WithdrawalID)
);
-- =========================================
-- Database: KoloBank1
-- Table: CheckCashing
-- =========================================
IF OBJECT_ID(N'dbo.CheckCashing', N'U') IS NOT NULL
DROP TABLE dbo.CheckCashing
GO
CREATE TABLE CheckCashing
(
CheckCashingID int identity(1, 1) NOT NULL,
LocationID int Constraint FK_BranchLocations
References Locations(LocationID) NOT NULL,
EmployeeID int Constraint FK_Employees
References Employees(EmployeeID),
CustomerID int Constraint FK_Customers
References Customers(CustomerID) NOT NULL,
CheckCashingDate date NOT NULL,
CheckCashingAmount money NOT NULL,
CheckCashingSuccessful bit NOT NULL,
Notes nvarchar(max),
CONSTRAINT PK_CheckCashing PRIMARY KEY(CheckCashingID)
);
GO
- Press F5 to execute the statement
A diagram is a window that visually displays the
relationships among tables of a database. To create a diagram:
- In the Object Explorer, in the database node, you can click Database
Diagrams
- A dialog box will inform you that this database doesn't have a
diagram. Read the message and click Yes
- Right-click Database Diagrams and click New Database Diagram
- In the Add Table dialog box, click each table and click the Add.
Alternatively, you can double-click a table to add it
- In the Add Table dialog box, you can click Close.
On the toolbar,
you can click the Zoom button and select a larger or smaller value.
To move a table, you can drag its title bar. Here is an example:
- To establish a relationship, you can click the gray box on the left
of any column from the parent table and drop it on any column in the
child table. A better way is to click gray box of the primary key column
from the parent table, drag that box then drop it on the foreign key
column of the child table. Here is an example:
- A Tables and Columns dialog box would come up. It would display the
column that was dragged and the column on which you dropped.
If you
had selected just any column, it would show but it may not be the one
you wanted to drag; that is, it may not be the actual column that is
supposed to manage the relationship. Regardless, under Primary Key
Table, you should select the parent table
- Under the parent table, select its primary column
- Under Foreign Table, select the foreign key column. Here is an
example:
- Once you are ready, click OK. A link would be created between the
tables
- In the same way, you can create other relationships.
When you
have finished, you can save and close the database
Practical
Learning: Creating a Diagram
|
|
- In the Object Explorer, in the KoloBank1 node, click Database
Diagrams
- A dialog box will inform you that this database doesn't have a
diagram:
Read the message and click Yes
- Right-click Database Diagrams and click New Database Diagram
- In the Add Table dialog box, click Customers and click the Add >
button
- Double-click CheckCashing to add it
- In the same way, add the AccountTypes , Deposits, Employees,
Locations, and Withdrawals tables
- On the Add Table dialog box, click Close.
Notice that, based on
how we created the database and its objects, the relationships have been
created already:
- To save the diagram, on the Standard toolbar, click Save
- Set its name to dgmKoloBank1 and click OK
- Close the window
Data relationships allow records from one object to be available
to other objects. When a relationship has been established between two
tables, one of the concerns is to plan what would happen if a record from a
parent table is deleted or moved.
Referential integrity is the ability to take appropriate
actions when tables or records involved in a relationship are affeected. To
assist you with this aspect of database management, Both Microsoft SQL
Server and Transact-SQL provide various tools.
To visually manage referential integrity, you can use the Foreign Key
Relationships dialog box. To access it, first open the child table in the design view.
Then:
- Right-click anywhere in the table and click Relationships...
- On the main menu, click Table Designer -> Relationships...
Any of these actions would display the Foreign Key Relationships dialog
box. As reviewed already, if you had not yet created a foreign key on the
table that was displaying, the dialog box would appear empty. We have
already seen how to create a foreign key using this dialog box.
Enforcing Referential Integrity
|
|
When a relationship has been established between two
tables, you can ask the database engine to observe some rules between the
tables on one hand and among the records on the other hand. Of course,
before setting these rules, you must have created the relationship. Here
is an example we saw already:
Once this is done, in the Foreign Key Relationships
dialog box, expand INSERT And UPDATE Specification. Two combo boxes would
appear: Delete Rule and Update Rule
These two combo boxes have the same four options:
The opttions can be set either visually or
programmatically. Remember how to create a foreign key with code. Here is an
example:
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int FK_Genders FOREIGN KEY REFERENCES Genders(GenderID)
);
GO
To specify what action to take on the foreign key when a
record is deleted, add an ON DELETE expression:
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int FOREIGN KEY REFERENCES Genders(GenderID)
ON DELETE . . .
);
GO
To specify what action to take on the foreign key when a
record has changed, add an ON UPDATE expression:
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int FOREIGN KEY REFERENCES Genders(GenderID)
ON UPDATE . . .
);
GO
In both cases, you must specify what option to apply.
An Error On Delete or On Update
|
|
The default option is No
Cation and it is the first one
selected in the Foreign Key Relationships dialog box. Here is an example of setting
it with code:
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int FOREIGN KEY REFERENCES Genders(GenderID)
ON DELETE NO ACTION
);
GO
You would follow the same approach for the update. The
No Action option asks the database engine to issue an error if the record in
the parent is deleted or updated while at least one record of the child
table uses that parent record. Consider the following tables:
CREATE TABLE Genders
(
GenderID int not null,
Gender nvarchar(20),
CONSTRAINT PK_Genders PRIMARY KEY(GenderID)
);
GO
INSERT INTO Genders
VALUES(1, N'Male'), (2, N'Female'), (3, N'Unknown');
GO
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int FOREIGN KEY REFERENCES Genders(GenderID)
ON DELETE NO ACTION
);
GO
INSERT INTO Persons(FirstName, LastName, GenderID)
VALUES(N'James', N'Palau', 1),
(N'Ann', N'Nsang', 2),
(N'Marc', N'Ulrich', 1),
(N'Arjuh', N'Namdy', 3),
(N'Aisha', N'Diabate', 2);
GO
Here is an example of showing all records of the table:
Now, if you try to delete one of the records of
the Genders table, you would receive an error. Here is an example:
In the same way, if you had set the update to No Action,
if you try updating a parent record and if the change would impact a child
record, the database engine would throw an error. Consider the following
code:
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int FOREIGN KEY REFERENCES Genders(GenderID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
GO
Here is an attempt to update a parent record:
Cascading On Delete or On Update
|
|
The Cascade option indicates that, if something happens
to a record in the parent table, the child records receive the change. For
example, if you are using the Delete Rule, if a record is deleted in the
parent table and if some records in the child table use the value in the
parent table, those records in the child table get deleted.
To visually apply the cascade option, if you are working
visually, in the Foreign Key Relationships dialog box, click the combo box
of either Delete Rule or Update Rule and select Cascade. To set it
programmatically, add CASCADE after ON DELETE or ON UPDATE. Here is
an example:
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int FOREIGN KEY REFERENCES Genders(GenderID)
ON DELETE CASCADE
);
GO
INSERT INTO Persons(FirstName, LastName, GenderID)
VALUES(N'James', N'Palau', 1),
(N'Ann', N'Nsang', 2),
(N'Marc', N'Ulrich', 1),
(N'Arjuh', N'Namdy', 3),
(N'Aisha', N'Diabate', 2);
GO
Here is an example of deleting a record from a parent
table:
Notice that the records in the child table that were
getting their values from the parent table have also been deleted.
If you apply the cascade option to the Update Rule, when a
record of the parent table is changed, the child records receive the change.
Setting NULL On Delete or On Update
|
|
Instead of displaying a nasty error or even deleting
records on cascade when something happens to a record of a parent table,
probably a better option is to reset to NULL every record of the child table
if that record is related to the parent table. To do this visually, in the
Delete Rule or the Update Rule, select Set Null. To do this
programmatically, after ON DELETE or ON UPDATE, add
SET NULL. Here is
an example:
DROP TABLE Persons;
GO
DROP TABLE Genders;
GO
CREATE TABLE Genders
(
GenderID int not null,
Gender nvarchar(20),
CONSTRAINT PK_Genders PRIMARY KEY(GenderID)
);
GO
INSERT INTO Genders
VALUES(1, N'Male'), (2, N'Female'), (3, N'Unknown');
GO
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int FOREIGN KEY REFERENCES Genders(GenderID)
ON DELETE SET NULL
);
GO
INSERT INTO Persons(FirstName, LastName, GenderID)
VALUES(N'James', N'Palau', 1),
(N'Ann', N'Nsang', 2),
(N'Marc', N'Ulrich', 1),
(N'Arjuh', N'Namdy', 3),
(N'Aisha', N'Diabate', 2);
GO
Here is an example of showing all records of the table:
Here is an example of deleting a record from a parent
table:
The update follows the same logic: If a record of the parent table
is updated, any record in the child table and that gets its value from the
parent table would have its value set to NULL.
Applying the Default Value On Delete or On Update
|
|
If a column of a parent table has a default value, when
a record of that column is affected by some action, you can ask the database
engine to apply the default value to the related records of the child table.
To do this programmatically, use ON DELETE SET DEFAULT or
ON UPDATE SET
DEFAULT. Here is an example:
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int default 3
CONSTRAINT FK_Genders FOREIGN KEY REFERENCES Genders(GenderID)
ON DELETE SET DEFAULT
);
GO
INSERT INTO Persons(FirstName, LastName, GenderID)
VALUES(N'James', N'Palau', 1),
(N'Ann', N'Nsang', 2),
(N'Marc', N'Ulrich', 1),
(N'Arjuh', N'Namdy', NULL),
(N'Aisha', N'Diabate', 2);
GO
Here is an example of showing the records of the table:
Here is an example of delete a record from the parent
table and showing the records of the child table:
Practical
Learning: Managing Referential Integrity
|
|
- In the Object Explorer, expand the Tables node under KoloBank1.
Right-click dbo.Customers and click Design
- Right-click in the table and click Relationships
- Under Selected Relationships, click FK_CustomerAccount. In the right
section, expand INSERT And UPDATE Specification
- Click Delete Rule. In its combo box, select Cascade
- Click Update Rule. In its combo box, select Cascade:
- In the same way, specify the following
Foreign Key |
Delete Rule |
Update Rule |
FK_ChargeReasons |
Cascade |
Cascade |
FK_Customers |
Cascade |
Cascade |
FK_Depositor |
Cascade |
Cascade |
FK_TypeOfAccount |
Cascade |
Cascade |
- Click Close
- Save and close the table
- In the same way, open the Deposits table in design view
- Access its Relationships dialog box
- Access the properties of its FK_Clerk field.
Specify its Delete
Rule and its Update Rule both to Cascade
- Perform the same actions for the other relationships
- Close the Relationships dialog box
- Save and close the table
There are many issues you need to be concerned about in a
relational database. Some issues are related to the columns of tables (and
views) and some issues have to do with records. This means that you should
always think about structures of tables when you decide to either add new
columns or delete records.
If you decide to delete a table, first check if it
involved in a relationship. If so, is it the parent (is it providing its
records to another table?) or the child (is one of its columns receiving
values from a parent table?)? If a table is a child, you can easily delete
it using any of the techniques we know
already. If a table is a parent, you will receive an error. Consider the
following two tables
CREATE TABLE Genders
(
GenderID int identity(1, 1) not null PRIMARY KEY,
Gender nvarchar(20)
);
GO
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int FOREIGN KEY REFERENCES Genders(GenderID)
);
GO
INSERT INTO Genders(Gender)
VALUES(N'Make'), (N'Female'), (N'Unknown');
GO
INSERT INTO Persons(FirstName, LastName, GenderID)
VALUES(N'Peter', N'Mukoko', 1), (N'Ann', N'Nsang', 2);
GO
If you try to delete the Genders table, which is a
parent to the Persons, table, you would receive an error:
To avoid this
problem (this error), you can first delete the child table.
After creating a table or when inheriting a table
created by someone else, you may find out that it lacks a primary key. You
can add it, of course following some rules. You have two options.
Imagine you have the following table:
CREATE TABLE Employees
(
FirstName nvarchar(20),
LastName nvarchar(20),
DepartmentCode nchar(6)
);
GO
You can add the PRIMARY KEY expresion after defining
the new column. Here is an example:
ALTER TABLE Employees
ADD EmployeeNumber int not null PRIMARY KEY;
GO
As an alternative, you can add a column, and then use
the CONSTRAINT formula to define the primary key. Here is an example:
ALTER TABLE Employees
ADD EmployeeNumber int not null
CONSTRAINT PK_Employees PRIMARY KEY(EmployeeNumber);
GO
Just as you add a primary key to an already created
table, you can also add a new column that is a foreign key. Consider the
following table named Persons:
CREATE TABLE Genders
(
GenderID int identity(1, 1) not null PRIMARY KEY,
Gender nvarchar(20)
);
GO
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL
);
GO
The formula to add a foreign key to an existing table is:
ALTER TABLE TableName
ADD NewColumnName DataType Options
FOREIGN KEY REFERENCES ParentTableName(ColumnNameOfOtherTable);
GO
Here is an example of adding a foreign key to the above Persons
table:
ALTER TABLE Persons
ADD GenderID int NULL FOREIGN KEY REFERENCES Genders(GenderID);
GO
When performing data entry, in some columns, even after
indicating the types of values you expect the user to provide for a certain
column, you may want to restrict a range of values that are allowed. In the
same way, you can create a rule that must be respected on a combination of
columns before the record can be created. For example, you can ask the
database engine to check that at least one of two columns received a value.
For example, on a table that holds information about customers, you can ask
the database engine to check that, for each record, either the phone number
or the email address of the customer is entered.
The ability to verify that one or more rules are
respected on a table is called a check constraint. A check constraint is a
Boolean operation performed by the SQL interpreter. The interpreter examines
a value that has just been provided for a column. If the value is
appropriate:
- The constraint produces TRUE
- The value gets accepted
- The value is assigned to the column
If the value is not appropriate:
- The constraint produces FALSE
- The value gets rejected
- The value is not assigned to the column
You create a check constraint at the time you are
creating a table.
Visually Creating a Check Constraint
|
|
To create a check constraint, when creating a table,
right-click anywhere in (even outside) the table and click Check
Constraints...
This would open the Check Constraints dialog box. From
that window, you can click Add. Because a constraint is an object, you must
provide a name for it. The most important piece of information that a check
constraint should hold is the mechanism it would use to check its values.
This is provided as an expression. Therefore, to create a constraint, you
can click Expression and click its ellipsis button. This would open the
Check Constraint Expression dialog box.
To create the expression, first type the name of the
column on which the constraint will apply, followed by parentheses. In the
parentheses, use the arithmetic and/or SQL operators we studied already.
Here is an example that will check that a new value specified for the
Student Number is greater than 1000:
After creating the expression, you can click OK. If the
expression is invalid, you would receive an error and given the opportunity
to correct it.
You can create as many check constraints as you judge
necessary for your table:
After creating the check constraints, you can click OK.
Programmatically
Creating a Check Constraint
|
|
To create a check constraint in SQL, first create the
column on which the constraint will apply. Before the closing parenthesis of
the table definition, use the following formula:
CONSTRAINT name CHECK (expression)
The CONSTRAINT and the CHECK
keywords are required. As an object, make sure you provide a name
for it. Inside the parentheses that follow the CHECK
operator, enter the expression that will be applied. Here is an example that
will make sure that the hourly salary specified for an employee is greater
than 12.50:
CREATE TABLE Employees
(
[Employee Number] nchar(7),
[Full Name] varchar(80),
[Hourly Salary] smallmoney,
CONSTRAINT CK_HourlySalary CHECK ([Hourly Salary] > 12.50)
);
It is important to understand that a check constraint it
neither an expression nor a function. A check constraint contains an
expression and may contain a function as part of its definition.
After creating the constraint(s) for a table, in the
Object Explorer of Microsoft SQL Server Management Studio, inside the
table's node, there is a node named Constraints and, if you expand it, you
would see the name of the constraint.
With the constraint(s) in place, during data entry, if
the user (or your code) provides an invalid value, an error would display.
Here is an example:
Instead of an expression that uses only the regular
operators, you can use a function to assist in the checking process. You can
create and use your own function or you can use one of the built-in
Transact-SQL functions.
Practical
Learning: Creating a Check Constraint
|
|
- In the Object Explorer, right-click the name of the server and click
Start PowerShell
- In the PowerShell window, type SQLCMD and press Enter
- To create a database and a new table that has a check mechanism,
type the following:
CREATE DATABASE GreensvilleElectricCompany;
GO
CREATE TABLE Customers
(
AccountNumber nchar(14) primary key not null,
CustomerName nvarchar(50) NOT NULL,
PhoneNumber nchar(16),
EmailAddress nvarchar(50),
CONSTRAINT CK_CustomerContact
CHECK ((PhoneNumber IS NOT NULL) OR (EmailAddress IS NOT NULL))
);
GO
- To add records to the new table, type the following:
INSERT INTO Customers
VALUES(N'188395805',N'Ann Zeke',N'301-128-3506',N'azeke@yahoo.jp'),
(N'806200422',N'Joan Simms',N'410-114-6820',N'jsimmson@emailct.com'),
(N'402628475',N'Peter Dorka',N'(202) 050-1629',N'pdorka@hotmail.com'),
(N'666204275',N'James Bengbiss',N'443-158-1833',N'jbango@emailct.com');
GO
- To try adding a new record to the table, type the following and
press Enter after each line:
1> INSERT INTO Customers
2> VALUES(N'402628475',N'James Darfield',N'202-188-8742',N'');
3> GO
- Notice that you receive an error:
Msg 2627, Level 14, State 1, Server CENTRAL, Line 1
Violation of PRIMARY KEY constraint 'PK__Customer__BE2ACD6E51BA1E3A'. Cannot ins
ert duplicate key in object 'dbo.Customers'.
The statement has been terminated.
1>
- To delete the database, type:
DROP DATABASE GreensvilleElectricCompany;
GO
- Type exit and press Enter
- To close the PowerShell window, type exit and press Enter
- Which one of the following is a
valid way to create a primary key?
CREATE TABLE Employees
(
PRIMARY KEY EmployeeID int identity(1,1),
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL
);
CREATE TABLE Employees
(
EmployeeID int identity(1,1) IS PRIMARY KEY,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL
);
CREATE TABLE Employees
(
EmployeeID int identity(1,1) PRIMARY KEY,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL
);
CREATE TABLE Employees
(
EmployeeID int identity(1,1) MAKE PRIMARY KEY,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL
);
CREATE TABLE Employees
(
EmployeeID int identity(1,1),
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
SET EmployeeID = PRIMARY KEY
);
- Imagine you have the following table:
CREATE TABLE Genders
(
GenderID int identity(1, 1) primary key,
Gender nvarchar(32)
);
GO
From the following examples, what code would create a foreign key?
CREATE TABLE Employees
(
EmployeeID int identity(1,1) primary key,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int SET AS FOREIGN KEY FOR GenderID FROM Genders()
);
CREATE TABLE Employees
(
EmployeeID int identity(1,1) primary key,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int FOREIGN KEY AS Genders(GenderID)
);
CREATE TABLE Employees
(
EmployeeID int identity(1,1) primary key,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int,
CONSTRAINT FK_Genders FOREIGN KEY REFERENCES Genders(GenderID)
);
CREATE TABLE Employees
(
EmployeeID int identity(1,1) primary key,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int NULL FOREIGN KEY REFERENCES Genders(GenderID)
);
CREATE TABLE Employees
(
EmployeeID int identity(1,1) primary key,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int CREATE AS FOREIGN KEY FROM Genders(GenderID)
);
- Which of the following codes would create a primary key?
CREATE TABLE Customers
(
CustomerID int identity(1,1),
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
CONSTRAINT PRIMARY KEY(CustomerID)
);
CREATE TABLE Customers
(
CustomerID int identity(1,1),
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
CONSTRAINT PK_Customers PRIMARY KEY(CustomerID)
);
CREATE TABLE Customers
(
CustomerID int identity(1,1) NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
CONSTRAINT SET CustomerID AS PRIMARY KEY
);
CREATE TABLE Customers
(
CustomerID int identity(1,1) NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
CREATE PRIMARY KEY CONSTRAINT FOR CustomerID
);
CREATE TABLE Customers
(
CustomerID int identity(1,1) NOT NULL,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
CONSTRAINT PRIMARY KEY CustomerID AS PK_Customers
);
- Imagine you have the following table:
CREATE TABLE Genders
(
GenderID int identity(1, 1) primary key,
Gender nvarchar(32)
);
GO
What is a valid way to create a foreign key?
CREATE TABLE Students
(
StudentID int identity(1,1) primary key,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int NULL CONSTRAINT FK_Students
FOREIGN KEY REFERENCES Genders(GenderID)
);
CREATE TABLE Students
(
StudentID int identity(1,1) primary key,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int SET FK_Students AS CONSTRAINT
FOREIGN KEY REFERENCES Genders(GenderID)
);
CREATE TABLE Students
(
StudentID int identity(1,1) primary key,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int SET AS FOREIGN KEY FOR Genders(GenderID)
);
CREATE TABLE Students
(
StudentID int identity(1,1) primary key,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int,
CREATE FOREIGN KEY SET FK_Students = GenderID FROM Genders
);
CREATE TABLE Students
(
StudentID int identity(1,1) primary key,
FirstName nvarchar(20),
LastName nvarchar(20) NOT NULL,
GenderID int CONSTRAINT FOREIGN KEY REFERENCES Genders(GenderID)
);
- Imagine you have table and you are creating another table that will be
related to the first. If a record of a parent table is deleted and if you want
to display an error, what option would you add to the foreign key of the child
table?
- ON DELETE CASCADE ERROR
- ON DELETE NO ACTION
- ON DELETE SET NULL
- ON DELETE SHOW ERROR
- ON DELETE SET REFERENCE ERROR
- If you are creating a child table that will use some records that get
their values from the parent table, if a record of a parent table is updated and if you want
to display an error, what option would you add to the foreign key of the child
table?
- ON UPDATE SHOW ERROR
- ON UPDATE CASCADE ERROR
- ON UPDATE NO ACTION
- ON UPDATE SET NULL
- ON UPDATE SET REFERENCE ERROR
- Imagine you are creating a child table that will get some values from another table.
When a record of a parent table is deleted and you want
the records of the child table to be deleted, what option would you add to the foreign key of the child
table?
- ON DELETE SET DELETE
- ON DELETE NO ACTION
- ON DELETE SET NULL
- ON DELETE CASCADE
- ON DELETE NULL
- You are creating a table whose records will get some of their values from a
parent table. If a record changes in the parent table and you want
to change the corresponding records in the child table, what option would you add to the foreign key of the child
table?
- ON UPDATE SET DELETE
- ON UPDATE CASCADE
- ON UPDATE NO ACTION
- ON UPDATE SET NULL
- ON UPDATE NULL
- When creating a table (the child), you want its records to get the values
of a certain column from another table, the parent. If a record of a parent table is deleted, you would
like the records that use its value in
the child table to receive a value of NULL. When creating the foreign key in
the child table, what option would you add?
- ON DELETE CASCADE NULL
- ON DELETE NO ACTION
- ON DELETE NULL IS TRUE
- ON DELETE SET REFERENCE NULL
- ON DELETE SET NULL
- While creating a table, you would like it to have a column that gets its
values from another table. When creating that column, what option can you add
to the foreign key so that when the records of the other table are updated, the
column would receive a NULL value?
- ON UPDATE SET NULL
- ON UPDATE CASCADE NULL
- ON UPDATE NO ACTION
- ON UPDATE NULL IS TRUE
- ON UPDATE SET REFERENCE NULL
- You create a table and add a column that has a default value. Then you
create another table that has a column that will receive values from the first
table. When creating the foreign key of that column, what option can you add
so that when the records of the first table are deleted, the column would
receive a default value?
- ON DELETE DEFAULT = NULL
- ON DELETE CASCADE DEFAULT
- ON DELETE SET DEFAULT
- ON DELETE NO ACTION
- ON DELETE DEFAULT IS TRUE
- You had created a table that has a column with a default value. Now you
are creating another table that has a column that must receive its values from the first
table. When creating the foreign key of that column, what option can you add
so that when the records of the first table are updated, the column would
receive a default value?
- ON UPDATE SET DEFAULT
- ON UPDATE DEFAULT = NULL
- ON UPDATE CASCADE DEFAULT
- ON UPDATE NO ACTION
- ON UPDATE DEFAULT IS TRUE
- Answers
- Wrong Answer
- Wrong Answer
- Right Answer
- Wrong Answer
- Wrong Answer
- Answers
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Right Answer
- Wrong Answer
- Answers
- Wrong Answer
- Right Answer
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Answers
- Right Answer
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Answers
- Wrong Answer
- Right Answer
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Answers
- Wrong Answer
- Wrong Answer
- Right Answer
- Wrong Answer
- Wrong Answer
- Answers
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Right Answer
- Wrong Answer
- Answers
- Wrong Answer
- Right Answer
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Answers
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Right Answer
- Answers
- Right Answer
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Answers
- Wrong Answer
- Wrong Answer
- Right Answer
- Wrong Answer
- Wrong Answer
- Answers
- Right Answer
- Wrong Answer
- Wrong Answer
- Wrong Answer
- Wrong Answer
|
|