How to insert primary key when import text file with ssis - ssis

I am a new guy in SSIS. Today, I have a problem. When I import data in text file to table by SSIS package, it show an error "Cannot insert the value NULL into column 'CustomerID', table 'Customer'; column does not allow nulls. INSERT fails". The column CustomerID is primary key in table. I want to generate primary key. Can anyone help me. Thank you so much.

Well the error is pretty obvious. No NULLs in the primary key. Usually the method used to generate a primary key is to declare CustomerID as an IDENTITY PRIMARY KEY column which will auto-populate the column with UNIQUE values as and when you INSERT a record.
For more information on IDENTITY columns - MSDN
Note that if you employ this, you MUST NOT insert any value for the column CustomerID from your text file.
Please note that in your question you have mentioned
I want to generate primary key.
Make sure any of your other data/tables do not currently use the column CustomerID. If so, then you need to retain the same CustomerIDs or UPDATE the referencing columns in order to make sure they hold the new CustomerIDs.

Related

Database requires duplicate values as primary key ( Which is not allowed )?

I have a database assignment where I must build the following database.
The issue is, I am given a table to create called dependent which has 5 columns. 1 of these columns is dependent_name. It can't be a Foreign Key because there is not table for the dependents, so I assume it is suppose to be a Primary Key.
When I attempted to add the data in the table I was given an error that says I cannot have duplicate values for primary key.
Of course this is because you cannot have duplicate primary keys ( Alice is a duplicate value in this situation ), but I am not sure how to get around this. I guess I could make another table called dependent_info and make the dependent_name and FK, but this is not stated in the instructions from the teacher.
Is there something I am missing here?

Inserting new data in mysql after creating new field

How can I insert new data in column after adding column without using update function. for example
"alter table Employee add column Gender varchar(1) after Birthdate then I get wrong when I used this statement insert into Employee(ENumber,EmpName,Birthdate,Address,Salary,DNumber,Gender)
-> values
-> ('E001','GSInocencio','1988-01-15','Munoz',18000,'D005','F'),
It gives me error Duplicate entry 'E001' for key 'PRIMARY'
MariaDB [Employees_Valdez]>
The messages is pretty clear: You already have an employee with that ENumber value.
You have a UNIQUE constraint on that column, it's a PRIMARY KEY, so either pick a different value, or use a different primary key.
One thing to note is MySQL doesn't use complex string primary keys very efficiently, they're also a real hassle for relating data since they're so big. It's usually better to include a standard id INT AUTO_INCREMENT PRIMARY KEY column and then have things like ENumber being a secondary UNIQUE constraint.
You can then relate data using the 4-byte id value, or 8-byte if BIGINT is a concern like you might have two billion employees.

MYSQL table column contraint based upon another tables columns

This is my first post and I'm a mysql noob, so I apologize for this question's length.
BACKGROUND
I have a lookup table cctypevals, with a foreign field 'cctypeID', in mysql this would be:
CREATE TABLE `cctypevals` (
`cctypevalsKEY` integer NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`cctypeID` varchar(50) NOT NULL, FOREIGN KEY(cctypeID) REFERENCES cctype(cctypeID) ,
`value` varchar(50) )
cctypeID contains field names from user tables, eg 'taskSTATE', 'serviceTYPE', 'projectCAT' etc.
The value field contains the only allowed values for these user table fields.
Thus cctypevals acts like a 'multi' keyed lookup table, for example:
select value from cctypevals where cctypeID ='serviceTYPE'
might return HomeVisit, BackToBase etc
I know it would be easier to have one lookup table per field but this is what I have.
QUESTION
How do I constrain (in a sql create table or alter statement), tables with fields like task.taskSTATE, service.serviceTYPE etc so they can only accept values from cctypevals.value where cctypeID contains the appropriate field name ?
In create or alter table statement you cannot do that, since the check constraint would be able to such things, but mysql has not implemented the check constraint yet (mysql can parse a check constraint, but it will not work).
You can create before insert and update triggers that check the specific restrictions and raise an sql error message if the updated value does not meet the requirements.

MySQL insert if data doesn't exist(excluding primary key)

I am trying to do a sql insert only if the data doesn't already exist in the table. I have a language table that has a auto incremented primary id field and a string description field to capture the name of the language(ex. English). I want to be able to insert into the database only if the description field is not already there. The primary id doesn't matter. So I only want to insert into the table if "English" isn't already a value in the table. Also, I am only given the description to check if it's already in the database (I mention this because the ON DUPLICATE KEY requires all fields to be matching and I don't have the id).
I have searched and tried so many queries without any of them producing the correct results. Any ideas would be helpful. Thanks.
You would simply add a UNIQUE constraint to the description field.
and then use "INSERT ... ON DUPLICATE KEY UPDATE" in your code. Refer this link.
if you need to update rows that have UNIQUE key conflict - you should use "meda" answer.
if you need only insert rows without duplicates you should also add UNIQUE constraint but with using query:
INSERT IGNORE INTO table SET ....
this query will insert row that have no conflict and ignore others without any mysql errors

How do I mitigate duplicate row inserts based on a non-key column?

I need to import data from one MySQL table into another. The old table has a different outdated structure (which isn't terribly relevant). That said, I'm appending a field to the new table called "imported_id" which saves the original id from the old table in order to prevent duplicate imports of the old records.
My question now is, how do I actually prevent duplicates? Due to the parallel rollout of the new system with the old, the import will unfortunately need to be run more than once. I can't make the "import_id" field PK/UNIQUE because it will have null values for fields that do not come from the old table, thereby throwing an error when adding new fields. Is there a way to use some type of INSERT IGNORE on the fly for an arbitrary column that doesn't natively have constraints?
The more I think about this problem, the more I think I should handle it in the initial SELECT. However, I'd be interested in quality mechanisms by which to handle this in general.
Best.
You should be able to create a unique key on the import_id column and still specify that column as nullable. It is only primary key columns that must be specified as NOT NULL.
That said, on the new table you could specify a unique key on the nullable import_id column and then handle any duplicate key errors when inserting from the old table into the new table using ON DUPLICATE KEY
Here's a basic worked example of what I'm driving at:
create table your_table
(id int unsigned primary key auto_increment,
someColumn varchar(50) not null,
import_id int null,
UNIQUE KEY `importIdUidx1` (import_id)
);
insert into your_table (someColumn,import_id) values ('someValue1',1) on duplicate key update someColumn = 'someValue1';
insert into your_table (someColumn) values ('someValue2');
insert into your_table (someColumn) values ('someValue3');;
insert into your_table (someColumn,import_id) values ('someValue4',1) on duplicate key update someColumn = 'someValue4';
where the first and last inserts represent inserts from the old table and the 2nd and 3rd represent inserts from elsewhere.
Hope this helps and good luck!