Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Table problems in writing script

Author  Topic 

abnormal 59
Starting Member

2 Posts

Posted - 2011-08-18 : 00:48:58
Can not figure out error cold some one Help me here is what I have.

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Employee]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Employee]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Job_title]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Job_title]
GO

CREATE TABLE JOB_TITLE
(
eeo_1_classification VARCHAR (20) NOT NULL,
job_title_id VARCHAR (5) PRIMARY KEY,
job_title VARCHAR (50) NOT NULL,
j_description VARCHAR (100) NOT NULL,
exempt VARCHAR (4) NOT NULL
)
GO

CREATE TABLE employee
(
id_num INT IDENTITY (1, 1) PRIMARY KEY,
Last_name VARCHAR (15) NOT NULL,
First_name VARCHAR(15) NOT NULL,
Address VARCHAR (50) NOT NULL,
City VARCHAR (20) NOT NULL,
state VARCHAR (2) NOT NULL,
tele_area_code INT NOT NULL,
tele_number VARCHAR (10) NOT NULL,
eeo_1_classification VARCHAR (20) NOT NULL,
hire_date DATETIME NOT NULL,
salary DECIMAL (10,2),
gender CHAR(1) NOT NULL,
race CHAR(16) NOT NULL,
age INT NOT NULL,
job_title_id VARCHAR (5) CONSTRAINT FK_Employee_Job_title FOREIGN KEY REFERENCES Job_title (Job_title_id)
)
GO


INSERT INTO JOB_TITLE VALUES
('Office Clerical', '061', 'Accounting Clerk', 'maintains records','No');

INSERT INTO JOB_TITLE VALUES
('Officials Managers', '062', 'Asst Manager', 'supervises workers', 'Yes');

INSERT INTO JOB_TITLE VALUES
('Sales Worker', '067', 'Bagger', 'places customer items in bags', 'No');

INSERT INTO JOB_TITLE VALUES
('Sales Workers', '064', 'Cashier', 'itemize and total cust purchases', 'No');

INSERT INTO JOB_TITLE VALUES
('Technician','065','Computer Support Specialist', 'Updates software maintain hardware provides training technical assistance', 'Yes');

INSERT INTO JOB_TITLE VALUES
('Officials Managers', '066','Director of Finance Accounting', 'plans and directs the finance and accounting activities','Yes');

INSERT INTO JOB_TITLE VALUES
('Craft Workers', '067', 'Retail Asst. Bakery & Pastry','monitors workers','No');

INSERT INTO JOB_TITLE VALUES
('Operatives', '068', 'Retail Asst. Butchers and Seafood Specialist', 'monitors workers', 'No');

INSERT INTO JOB_TITLE VALUES
('Stocker', '069', 'Office clerical','Stores, prices and restocks merchandise displays in store', 'No');

INSERT INTO EMPLOYEE VALUES
(
'Edelman','Glenn','225 Proctor ST ','La_Jolla','CA','219','555-3199','Sales Workers',
'07-OCT-2003',21500.75,'M','Caucasian',64,'084');

INSERT INTO EMPLOYEE VALUES
(
'McMullen','Eric','932 Mulberry lane ','Alta Vista','CA','519','555-3333','Sales Workers',
'1-NOV-2002',13500.00,'M','Caucasian',20,'084');

INSERT INTO EMPLOYEE VALUES
('Slentz','Raj','123 Axis Dr.','North Olive','CA','619','555-5555','Officials & Managers',
'1-JUN-2000',48000.00,'M','Asian',34,'016');

INSERT INTO EMPLOYEE VALUES
('Broun','Erin','2555 Palistine','Encinitas','CA','960','555-7777','Sales Workers',
'12-MAR-2003',10530.00,'F','Caucasian',24,'053');

INSERT INTO EMPLOYEE VALUES
('Carpenter','Donald','812 lawerence', 'Encinitas','CA','619','525-4444','Office Clerical',
'1-NOV-2003', 15000.00,'M','African American',18,'071');

