Trying to insert data correctly - sql

After receiving very good correction from fuzzy lollipop, I amended my code to create an insert statement for every row of data. This is the code that I entered:
INSERT DEPARTMENTS
(Department_Id,Department_Name,Manager_Id,Location_Id)
VALUES
('D0001','Think Tank',NULL,'L0001')
GO
INSERT DEPARTMENTS
(Department_Id,Department_Name,Manager_Id,Location_Id)
VALUES
('D0002','Creators',NULL,'L0002')
GO
INSERT DEPARTMENTS
(Department_Id,Department_Name,Manager_Id,Location_Id)
VALUES
('D0003','Marketers',NULL,'L0003')
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0001','Joe','Blow',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0002','John','Doe',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0003','Sue','Happy',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0004','Tina','Turner',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0005','Ike','Turner',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0006','Big','Bird',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0007','Speedy','Gonzales',NULL,NULL,2010/06/25,NULL,NULL)
GO
However, these were the error messages that I received:
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'.
The statement has been terminated.
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
I won’t make the same mistake by failing to respond immediately to solutions. I did not know the checking the green checkmark meant that the answer was satisfactory.
Thank you for any help

You have two different types of errors.
The first is that you are violating a foreign key constraint. There are three ways to solve this:
Find out what the correct key should be (for example by querying the LOCATIONS table) and change your foreign key to the correct value.
Insert the missing row in the LOCATIONS table before inserting into DEPARTMENTS.
Remove the constraint (this is probably a bad idea).
The second error is simpler - you have incorrectly formatted your date. It should be a string.
'2010-06-25'
The complete query:
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0001','Joe','Blow',NULL,NULL,'2010-06-25',NULL,NULL)

1) there are no records with the given LocationIDs in the Location table
2) you need to quote the date values

Is there a FOREIGN KEY constraint between Department and Location. If so, then inserting a deparment requires an existing Location. Location is linked to Department with Location_ID
Timestamps syntax should be as a string '12/12/2010' not without the '-signs

Related

Bulk insert null into table from csv file in SQL Server

I want to bulk insert from a csv file to a table.
This is my code for creating the table
create table customers
(
customerNumber int not null,
customerName varchar(50) not null,
salesRepNumber int null,
primary key(customerNumber)
);
This is the example of the csv file:
1001,John,1121
1002,James,1122
1003,Jonas,1123
1004,Jane,1124,
1005,Tom,1125,
1006,Bob,1126
1007,Thomas,NULL
This is the code for inserting data into the table:
BULK INSERT customers
FROM 'D:\Customers.csv'
WITH (fieldterminator=',', rowterminator='\n')
And I get these errors:
Msg 4864, Level 16, State 1, Line 4
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 7, column 12 (salesRepNumber).
Msg 4864, Level 16, State 1, Line 4
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 22, column 12 (salesRepNumber).
I am not so sure about how to bulk insert 'NULL' into a table and I am only supposed to use int datatype for the salesRep.

Bulk load data conversion error (EMP_TITLE)

I am trying to work with amusement park database here. I have created all the required tables. Now i need to Bulk insert records in all the tables. In the employee table i am trying to insert the records but keep getting this error
Msg 4863, Level 16, State 1, Line 77
Bulk load data conversion error (truncation) for row 24, column 3 (EMP_TITLE).
Msg 4863, Level 16, State 1, Line 77
Bulk load data conversion error (truncation) for row 45, column 3 (EMP_TITLE).
Msg 4863, Level 16, State 1, Line 77
Bulk load data conversion error (truncation) for row 648, column 3 (EMP_TITLE).
Msg 4863, Level 16, State 1, Line 77
I am unable to figure out what is the error here.
My EMPLOYEE table structure is this
CREATE TABLE EMPLOYEE (
EMP_NUM NUMERIC(4) PRIMARY KEY,
EMP_NAME VARCHAR(30) NOT NULL,
EMP_TITLE VARCHAR(35),
EMP_HIRE_DATE DATE,
EMP_DOB DATE NOT NULL,
EMP_PHONE VARCHAR(15) NOT NULL,
PARK_CODE VARCHAR(10),
CONSTRAINT FK_EMP_PARK FOREIGN KEY(PARK_CODE) REFERENCES THEMEPARK(PARK_CODE)
);
and i am using this command to bulk insert records into database
BULK INSERT EMPLOYEE
FROM 'C:\Users\TechProBox\Desktop\Amusement Park Data\Employee.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '0X0a',
TABLOCK
)
This is how my data looks like in csv file

Error String or binary data would be truncated

I've been trying to create tables and add data into but I've run into this error
Msg 8152, Level 16, State 14, Line 35
String or binary data would be truncated.
Code:
CREATE TABLE Speakers_photos
(
SpeakerID CHAR(10) NOT NULL,
Image VARBINARY(MAX) NOT NULL,
PRIMARY KEY(SpeakerID),
FOREIGN KEY (SpeakerID) REFERENCES Speakers(SpeakerID)
ON UPDATE CASCADE ON DELETE NO ACTION
)
INSERT INTO Speakers_photos VALUES('S001210001', 0)
Where did it go wrong?
The truncation error will not occur with the code provided. Either the script is incomplete or the error is in an INSERT trigger on the Speakers_photos table. In the case of a trigger, look at line 35 as indicated in the error message.

To Make Db column Case Sensitive

I have a database column of string type which is a primary key and case insensitive, I want to make the column as case sensitive such that it can have 2 entries like abc and ABC. I am trying like this
ALTER TABLE A
ALTER COLUMN B VARCHAR(10)
COLLATE SQL_Latin1_General_CP1_CS_AS
and I am getting following error
Msg 5074, Level 16, State 1, Line 1
The object 'PK_A' is dependent on column B'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN B failed because one or more objects access this column.

Msg 5074, Level 16, State 1, Line 1 The index 'IX_Alias_Alias' is dependent on column 'Alias'

Want to change the collation
using the below script
ALTER TABLE Alias ALTER COLUMN Alias nvarchar(25) COLLATE SQL_Latin1_General_CP1256_CI_AS
Error occurs
Msg 5074, Level 16, State 1, Line 1
The index 'IX_Alias_Alias' is dependent on column 'Alias'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN Alias failed because one or more objects access this column.
Actually I want to change the collation of all the table in database
You can use the below link to alter the collation of all the objects in a database.
Alter Collation for all the objects in a DB
You should delete the index first and then modify the column.