I'm getting error saying:
incorrect syntax near for.
CREATE TABLE Test (ID INT not null IDENTITY Primary key,
CreatedBy VARCHAR(20) not null,
CreatedDate DATETIME not null,
UpdatedBy VARCHAR(20) not null,
LastUpdated DATETIME not null,
CONSTRAINT Test_CreatedBy DEFAULT USER_NAME() FOR CreatedBy,
CONSTRAINT Test_CreatedDate DEFAULT GETDATE() FOR CreatedDate,
CONSTRAINT Test_UpdatedBy DEFAULT USER_NAME() FOR UpdatedBy,
CONSTRAINT Test_LastUpdated DEFAULT GETDATE() FOR LastUpdated)
go
Try this syntax:
CREATE TABLE Test
(
ID INT not null IDENTITY Primary key,
CreatedBy VARCHAR(20) not null
CONSTRAINT Test_CreatedBy DEFAULT USER_NAME(),
CreatedDate DATETIME not null
CONSTRAINT Test_CreatedDate DEFAULT GETDATE(),
UpdatedBy VARCHAR(20) not null
CONSTRAINT Test_UpdatedBy DEFAULT USER_NAME(),
LastUpdated DATETIME not null
CONSTRAINT Test_LastUpdated DEFAULT GETDATE()
);
GO
dbfiddle here
Or if you prefer use ALTER TABLE:
CREATE TABLE Test
(
ID INT not null IDENTITY Primary key,
CreatedBy VARCHAR(20) not null,
CreatedDate DATETIME not null,
UpdatedBy VARCHAR(20) not null,
LastUpdated DATETIME not null
);
GO
ALTER TABLE Test
ADD CONSTRAINT Test_CreatedBy DEFAULT USER_NAME() FOR CreatedBy;
GO
ALTER TABLE Test
ADD CONSTRAINT Test_CreatedDate DEFAULT GETDATE() FOR CreatedDate;
GO
ALTER TABLE Test
ADD CONSTRAINT Test_UpdatedBy DEFAULT USER_NAME() FOR UpdatedBy;
GO
ALTER TABLE Test
ADD CONSTRAINT Test_LastUpdated DEFAULT GETDATE() FOR LastUpdated;
GO
dbfiddle here
You could define the constraint as column level as follow
CREATE TABLE Test (
ID INT NOT NULL IDENTITY(1,1) Primary key,
CreatedBy VARCHAR(20) NOT NULL DEFAULT USER_NAME(),
CreatedDate DATETIME NOT NULL DEFAULT GETDATE(),
UpdatedBy VARCHAR(20) NOT NULL DEFAULT USER_NAME(),
LastUpdated DATETIME NOT NULL DEFAULT GETDATE());
GO
Related
I need to assign default value to column SERVERTIME with datatype TIMESTAMP_NTZ in snowflake.
I have a below query:-
CREATE TABLE STG_ORDER_DETAIL
(
ORDERID NUMBER(38,0) not null,
ORDER_TYPE VARCHAR(3),
AGGRID VARCHAR(20),
AGGRNAME VARCHAR(250),
MERCHANTID VARCHAR(20) not null,
SERVERTIME NOT NULL DEFAULT '1900-01-01'::TIMESTAMP_NTZ(9),
CURRENCY VARCHAR(5),
constraint STG_ORDER_DETAIL_PK primary key (ORDERID, MERCHANTID) not enforced);
getting syntax error.
Please make sure the data type is included and matched with the expression:
CREATE TABLE STG_ORDER_DETAIL
(
ORDERID NUMBER(38,0) not null,
ORDER_TYPE VARCHAR(3),
AGGRID VARCHAR(20),
AGGRNAME VARCHAR(250),
MERCHANTID VARCHAR(20) not null,
SERVERTIME TIMESTAMP_NTZ(9) NOT NULL DEFAULT '1900-01-01'::TIMESTAMP_NTZ(9),
CURRENCY VARCHAR(5),
constraint STG_ORDER_DETAIL_PK primary key (ORDERID, MERCHANTID) not enforced);
So I tried to insert data in the ''customer'' table but I always get this certain error (at the bottom).
Can anyone pinpoint if I miss anything or if it's lacking?
create table rooms
(
roomid int identity(1,1) primary key,
roomNo varchar(250) not null unique,
roomType varchar(250) not null,
bed varchar(250) not null,
price bigint not null,
booked varchar(50) default 'NO'
);
insert into rooms (roomNo, roomType, bed, price) values (010, 'Studio with AC', 'Single', '1000');
create table customer
(
cid int identity(1,1) primary key,
cname varchar(250) not null,
mobile bigint not null,
nationality varchar(250) not null,
gender varchar(50) not null,
dob varchar(50) not null,
idproof varchar(50) not null,
addres varchar(250) not null,
checkIn varchar(250) not null,
checkOut varchar(250),
chekOut varchar(250) not null default 'NO',
roomid int foreign key references rooms(roomid)
);
insert into customer (cname,mobile,nationality,gender,dob,idproof,addres,checkIn,customer.roomid) values ('Kayano Ai', 09238400394, 'Japanese', 'Female', '01-02-1998', '8923giaf', 'Somewhere', 'YES', 20);
the error notif I got is
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__customer__roomid__1F98B2C1". The conflict occurred in database "myHotel", table "dbo.rooms", column 'roomid'.
When you enter row into your customer table, customer.roomid should exist in the rooms table first.
Fist make sure the room exists or insert a new room.
INSERT INTO rooms ( roomid,roomNo, ...) VALUES (1,'room1',...);
Then insert into customer with an custormer.roomid that exists in the room table (1 in the example)
INSERT INTO customer ( cid,cname,roomid ...) VALUES (15,'customer name',1,...);
I want to create a table with operational details like:
CRAETED_ON DATETIME NOT NULL DEFAULT GETDATE(),
CREATED_BY VARCHAR(10) NOT NULL,
DELETED_ON DATETIME NULL,
DELETED_BY VARCHAR(10) NULL
I want to put constraint IF DELETED_ON is updated then they should provide the DELETED_BY.
BOTH should be NULL or both should not be NULL are allowed. One NULL & other NOT NULL is not allowed.
This can be accomplished with a table level check constraint, assuming you want the constraint to apply to both inserts and updates:
CREATE TABLE dbo.Example(
CREATED_ON DATETIME NOT NULL DEFAULT GETDATE(),
CREATED_BY VARCHAR(10) NOT NULL,
DELETED_ON DATETIME NULL,
DELETED_BY VARCHAR(10) NULL
,CONSTRAINT IF_DELETED_ON CHECK ((DELETED_ON IS NULL AND DELETED_BY IS NULL) OR (DELETED_ON IS NOT NULL AND DELETED_BY IS NOT NULL))
);
The ArtistID column in Piece table refers to the ArtistID in the Artist table. Likewise, the LocationID column in Piece refers to LocationID in the GeographicLocation table. However, both foreign key references throw a "not the same data type as referencing column" error. What am I doing wrong?
CREATE TABLE dbo.Artist
(
ArtistID SMALLINT PRIMARY KEY IDENTITY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(40) NOT NULL,
Nationality VARCHAR(10) NULL,
BirthYear SMALLINT NOT NULL CHECK(BirthYear <= 1980),
DeathYear SMALLINT NULL,
Sex CHAR(1) NOT NULL CHECK(Sex = 'F' OR Sex = 'M')
)
CREATE TABLE dbo.Piece
(
PieceID SMALLINT PRIMARY KEY IDENTITY(1, 5),
ArtistID SMALLINT NOT NULL
FOREIGN KEY REFERENCES Artist(ArtistID),
LocationID SMALLINT NOT NULL
FOREIGN KEY REFERENCES GeographicLocation(LocationID),
CommonName VARCHAR(100) NULL,
YearProduced TINYINT NULL,
Period VARCHAR(50) NULL,
Medium VARCHAR(35) NOT NULL,
Frame VARCHAR(35) NULL,
AppraisedValue MONEY NOT NULL,
AppraiserID SMALLINT NOT NULL
FOREIGN KEY REFERENCES Appraiser(AppraiserID)
)
CREATE TABLE dbo.GeographicLocation
(
LocationID SMALLINT PRIMARY KEY IDENTITY,
Country VARCHAR(25) NOT NULL,
City VARCHAR(50) NOT NULL
)
I think you missed a table here, which is AppraiserID column in Piece table refers to the AppraiserID in the Appraisertable
So, when I execute your SQL I am getting below error:
Foreign key 'FK__Piece__Appraiser__322C6448' references invalid table 'Appraiser'.
First Create a table called Appraiser, then create Foreign key references for those column.
Below SQL I am able to create all tables and Foreign key references:
CREATE TABLE dbo.Artist
(ArtistID SMALLINT PRIMARY KEY IDENTITY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(40) NOT NULL,
Nationality VARCHAR(10) NULL,
BirthYear SMALLINT NOT NULL CHECK(BirthYear <= 1980),
DeathYear SMALLINT NULL,
Gender CHAR(1) NOT NULL CHECK(Gender = 'F' OR Gender = 'M'))
CREATE TABLE dbo.GeographicLocation
(LocationID SMALLINT PRIMARY KEY IDENTITY,
Country VARCHAR(25) NOT NULL,
City VARCHAR(50) NOT NULL)
CREATE TABLE dbo.Appraiser
(AppraiserID SMALLINT PRIMARY KEY IDENTITY(1, 5),
AppraisedValue MONEY NOT NULL,
AppraisedName VARCHAR(100) NULL)
CREATE TABLE dbo.Piece
(PieceID SMALLINT PRIMARY KEY IDENTITY(1, 5),
ArtistID SMALLINT NOT NULL,
LocationID SMALLINT NOT NULL,
CommonName VARCHAR(100) NULL,
YearProduced TINYINT NULL,
Period VARCHAR(50) NULL,
Medium VARCHAR(35) NOT NULL,
Frame VARCHAR(35) NULL,
AppraisedValue MONEY NOT NULL,
AppraiserID SMALLINT NOT NULL)
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_ArtistID FOREIGN KEY(ArtistID)
REFERENCES dbo.Artist (ArtistID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_ArtistID
GO
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_AppraiserID FOREIGN KEY(AppraiserID)
REFERENCES dbo.Appraiser (AppraiserID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_AppraiserID
GO
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_LocationID FOREIGN KEY(LocationID)
REFERENCES dbo.GeographicLocation (LocationID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_LocationID
GO
Why does SCOPE_IDENTITY() return NULL after inserting a row in the ComponentAssociation table (as ##IDENTITY returns the right Id)
while SCOPE_IDENTITY() returns the right Id after inserting a row in the CustomerProjectAssociation table ?
The two association tables are created the same way.
Here is an extract of the table creation script:
-- Creating table 'CustomerProjectAssociation'
CREATE TABLE [dbo].[CustomerProjectAssociation]
(
[Id] int IDENTITY(1,1) NOT NULL,
[CustomerId] int NOT NULL,
[ProjectId] int NOT NULL,
[CreationDate] datetime NOT NULL CONSTRAINT DF_CustomerProjectAssociation_CreationDate DEFAULT (SYSUTCDATETIME()),
[LastModificationDate] datetime NOT NULL CONSTRAINT DF_CustomerProjectAssociation_ModificationDate DEFAULT (SYSUTCDATETIME())
);
GO
-- Creating table 'ComponentAssociation'
CREATE TABLE [dbo].[ComponentAssociation]
(
[Id] int IDENTITY(1,1) NOT NULL,
[EcuId] int NOT NULL,
[CreationDate] datetime NOT NULL CONSTRAINT DF_ComponentAssociation_CreationDate DEFAULT (SYSUTCDATETIME()),
[LastModificationDate] datetime NOT NULL CONSTRAINT DF_ComponentAssociation_ModificationDate DEFAULT (SYSUTCDATETIME()),
[ComponentId] int NOT NULL
);
GO
-- Creating primary key on [Id] in table 'CustomerProjectAssociation'
ALTER TABLE [dbo].[CustomerProjectAssociation]
ADD CONSTRAINT [PK_CustomerProjectAssociation]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO
-- Creating primary key on [Id] in table 'ComponentAssociation'
ALTER TABLE [dbo].[ComponentAssociation]
ADD CONSTRAINT [PK_ComponentAssociation]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO
And here are two queries executed on the database from SQL Server Management Studio:
INSERT [dbo].[CustomerProjectAssociation]([CustomerId], [ProjectId])
VALUES (1, 2)
SELECT
[RowCount] = ##RowCount,
[##IDENTITY] = ##IDENTITY,
[SCOPE_IDENTITY] = SCOPE_IDENTITY()
Result:
RowCount ##IDENTITY SCOPE_IDENTITY
1 24 24
INSERT [dbo].[ComponentAssociation]([EcuId], [ComponentId])
VALUES(1, 2)
SELECT
[RowCount] = ##RowCount,
[##IDENTITY] = ##IDENTITY,
[SCOPE_IDENTITY] = SCOPE_IDENTITY()
Result:
RowCount ##IDENTITY SCOPE_IDENTITY
1 613 NULL
OK, the issue is solved.
Found the solution here: error when inserting into table having instead of trigger from entity data framework
I added the following select statement at the end of the instead of insert,update trigger returning all the computed columns:
select [Id], [CreationDate], [LastModificationDate] from {0}.[dbo].[ComponentAssociation] where ##ROWCOUNT > 0 and Id = scope_identity()