INSERT INTO EMPLOYEE VALUES
('Esquivez','David','983 N. Windsor','Encinitas','CA','718','555-8888','Operatives',
'25-JUL-2003',18500.00,'M','Hispanic',25,'038');

INSERT INTO EMPLOYEE VALUES
('Sharp','Nancy','103 Monte Rd','Ramona Beech','CA','858','658-0135','Sales Workers',
'12-JUL-2003',21000.00,'F','Caucasian',24,'053');

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-08-18 : 01:00:49
You should see the error message something like
quote:

Server: Msg 2627, Level 14, State 1, Line 21
Violation of PRIMARY KEY constraint 'PK__Job_titl__CF16631743A1090D'. Cannot insert duplicate key in object 'dbo.JOB_TITLE '.
The statement has been terminated.


It means you have attempt to insert a duplicate PK for JOB_TITLE


CREATE TABLE JOB_TITLE
(
eeo_1_classification VARCHAR (20) NOT NULL,
job_title_id VARCHAR (5) PRIMARY KEY,
job_title VARCHAR (50) NOT NULL,
j_description VARCHAR (100) NOT NULL,
exempt VARCHAR (4) NOT NULL
)
GO

INSERT INTO JOB_TITLE VALUES
('Sales Worker', '067', 'Bagger', 'places customer items in bags', 'No');

INSERT INTO JOB_TITLE VALUES
('Craft Workers', '067', 'Retail Asst. Bakery & Pastry','monitors workers','No');



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

abnormal 59
Starting Member

2 Posts

Posted - 2011-08-18 : 08:59:23
quote:
Originally posted by abnormal 59

Can not figure out error cold some one Help me here is what I have.

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Employee]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Employee]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Job_title]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Job_title]
GO

CREATE TABLE JOB_TITLE
(
eeo_1_classification VARCHAR (20) NOT NULL,
job_title_id VARCHAR (5) PRIMARY KEY,
job_title VARCHAR (50) NOT NULL,
j_description VARCHAR (100) NOT NULL,
exempt VARCHAR (4) NOT NULL
)
GO

CREATE TABLE employee
(
id_num INT IDENTITY (1, 1) PRIMARY KEY,
Last_name VARCHAR (15) NOT NULL,
First_name VARCHAR(15) NOT NULL,
Address VARCHAR (50) NOT NULL,
City VARCHAR (20) NOT NULL,
state VARCHAR (2) NOT NULL,
tele_area_code INT NOT NULL,
tele_number VARCHAR (10) NOT NULL,
eeo_1_classification VARCHAR (20) NOT NULL,
hire_date DATETIME NOT NULL,
salary DECIMAL (10,2),
gender CHAR(1) NOT NULL,
race CHAR(16) NOT NULL,
age INT NOT NULL,
job_title_id VARCHAR (5) CONSTRAINT FK_Employee_Job_title FOREIGN KEY REFERENCES Job_title (Job_title_id)
)
GO


INSERT INTO JOB_TITLE VALUES
('Office Clerical', '061', 'Accounting Clerk', 'maintains records','No');

INSERT INTO JOB_TITLE VALUES
('Officials Managers', '062', 'Asst Manager', 'supervises workers', 'Yes');

INSERT INTO JOB_TITLE VALUES
('Sales Worker', '070', 'Bagger', 'places customer items in bags', 'No');

INSERT INTO JOB_TITLE VALUES
('Sales Workers', '064', 'Cashier', 'itemize and total cust purchases', 'No');

INSERT INTO JOB_TITLE VALUES
('Technician','065','Computer Support Specialist', 'Updates software maintain hardware provides training technical assistance', 'Yes');

INSERT INTO JOB_TITLE VALUES
('Officials Managers', '066','Director of Finance Accounting', 'plans and directs the finance and accounting activities','Yes');

