Q:

Failing to create table in SQL

CREATE TABLE example 
(
ycode char(5) NOT NULL,
xcode char(10) NOT NULL,
CONSTRAINT PK_EXAMPLE PRIMARY KEY (ycode,xcode),
CONSTRAINT FK_EXAMPLE_Y FOREIGN KEY (ycode) REFERENCES ycode(ycode),
CONSTRAINT FK_EXAMPLE_X FOREIGN KEY (xcode) REFERENCES xcode(xcode),
);

This is the script that I am using. It is followed up by the error
Server: Msg 1776, Level 16, State 1, Line 1
There are no primary or candidate keys in the referenced table 'xcode'
that match the referencing column list in the foreign key 'FK_EXAMPLE_X'.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.

My X table has 5 fields but one of them is xcode as a primary key.

A:

You don't have a primary key in your secondary table you are attempting to join to.  Add a primary key to your table you are trying to join to and ensure that the data types are the same for the secondary table's type and the primary.
Example:
--Works
IF OBJECT_ID('.X') IS NOT NULL 
    DROP TABLE .X;
CREATE TABLE X (Id INT IDENTITY, YId INT, Val VARCHAR(8), CONSTRAINT PK_X_Id PRIMARY KEY (Id))

IF OBJECT_ID('.Y') IS NOT NULL 
    DROP TABLE .Y;
CREATE TABLE Y (Id INT IDENTITY, Val VARCHAR(8), CONSTRAINT PK_Y_Id PRIMARY KEY (Id))

ALTER TABLE dbo.X
ADD CONSTRAINT FK_X_Y_Id FOREIGN KEY(YId) REFERENCES Y(Id)

--Fails, due to Table Y not having appropriate Primary Key
IF OBJECT_ID('.X') IS NOT NULL 
    DROP TABLE .X;
CREATE TABLE X (Id INT IDENTITY, YId INT, Val VARCHAR(8), CONSTRAINT PK_X_Id PRIMARY KEY (Id))

IF OBJECT_ID('.Y') IS NOT NULL 
    DROP TABLE .Y;
CREATE TABLE Y (Id INT IDENTITY, Val VARCHAR(8))

ALTER TABLE dbo.X
ADD CONSTRAINT FK_X_Y_Id FOREIGN KEY(YId) REFERENCES Y(Id)

