I am trying to reseed my table column but getting error
'Employees' does not contain an identity column
I have checked my table and it does have an ID column.
DDL
CREATE TABLE [dbo].[Employees]
(
[ID] [int] NOT NULL,
[FirstName] [nvarchar](max) NULL,
[MiddleName] [nvarchar](max) NULL,
[LastName] [nvarchar](max) NULL,
[Email] [nvarchar](max) NULL,
CONSTRAINT [PK_Employees]
PRIMARY KEY CLUSTERED ([ID] ASC)
)
You need to specify the column as the IDENTITY column, it is not assumed/implicit.
CREATE TABLE [dbo].[Employees]
([ID] [int] IDENTITY(1,1) NOT NULL,
-- rest of columns
Related
I have two tables
CREATE TABLE [dbo].[Customer]
(
[SourceID] [int] NOT NULL,
[ID_N] [bigint] NOT NULL,
[RegionCode] [varchar](1) NOT NULL,
[NSID] [int] NOT NULL,
CONSTRAINT [Pk_Customer] PRIMARY KEY CLUSTERED
([RegionCode] ASC, [ID_N] ASC)
)
CREATE TABLE [dbo].[Order]
(
[Reference][nvarchar](50) NOT NULL,
[SourceID] [int] NOT NULL,
[ID_N] [nvarchar](50) NULL,
[RegionCode] [varchar](1) NULL,
[Customer_ID_N] [bigint] NULL,
CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED
([Reference] ASC)
)
I want to put (CustomemrID_N,RegionCode) as a Foreign Key reference to the Customer table.
Alter table [dbo].[Order]
ADD CONSTRAINT FK_Order_Customer FOREIGN KEY (Customer_ID_N,RegionCode)
REFERENCES[dbo].[Customer](ID_N,RegionCode)
but i get this error :
There are no primary or candidate keys in the referenced table 'dbo.Customer' that match the referencing column list in the foreign key 'FK_Order_Customer'.
There are so many problems with your attempt... Most are typographical.
Firstly this statement:
CREATE TABLE [dbo].[Customer](
[SourceID] [int] NOT NULL,
[ID_N] [bigint] NOT NULL,
[RegionCode] [varchar](1) NOT NULL,
[NSID] [int] NOT NULL,
CONSTRAINT [Pk_Customer] PRIMARY KEY CLUSTERED
([RegionCode] ASC, [ID_N] ASC)
This is missing a right parenthesis ()) at the end of the statement.
Then the next statement:
CREATE TABLE [dbo].[Order](
[Reference][nvarchar](50) NOT NULL,
[SourceID] [int] NOT NULL,
[ID_N] [nvarchar](50) NULL,
[RegionCode] [varchar](1) NULL,
[Customer_ID_N] [bigint] NULL,
CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED
([Reference] ASC)
This too, isn't valid, there is also a missing right parenthesis.
Your last statement is also, not valid:
Alter table [dbo].[Order]
ADD CONSTRAINT FK_Order_CustomerFOREIGN KEY ([Customer_ID_N],RegionCode)
REFERENCES[dbo].[Customer](ID_N,RegionCode)
CONSTRAINT FK_Order_CustomerFOREIGN KEY should be CONSTRAINT FK_Order_Customer FOREIGN KEY; notice the space between FK_Order_Customer and `FOREIGN.
Once we fix that we get yet something like this:
CREATE TABLE [dbo].[Customer](
[SourceID] [int] NOT NULL,
[ID_N] [bigint] NOT NULL,
[RegionCode] [varchar](1) NOT NULL,
[NSID] [int] NOT NULL,
CONSTRAINT [Pk_Customer] PRIMARY KEY CLUSTERED
([RegionCode] ASC, [ID_N] ASC))
GO
CREATE TABLE [dbo].[Order](
[Reference][nvarchar](50) NOT NULL,
[SourceID] [int] NOT NULL,
[ID_N] [nvarchar](50) NULL,
[RegionCode] [varchar](1) NULL,
[Customer_ID_N] [bigint] NULL,
CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED
([Reference] ASC))
GO
Alter table [dbo].[Order]
ADD CONSTRAINT FK_Order_Customer FOREIGN KEY ([Customer_ID_N],RegionCode)
REFERENCES[dbo].[Customer](ID_N,RegionCode)
Note, when you run that, you get the error you have above. That's because your PK is defined as [RegionCode] ASC, [ID_N] ASC not ID_N,RegionCode. So let's fix that as swap the foreign key around...
Alter table [dbo].[Order]
ADD CONSTRAINT FK_Order_Customer FOREIGN KEY (RegionCode,[Customer_ID_N])
REFERENCES[dbo].[Customer](RegionCode, ID_N)
And now it works.
Address the typographical errors, then read the error; it was telling you the problem.
For a school project I have made a .csv import via C# and imported all the data from the file into a table containing only strings. We have to do some validation on the imported code using SQL server which I have already done. The table I have imported my data into looks like this:
CREATE TABLE [dbo].[StoreData]
(
[StoreName] [nvarchar](max) NULL,
[Street] [nvarchar](max) NULL,
[StreetNumber] [nvarchar](max) NULL,
[City] [nvarchar](max) NULL,
[ZipCode] [nvarchar](max) NULL,
[TelephoneNumber] [nvarchar](max) NULL,
[Country] [nvarchar](max) NULL
)
With this table filled, I have to Insert this data into the [Stores] table :
CREATE TABLE [dbo].[Stores]
(
[Id] [nvarchar](450) NOT NULL, <- GUID
[Name] [nvarchar](85) NOT NULL,
[CountryCode] [nvarchar](max) NOT NULL,
[AddressId] [nvarchar](450) NULL <- FK to [Address] Table
)
And here is my problem, the [Stores] contains a FK to the [Addresses] table:
CREATE TABLE [dbo].[Addresses]
(
[Id] [nvarchar](450) NOT NULL, <- GUID
[Street] [nvarchar](100) NOT NULL,
[HouseNumber] [nvarchar](4) NOT NULL,
[Addition] [nvarchar](10) NULL,
[ZipCode] [nvarchar](6) NOT NULL,
[City] [nvarchar](85) NOT NULL,
[SeriesIndicationStart] [int] NOT NULL,
[SeriesIndicationEnd] [int] NOT NULL
CONSTRAINT [PK_Addresses] PRIMARY KEY CLUSTERED
)
So now I have [StoreData] that contains the data I have to put in [Addresses] and in [Stores], and I have to keep in mind that the FK has to be set in [Stores]. This is our first database semester, and I am clueless, and tomorrow is the deadline..
I hope someone can help me out.. thanks in advance!
I have 2 tables (BSH_FLD1) and (BSH_IMGVER).These tables are related by a third table called (BSH_FLDIMG1).One id field in (BSH_FLD1) can relate to many id images in (BSH_IMGVER).
The script for creating the tables is this :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[BSH_FLD1](
[IDFIELD] [int] NOT NULL,
[EC_NOP] [int] NULL,
[EC_NOI] [int] NULL,
[EC_NOA] [int] NULL,
[INTESTIMI] [varchar](20) COLLATE Albanian_CI_AI NULL,
[KUTIA] [int] NULL,
[DREJTORIA] [varchar](30) COLLATE Albanian_CI_AI NULL,
[DOSJA] [int] NULL,
[VITI] [int] NULL,
[AFATI] [varchar](20) COLLATE Albanian_CI_AI NULL,
[FILLIMI] [datetime] NULL,
[FUNDI] [datetime] NULL,
[FAQE] [int] NULL,
[FILMI] [int] NULL,
[BLIP] [varchar](10) COLLATE Albanian_CI_AI NULL,
[DEPARTAMENTI] [varchar](150) COLLATE Albanian_CI_AI NULL,
[LLOJI_I_DOKUMENTIT] [varchar](50) COLLATE Albanian_CI_AI NULL,
[ADRESIMI] [varchar](30) COLLATE Albanian_CI_AI NULL,
[KLASIFIKIMI] [varchar](30) COLLATE Albanian_CI_AI NULL,
[KOMENTE] [varchar](255) COLLATE Albanian_CI_AI NULL,
[FRAKSION] [int] NULL,
[OBJEKTI] [varchar](50) COLLATE Albanian_CI_AI NULL,
CONSTRAINT [P080318150042500] PRIMARY KEY CLUSTERED
(
[IDFIELD] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[BSH_FLDIMG1](
[IDFIELD] [int] NOT NULL,
[IDIMAGE] [int] NOT NULL,
CONSTRAINT [P080318150042718] PRIMARY KEY CLUSTERED
(
[IDFIELD] ASC,
[IDIMAGE] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
USE [eCodis_Data]
GO
ALTER TABLE [dbo].[BSH_FLDIMG1] WITH CHECK ADD CONSTRAINT [F080318150042828] FOREIGN KEY([IDFIELD])
REFERENCES [dbo].[BSH_FLD1] ([IDFIELD])
GO
ALTER TABLE [dbo].[BSH_FLDIMG1] WITH CHECK ADD CONSTRAINT [F080318150042937] FOREIGN KEY([IDIMAGE])
REFERENCES [dbo].[BSH_IMG] ([IDIMAGE])
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[BSH_IMGVER](
[IDIMAGE] [int] NOT NULL,
[VERSION] [int] NOT NULL,
[IDDATASET] [int] NULL,
[IDVOLUME] [int] NULL,
[IMAGE] [varchar](250) COLLATE Albanian_CI_AI NULL,
[CHECKEDOUT] [smallint] NULL,
[CHECKOUTBY] [varchar](250) COLLATE Albanian_CI_AI NULL,
[ADDDATE] [datetime] NULL,
[ADDBY] [varchar](250) COLLATE Albanian_CI_AI NULL,
[ORGFNAME] [varchar](250) COLLATE Albanian_CI_AI NULL,
[NROFPAGES] [int] NULL,
CONSTRAINT [P080318150040953] PRIMARY KEY CLUSTERED
(
[IDIMAGE] ASC,
[VERSION] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
USE [eCodis_Data]
GO
ALTER TABLE [dbo].[BSH_IMGVER] WITH CHECK ADD CONSTRAINT [F080318150041062] FOREIGN KEY([IDIMAGE])
REFERENCES [dbo].[BSH_IMG] ([IDIMAGE])
GO
ALTER TABLE [dbo].[BSH_IMGVER] WITH CHECK ADD CONSTRAINT [F080318150041171] FOREIGN KEY([IDDATASET])
REFERENCES [dbo].[BSH_DATASET] ([IDDATASET])
GO
ALTER TABLE [dbo].[BSH_IMGVER] WITH CHECK ADD CONSTRAINT [F080318150041281] FOREIGN KEY([IDVOLUME])
REFERENCES [dbo].[VOLUME] ([IDVOLUME])
I want to take distinct rows but since the relation is one to many the following query gives all the id fields that relate to an id image
Select DISTINCT
BSH_FLD1.IDFIELD as name,
'Dosje' as type,
'arkivuar' as 'ecm:currentLifeCycleState',
BSH_FLD1.IDFIELD as 'dc:title',
BSH_FLD1.IDFIELD,
BSH_FLD1.EC_NOP,
BSH_FLD1.EC_NOI,
BSH_FLD1.EC_NOA,
BSH_FLD1.INTESTIMI,
BSH_FLD1.KUTIA,
BSH_FLD1.DREJTORIA,
BSH_FLD1.DOSJA,
BSH_FLD1.VITI,
BSH_FLD1.AFATI,
CONVERT(VARCHAR(10), BSH_FLD1.FILLIMI,103) as FILLIMI,
CONVERT(VARCHAR(10),BSH_FLD1.FUNDI,103) as FUNDI,
BSH_FLD1.FAQE,
BSH_FLD1.FILMI,
BSH_FLD1.BLIP,
BSH_FLD1.DEPARTAMENTI,
BSH_FLD1.LLOJI_I_DOKUMENTIT,
BSH_FLD1.ADRESIMI,
BSH_FLD1.KLASIFIKIMI,
BSH_FLD1.KOMENTE,
BSH_FLD1.FRAKSION,
BSH_FLD1.OBJEKTI,
BSH_IMGVER.IDIMAGE,
BSH_IMGVER.VERSION,
BSH_IMGVER.IDDATASET,
BSH_IMGVER.IDVOLUME,
BSH_IMGVER.IMAGE as 'file:content',
BSH_IMGVER.CHECKEDOUT,
BSH_IMGVER.CHECKOUTBY,
CONVERT(VARCHAR(10),BSH_IMGVER.ADDDATE,103) as ADDDATE,
BSH_IMGVER.ADDBY,
BSH_IMGVER.ORGFNAME,
BSH_IMGVER.NROFPAGES
From BSH_FLDIMG1
JOIN BSH_FLD1 ON BSH_FLDIMG1.IDFIELD = BSH_FLD1.IDFIELD
JOIN BSH_IMGVER ON BSH_FLDIMG1.IDIMAGE = BSH_IMGVER.IDIMAGE
This brings me for example id field 1-idimage 1 and idfield 1-idimage 2
.I want only one row for one id field.
Now the result set is:
|name|type |.......|idImage|
|1 |Dosje|.......|1
|1 |Dosje|.......|2
|2 |Dosje|.......|3
|2 |Dosje|.......|4
|2 |Dosje|.......|5
I want to have the following :
|name|type |.......|idImage|
|1 |Dosje|.......|1
|2 |Dosje|.......|3
Can someone help me??
I have 2 tables
tbl_jobs
CREATE TABLE [dbo].[tbl_jobs]
(
[JobID] [int] IDENTITY(1,1) NOT NULL,
[JobType] [nvarchar](50) NOT NULL,
[RequestID] [int] NOT NULL,
[AssignTo] [int] NOT NULL,
[FromOrgID] [int] NOT NULL,
[ToOrgID] [int] NOT NULL,
[Ammount] [nvarchar](50) NOT NULL,
[JobStatus] [nvarchar](50) NOT NULL,
[Remark] [nvarchar](50) NULL,
[strOwner] [nvarchar](50) NOT NULL,
[dbTstamp] [datetime2](7) NOT NULL,
CONSTRAINT [PK_tbl_jobs]
PRIMARY KEY CLUSTERED ([JobID] ASC)
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tbl_jobs] WITH CHECK
ADD CONSTRAINT [FK_tbl_jobs_tbl_orgs]
FOREIGN KEY([FromOrgID]) REFERENCES [dbo].[tbl_orgs] ([OrgID])
GO
ALTER TABLE [dbo].[tbl_jobs] CHECK CONSTRAINT [FK_tbl_jobs_tbl_orgs]
GO
ALTER TABLE [dbo].[tbl_jobs] WITH CHECK
ADD CONSTRAINT [FK_tbl_jobs_tbl_orgs1]
FOREIGN KEY([ToOrgID]) REFERENCES [dbo].[tbl_orgs] ([OrgID])
GO
tbl_orgs
CREATE TABLE [dbo].[tbl_orgs]
(
[OrgID] [int] IDENTITY(1,1) NOT NULL,
[OrgName] [nvarchar](50) NOT NULL,
[OrgTele] [nvarchar](50) NULL,
[OrgEmail] [nvarchar](50) NULL,
[OrgArea] [nvarchar](50) NOT NULL,
[OrgCity] [nvarchar](50) NOT NULL,
[OrgLocation] [nvarchar](50) NOT NULL,
[OrgType] [nvarchar](50) NOT NULL,
[OrgStatus] [nvarchar](50) NOT NULL,
[strOwner] [nvarchar](50) NOT NULL,
[dbTStamp] [datetime2](7) NOT NULL,
CONSTRAINT [PK_tbl_orgs]
PRIMARY KEY CLUSTERED ([OrgID] ASC)
) ON [PRIMARY]
GO
I need to get most of the tbl_jobs columns and corresponding tbl_orgs.OrgName for tbl_jobs.FromOrgID & tbl_jobs.ToOrgID.
If I choose tbl_orgs.orgname, I don't get the proper results.
I am stuck here. What type of query should I use to get the result.?
You're joining two times to the same table instance. You should click on "add table" and add tbl_orgs one more time and join "FromOrgID" to one instance of it and "ToOrgID" to the other one. Otherwise the join doesn't make sense unless "FromOrgID" and "ToOrgID" are equal.
I have created 2 tables # CustomerHistory and # CustomerPaymentHistory
here is the sql syntax
CREATE TABLE [dbo].[CustomerHistory]
(
[ID] INT IDENTITY (1, 1) NOT NULL,
[HistoryID] AS ('CLI'+right('000000000'+CONVERT([varchar](10),[ID]),(5))) PERSISTED NOT NULL,
[CustomerID] INT NOT NULL,
[InvoiceNumber] NVARCHAR(30) NOT NULL,
[InvoiceDate] DATETIME NOT NULL,
[InvoiceTotal] NVARCHAR(20) NOT NULL,
[Balance] NVARCHAR(20) NOT NULL,
CONSTRAINT [PK_CUSTOMERHISTORY]
PRIMARY KEY CLUSTERED ([ID] ASC, [CustomerID] ASC),
CONSTRAINT [FK_CUSTOMER_CH]
FOREIGN KEY ([CustomerID]) REFERENCES [dbo].[Customers] ([ID])
)
and
CREATE TABLE [dbo].[CustomerPaymentHistory]
(
[ID] INT IDENTITY (1, 1) NOT NULL,
[PaymentID] AS ('CLI'+right('000000000'+CONVERT([varchar](10),[ID]),(5))) PERSISTED NOT NULL,
[HistoryID] INT NOT NULL,
[PaymentDate] DATETIME NOT NULL,
[PaymentAmount] NVARCHAR(20) NOT NULL,
[BalanceDue] NVARCHAR(20) NULL,
[PaidInFull] BIT NOT NULL,
CONSTRAINT [PK_CUSTOMERPAYMENTHISTORY]
PRIMARY KEY CLUSTERED ([ID] ASC, [HistoryID] ASC),
CONSTRAINT [FK_CUSTOMERHISTORY_CPH]
FOREIGN KEY ([HistoryID]) REFERENCES [dbo].[CustomerHistory] ([ID])
)
but when i'm trying to update the table, I get an error:
Update cannot proceed due to validation errors.
Please correct the following errors and try again.
SQL71516 :: The referenced table '[dbo].[CustomerHistory]' contains no
primary or candidate keys that match the referencing column list in
the foreign key. If the referenced column is a computed column, it
should be persisted.
How can fix this problem?