I have a column in one table that gives me the values I expect as a primary key, in another table I have the same column as a foreign key, but when I query this other table the values that exist are only -1, and 1.
Is this some kind of lookup or index? What is going on here?
Thank you
SQL Server 2014
Related
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.
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.
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.
I have two tables, let's call them charts and charts_tree. The charts_tree.idDir (auto-increment, integer) column is constrained to charts.chart_tree_dir. Both tables are InnoDB based.
Now I am trying to insert a row in charts_tree with null value for idDir (due to its auto-increment nature), but I am getting a foreign constrain fails error message on this column. How is this possible, considering that both tables are blank initially and there are no interconnected columns?
Is the foreign key constraint on the idDir column and references the chart_tree_dir column ?
That's what I understood from what you said ; if that's the case, when you try to insert a row in charts_tree, the auto_incremented value generated by mysql is not present in charts at the moment of creation (since both tables are blank), thus the foreign key failing.
It should be the other way around I think.
The input file name is employee and it contain the data like:
eno,ename,esal
1,sravan,2000
2,kumar,12000
3,naresh,10000
3,sathish,1500
I need to load this data into a sql server table that is emp table, but the eno field is a primary key. When I tried to insert the data, I was given an error. So how can I resolve this problem?
Assuming I understand what you are asking, you are receiving an error because you are trying to insert the same ENO (3rd record and 4th record both containing an ENO of 3) into the primary key. Your primary key uniquely identifies each record in the table. Primary keys cannot contain nulls and must be unique.
You can either modify the SQL table by adding an auto-incrementing field and allow that new field to become your primary key, or clean your data only allowing unique ENOs to be passed through to your SQL server table.