INSERT INTO JOB_TITLE VALUES
('Craft Workers', '067', 'Retail Asst. Bakery & Pastry','monitors workers','No');

INSERT INTO JOB_TITLE VALUES
('Operatives', '068', 'Retail Asst. Butchers and Seafood Specialist', 'monitors workers', 'No');

INSERT INTO JOB_TITLE VALUES
('Stocker', '069', 'Office clerical','Stores, prices and restocks merchandise displays in store', 'No');

INSERT INTO EMPLOYEE VALUES
(
'Edelman','Glenn','225 Proctor ST ','La_Jolla','CA','219','555-3199','Sales Workers',
'07-OCT-2003',21500.75,'M','Caucasian',64,'084');

INSERT INTO EMPLOYEE VALUES
(
'McMullen','Eric','932 Mulberry lane ','Alta Vista','CA','519','555-3333','Sales Workers',
'1-NOV-2002',13500.00,'M','Caucasian',20,'084');

INSERT INTO EMPLOYEE VALUES
('Slentz','Raj','123 Axis Dr.','North Olive','CA','619','555-5555','Officials & Managers',
'1-JUN-2000',48000.00,'M','Asian',34,'016');

INSERT INTO EMPLOYEE VALUES
('Broun','Erin','2555 Palistine','Encinitas','CA','960','555-7777','Sales Workers',
'12-MAR-2003',10530.00,'F','Caucasian',24,'053');

INSERT INTO EMPLOYEE VALUES
('Carpenter','Donald','812 lawerence', 'Encinitas','CA','619','525-4444','Office Clerical',
'1-NOV-2003', 15000.00,'M','African American',18,'071');

INSERT INTO EMPLOYEE VALUES
('Esquivez','David','983 N. Windsor','Encinitas','CA','718','555-8888','Operatives',
'25-JUL-2003',18500.00,'M','Hispanic',25,'038');

INSERT INTO EMPLOYEE VALUES
('Sharp','Nancy','103 Monte Rd','Ramona Beech','CA','858','658-0135','Sales Workers',
'12-JUL-2003',21000.00,'F','Caucasian',24,'053');



Norman Hunter

Made Changes and Now I get

Msg 547, Level 16, State 0, Line 28
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Employee_Job_title". The conflict occurred in database "HunterNData", table "dbo.JOB_TITLE", column 'job_title_id'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 33
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Employee_Job_title". The conflict occurred in database "HunterNData", table "dbo.JOB_TITLE", column 'job_title_id'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 38
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Employee_Job_title". The conflict occurred in database "HunterNData", table "dbo.JOB_TITLE", column 'job_title_id'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 42
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Employee_Job_title". The conflict occurred in database "HunterNData", table "dbo.JOB_TITLE", column 'job_title_id'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 46
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Employee_Job_title". The conflict occurred in database "HunterNData", table "dbo.JOB_TITLE", column 'job_title_id'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 50
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Employee_Job_title". The conflict occurred in database "HunterNData", table "dbo.JOB_TITLE", column 'job_title_id'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 54
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Employee_Job_title". The conflict occurred in database "HunterNData", table "dbo.JOB_TITLE", column 'job_title_id'.
The statement has been terminated.

Are my Table names conflicting with Insert names
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-08-18 : 09:16:35
1. learn to debug
- execute the query one by one and see which statement give you the error

2. learn to read the error message
quote:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Employee_Job_title". The conflict occurred in database "HunterNData", table "dbo.JOB_TITLE", column 'job_title_id'.

it means for the record you are trying to insert, the value in [job_title_id] violates the foreign key constraint FK_Employee_Job_title defined.
quote:
job_title_id VARCHAR (5) CONSTRAINT FK_Employee_Job_title FOREIGN KEY REFERENCES Job_title (Job_title_id)

In your table creation script you defined the constraint FK_Employee_Job_title references column Job_title_id in table Job_title. Which means the value for Job_title_id in table employee must exists in column Job_title_id in table Job_title


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -