problem creating a simple relationship in SQL Server 2005 - sql

I am using the database diagram to simply drag one column in a table to another to associate them and then trying to save it. i have done this a million times in the past with no problems. Both of the data types are the same, uniqueidentifier.
Here is the error I get:
'Customer ' table saved successfully
'CustomerOrder ' table
- Unable to create relationship 'FK_CustomerOrder_Customer'.
The
ALTER TABLE statement conflicted with
the FOREIGN KEY constraint
"FK_CustomerOrder_Customer". The
conflict occurred in database
"mydatabase", table "Customer", column
'CustomerID'.
Not sure how to trouble shoot this.

It means that there's a CustomerID in the CustomerOrder which can not be found in the Customer table.
Run this query inside SQL Server Management Studio separately:
SELECT *
FROM CustomerOrder co
WHERE NOT EXISTS (SELECT * FROM Customer c WHERE c.CustomerID = co.CustomerID)
and that should tell you what the "bad" Customer Order records are.

Are there customer orders with customer id's that don't exist in the customer table?

Related

SSMS SQL - Create table with related columns

I have a DB Diagram with relationships on primary and foreign keys. Then I create a new table in the diagram, with foreign keys to 2 other tables. Example:
Tables in the diagram: Customers, Orders and Status
New table: View (with fk to Customers(CustID), Orders(OrID) and Status(SID))
In view table, I want a column Ordertype which should be a related/linked column to Orders table.
Meaning, that whenever I change a value of Ordertype in my new View table, it gets updated in the Orders table as well.
How can I achieve this? I guess I need sql script that adds such a related column to my view table.
What I have tried:
Relationship Orders(PK) and View(FK) has update property set to "Cascade".
Given same column name and datatype to Ordertype in both tables, Orders and View
But, this doesn't work for me.
Thanks.
Foreign key constraints can't help you to update data. You could try to use DML Trigger.
For example,
CREATE TRIGGER InsertUpdateTrigger ON View
AFTER Insert, Update AS
BEGIN
UPDATE O
SET O.OrderType = I.OrderType
FROM ORDERS O
JOIN INSRTED I on O.orid = I.id(change to your id)
END

one-to-one tables relationship, linked by the autonumber in the main table

I have a main table that contain the customers details, I built another table that contain for example a yes/no fields about if the customer paid his taxes, the two tables is linked with autonumber from the main table.
I want always to keep them both with the same amount of records (that means every customer has a record in the second table even if the second table has empty record with data only in the primary key field)
I need that cause with missing records I cannot run update query to auto fill the second table and i got an error of validation rule violation.
I use this sql:
update clients LEFT JOIN MonthlyTbl ON clients.SerialNo = MonthlyTbl.serialno
set sReport04='ready';
I have almost 700 records in the main table and only 80 records in the second, and when I run the sql it updates only 80!!!!
Thanks for Help
Please use below query,
update clients set sReport04='ready' where SerialNo in
(select serialno from MonthlyTbl);
here is the right answer
first run the sql:
INSERT INTO monthlytbl ( serialno )
SELECT clients.serialno FROM clients
WHERE (((clients.[serialno]) Not In (select serialno from monthlytbl)));
and then:
select sreport04 from monthlytbl
set sReport04='ready';

Merge and populate tables

I have 2 tables in MS Access, that are updated externally each day (any tables I add will be deleted when the copy arrives in the morning):
Current status of a customer
All customer errors
The current status of a customer includes all customers in a particular process, and shows which status they are in today. The "all customer errors" table shows the details of customers IF they had an error at some stage. Both tables have some fields in common, but not everything (so a simple union isn't possible).
I need some help to do the following:
Join the tables and create a column stating the initial table they came from. I realise I will have some duplicates.
Taking "current status of a customer" table, populate the missing data from "all customer errors"
create an extra column - "number of errors" where I count the number of times the customer appeared in the error table
Help!
My SQL skills are a bit basic, but improving each day :-)
Thanks
Kirstin
You must have a primary key and create an inner join. Use this formula in the SQL tab. You can then go into design view and use 'make table'. Simply select what data you want to appear.
'SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;'
When you have your new table you can update it to have new columns etc.
'UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;'

Tables not showing anything when creating new one

I have problem with SQL .
I am trying to create tables from other tables with foreign keys. The table is created normally without problem but when I'm trying to see the data inside the table there is none of the data inside! Anyone who knows?
-- Here is my table with the foreign keys
CREATE TABLE StaffXCustomersXMeals(
--MealID int FOREIGN KEY REFERENCES Meals(MealID),
--CustomersID int FOREIGN KEY REFERENCES Customers(CustomersID),
--StaffID int FOREIGN KEY REFERENCES Staff(StaffID)
)
You might want to consider reading up on the concept of a SQL View - think of it as a "virtual table based on the result-set of an SQL statement." When you create a view based on the JOIN between multiple tables, it saves your query and you can then select from it like you would a table.
For example, you might have the following:
CREATE VIEW StaffCustomerMeals
AS
SELECT
m.MealID,
c.CustomersID,
s.StaffID
FROM
Meals m
LEFT JOIN
Customers c ON
m.SomeIDThatMeansThisCustomer = c.CustomersID
LEFT JOIN
Staff s ON
m.SomeIDThatMeansAStaffMember = s.StaffID
You need to define these relationships in accordance with your own schema - it's your homework assignment, so do your best to figure it out. A couple more questions to consider:
Does a meal always have both a customer and a staff member, or are they customers who might happen to be staff members?
Should you include other information besides the IDs (e.g., CustomerName, StaffMemberDepartment, MealPrice, PurchaseDate)
You didn't enter any data. You only said that whatever data entered must reference the other tables. So you cannot enter invalid meals for instance.
You need INSERT statments to fill the table with whatever data you need.

Import Data and Ignore Duplicates in SQL Server

I'm using SQL Server 2008 R2. I have a source table of data (I_Vendor) that may have duplicates on the CompanyName column. I want to import that data into a new table (Vendor) but the new table has a Name column (that corresponds to CompanyName) with a unique constraint on it. It's been a while since I've done SQL but I saw the MERGE function, and it appears it fits the bill. I wrote the following:
MERGE Vendor AS T
USING I_Vendor AS S
ON (T.Name = S.CompanyName)
WHEN NOT MATCHED BY TARGET
THEN INSERT(VendorId, Name, ContactName, ContactInfoId)
VALUES(S.Vendor_ID, S.CompanyName, S.ContactName, S.Vendor_ID+10000);
It generates a "Violation of UNIQUE KEY constraint" and gives the name of the unique constraint on Vendor.Name. Anybody know what I'm doing wrong?
Your MERGE statement will insert all rows from I_Vendor that do not have a matching row in the Vendor table.
For example, suppose there are two rows in the I_Vendor table with company name "X", and further suppose that company name "X" does not appear in the Vendor table, then both rows will be inserted into the Vendor table, violating the constraint.
To fix the problem, you need to ensure that there is only one row per company name in the source data of the MERGE statement. The following merge statement does this, but as Aditya Naidu has already pointed out, we don't know what you want to do when there multiple records with the same company name in the I_Vendor table:
MERGE Vendor AS T
USING (SELECT MAX(Vendor_ID), CompanyName, MAX(ContactName), MAX(ContactInfoId)
FROM I_Vendor
GROUP BY CompanyName) AS S
WHEN NOT MATCHED BY TARGET
THEN INSERT(VendorId, Name, ContactName, ContactInfoId)
VALUES(S.Vendor_ID, S.CompanyName, S.ContactName, S.Vendor_ID+10000);