Assign default value to TIMESTAMP_NTZ snowflake - snowflake-cloud-data-platform

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);

Related

INSERT statement conflicted with the FOREIGN KEY constraint? I tried to insert data in the table ''customer'' but I always got this error

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,...);

Foreign key reference mismatched data type error

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

Error while applying a default constraint

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

how to refer a computed column in MS SQL?

I have written a query as follows.
create table registration(
id int identity(1000,1) ,
first_name varchar(45) unique,
sur_name varchar(45),
address_line1 varchar(45),
address_line2 varchar(45),
state varchar(45),
city varchar(45),
email_id varchar(45),
contact_no varchar(45),
date_of_birth date,
apply_type varchar(45),
qualification varchar(45),
gender varchar(45),
password as CONVERT(VARCHAR(4),DATEPART(dd,GETDATE())) +
substring(CONVERT(VARCHAR(4),DATENAME(mm,GETDATE())),1,3) +
convert(varchar(4),FLOOR(RAND(CHECKSUM(NEWID()))*(999-100)+100)),
hint_question varchar(50),
hint_answer varchar(50),
user_id as substring(apply_type,1,4) + convert(char(45),id)
persisted primary key not null);
create table passport(
id int identity(1000,1),
Passport_Number as substring(Booklet_type,1,2) +
convert(varchar(100),id) persisted primary key not null,
user_id varchar(200) constraint fk_uid foreign key(user_id) references
registration(user_id) on delete cascade,
Type_of_Passport varchar(45),
Type_of_Service varchar(50),
Booklet_type varchar(50),
Address1 varchar(50),
Address2 varchar(50),
City varchar(50),
State varchar(50),
Country varchar(50)n
Pin int,
Number_of_Years int,
Date_Of_Application date,
Issue_Date date,
Amount int,
Reason_for_reissue varchar(50),
Expired_Date date);
But I'm getting the following error:
Column 'registration.user_id' is not the same length or scale as referencing column 'passport.user_id' in foreign key 'fk_uid'. Columns participating in a foreign key relationship must be defined with the same length and scale.
How to rectify this problem?
cast or convert the user_id in registration to varchar(200) ie same as the one in passport
user_id as CONVERT(VARCHAR(200),
substring(apply_type,1,4) +
convert(char(45),id) )
persisted primary key not null);

foreign key references to a table problem here

create database Exer4
use Exer4
create table customer (
cus_code int,
constraint PK_customer primary key (cus_code),
cus_Lname varchar(20),
cus_Fname varchar (30),
cus_intial varchar (2),
cus_areacode int,
cus_phone int,
cus_balance float
)
create table charter (
char_trip int,
constraint PK_charter primary key (char_trip),
char_date date,
ac_number varchar(5),
foreign key(ac_number) references aircraft,
char_destination varchar(5),
char_distance float,
char_hours_flown float,
char_hours_wait float,
char_fuel_gallons float,
cus_code int,
foreign key(cus_code) references customer
)
create table aircraft(
ac_number varchar(5),
constraint PK_aircraft primary key(ac_number),
mod_code varchar(10),
foreign key(mod_code) references model,
ac_itaf varchar(10),
ac_tiel varchar(10),
ac_tier varchar(10),
)
create table model(
mod_code int,
constraint PK_model primary key(mod_code),
mod_manufacturer varchar(10),
mod_name varchar(10),
mod_seats int,
mod_chg_mile int,
)
create table crew (
char_trip int,
emp_num int,
constraint PK_crew primary key (char_trip,emp_num),
foreign key (char_trip) references charter,
foreign key(emp_num) references employee,
crew_job varchar(10)
)
create table rating (
rtg_code varchar(5),
rtg_name varchar (30),
constraint PK_rating primary key (rtg_code)
)
create table employee (
emp_num int,
constraint PK_employee primary key (emp_num),
emp_title varchar (4),
emp_lname varchar (20),
emp_fname varchar (30),
emp_initial varchar (2),
emp_dob date,
emp_hire_date date,
)
create table pilot (
emp_num int,
pl_license varchar (3),
pl_ratings varchar (30),
pl_med_type int,
pl_med_date date,
pl_pt135_date date,
constraint PK_pilot primary key (emp_num)
)
why is that when i reference to "aircraft" it said invalid table??
what is wrong with my code??
You have to specify which field to reference:
foreign key (mod_code) references model (mod_code),
The foreign key constraint consists of 2 parts:
ALTER TABLE aircraft
ADD CONSTRAINT fk_aircraft_model
FOREIGN KEY (mod_code) -- here you specify the field(s) to reference from in the aircraft table
REFERENCES model (mod_code) -- here you specify the field(s) to reference to in the model table
You are creating the tables in the wrong order. charter references aircraft but create table aircraft doesn't appear until later.

Resources