Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Chapter 11 Answers:

1.
CREATE TABLE [dbo].[test3](
[name] [varchar](20) NULL,
[ssn] [char](9) NULL,
[dept_number] [int] NULL,
[acct_balance] [smallmoney] NULL
) ON [PRIMARY]
GO
INSERT test3(name,ssn,dept_number,acct_balance)
VALUES ('xx','yy',2,5),
('xx','yyy',200000000,5),
('xx','yyyy',2,1234567.89)
Gives
Msg 8115, Level 16, State 4, Line 1
Arithmetic overflow error converting numeric to data type smallmoney.
The statement has been terminated.

INSERT test3(name,ssn,dept_number,acct_balance)
VALUES ('xx','yy',2,5),
('xx','yyy',200000000,5),
('xx','yyyy',2,567.89)
GIVES
(3 row(s) affected)
2.

CREATE NONCLUSTERED INDEX [ssn] ON [dbo].[test3]


(
[ssn] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB
= OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
insert test3 (ssn)
values ('zz'),
(''),
('yyy')
(3 row(s) affected)

a.

Yes the ssn takes nulls, in order to correct this you would add a contraint of
not null

b. Yes the ssn allows duplicates, in order to prevent this you would add a constraint of
duplicates not allowed.
CREATE TABLE [dbo].[Test2](
[name] [varchar](20) NULL,
[ssn] [char](9) NOT NULL,
[dept_number] [int] NULL,
[acct_balance] [smallmoney] NULL
) ON [PRIMARY]

You now cannot insert a null

c. You cannot set a primary key on a table with nulls or duplicates Drop or recreate the table
then alter

ALTER TABLE Test4


ADD CONSTRAINT ssn_pk PRIMARY KEY (ssn)
There is no difference with the primary key added

d. You can put values in both the acct_balance and dept_number column however once
the constraint is added you cannot.

You might also like