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);
Related
I am using Microsoft SQL Server and trying to add a foreign key to the "Orders" table that references the primary key in my "Customer" table. I keep getting this message:
Msg 1769, Level 16, State 1, Line 28
Foreign key 'orders_customerid_fk' references invalid column 'CustomerID' in referencing table 'Orders'.
Msg 1750, Level 16, State 0, Line 28
Could not create constraint or index. See previous errors.
CREATE TABLE Customer
(
CustomerID INT NOT NULL PRIMARY KEY,
fName varchar(40),
lName varchar(40),
City varchar(40),
Country varchar(40),
Phone varchar(20)
);
CREATE TABLE Supplier
(
SupplierID INT NOT NULL PRIMARY KEY,
CompanyName varchar(40),
ContactName varchar(50),
ContactTitle varchar(40),
City varchar(40),
Country varchar(40),
Phone varchar(30),
Fax varchar(30),
);
CREATE TABLE Orders
(
OrderID INT NOT NULL PRIMARY KEY,
OrderDate datetime,
OrderNumber varchar(10),
TotalAmount decimal(12,2)
);
ALTER TABLE Orders
ADD CONSTRAINT Orders_CustomerID_FK
FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID);
You are missing the column that should receive the constrain of FK.
ALTER TABLE Orders
ADD CustomerID INT NULL; /*Adds a new int column existing rows will be
given a NULL value for the new column*/
Or
ALTER TABLE Orders
ADD CustomerID INT NOT NULL DEFAULT(0);
And then you can
ALTER TABLE Orders
ADD CONSTRAINT Orders_CustomerID_FK
FOREIGN KEY(CustomerID)
REFERENCES Customer(CustomerID)
;
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);
Working on small ASP.Net MVC project, and got requirement from business analyst who gave me PDF file with below SQL staff to create in database.
Looking on how to achieve result from the below SQL question.
By the way should all be created using T-SQL
Student - Table
studentNo Identity column INT NOT NULL PK
lastName VARCHAR(30) NOT NULL
firstName VARCHAR(30) NOT NULL
gender CHAR NOT NULL
phoneNumber VARCHAR(20) NOT NULL
salaryAmount INT,
departmentID INT FK
Department - Table
departmentID Identity Column INT NOT NULL PK
name VARCHAR(40) NOT NULL
priceCentreID INT FK
PriceCentre - Table
priceCentreID
name VARCHAR(50)
accountID varchar(50)
As I understand you want help to create the sql code to generate the tables you provided?
Below is the code to create the tables and the relations that you are looking for.
code:
CREATE TABLE PriceCentre(
priceCenterID INT PRIMARY KEY,
name VARCHAR(50),
accountID VARCHAR(50)
);
GO
CREATE TABLE Department(
departmentID INT NOT NULL PRIMARY KEY,
name VARCHAR(40) NOT NULL,
priceCenterID int FOREIGN KEY REFERENCES PriceCentre (priceCenterID)
)
GO
CREATE TABLE Student(
studentNo INT NOT NULL PRIMARY KEY,
lastName VARCHAR(30) NOT NULL,
firstName VARCHAR(30) NOT NULL,
gender CHAR NOT NULL,
phoneNumber VARCHAR(20) NOT NULL,
salaryAmount INT,
departmentID INT FOREIGN KEY REFERENCES Department(departmentID)
);
I have a school project where I need to create a database in SQL Server. I am currently having issues entering in my tables.
I have successfully entered in the first two tables, but the associative table is erroring, as are other tables following.
create table Home
(
HomeID INT Identity (1,1) Primary key,
Address1 varchar(250) not null,
City varchar(25) not null,
State1 varchar(25) not null,
Zipcode CHAR(5) not null,
SmokeDetectors INT,
Co2Detectors INT,
SecuritySystem CHAR(1),
TotalSqFt int,
Rooms INT,
AdditionalBuilding CHAR(1),
AssessedValue DEC(10,2),
YearAssessed char(4),
CompanyID int not null,
)
create table MortgageCompany
(
CompanyID INT Identity (1,1) Primary key,
CompanyName varchar(100) not null,
Address1 varchar(250),
City varchar(25),
State1 varchar(25),
Zipcode char(5),
HomeID int not null
)
create table HomeMortgageCompany
(
HomeID INT,
CompanyID INT,
foreign key CompanyID references MortgageCompany(CompanyID),
foreign key HomeID references Home(HomeID),
Primary key (HomeID,CustomerID)
)
create table Loan
(
LoanID INT Identity (1,1) Primary key,
LoanNumber int not null,
MortgageAmount dec(10,2),
CompanyID int
foreign key CompanyID references MortgageCompany(CompanyID)
)
create table PersonalProperty
(
PropertyID INT Identity (1,1) Primary key,
ItemName int,
Type1 varchar(150) not null,
Value1 Dec(10,2) not null,
HomeID int
foreign key HomeID references Home(HomeID)
)
create table Policy
(
PolicyID INT Identity (1,1) Primary key not null,
PolicyYear char(4),
PremiumCharged dec(10,2),
EffectiveDate date not null,
ExpirationDate date not null,
ReviewDateTime datetim,
Decision char(8),
HomeID int
foreign key HomeID references Home(HomeID)
)
create table Bank
(
AccountID int Identity (1,1) Primary Key,
BankName varchar(150) not null,
AccountNumber int not null,
RoutingNumber int not null,
Amount Dec(10,2),
PolicyID int not null
)
create table PolicyBank
(
PolicyID int,
AccountID INT,
foreign key PolicyID references Policy(PolicyID),
foreign key AccountID references Bank(AccountID),
Primary Key (PolicyID, AccountID)
)
Create table PolicyHolder
(
SSN char(9) Primary key not null,
FirstName varchar(50) not null,
LastName varchar(50) not null,
)
create table PolicyPolicyHolder
(
SSN char(9),
PolicyID INT,
foreign key PolicyID references Policy(PolicyID),
foreign key SSN references PolicyHolder(SSN),
Primary key (SSN, PolicyID)
)
create table HomePolicyHolder
(
SSN char(9),
HomeID Int,
foreign key HomeID references Home(HomeID),
foreign key SSN references PolicyHolder(SSN),
Primary key (SSN, HomeID)
)
create table Employment
(
SSN char(9) Primary Key,
EmployerFirstName varchar(50)not null,
EmployerLastName varchar(50) not null,
Address1 varchar(250),
City varchar(25),
State1 varchar(25),
Zipcode char(5),
StartDate date,
CurrentSalary dec(10,2) not null,
JobTitle varchar (75),
RecordExtractionDate date not null,
foreign key SSN references PolicyHolder(SSN)
)
Create table Document
(
DocumentID INT Identity (1,1) Primary key,
DocumentType varchar(50),
DateTimeSent datetime not null,
SSN char(9) not null,
PolicyID int,
foreign key PolicyID references Policy(PolicyID)
)
create table PolicyHolderDocument
(
DocumentID int,
SSN char(9),
foreign key DocumentID references Document(DocumentID),
foreign key SSN references PolicyHolder(SSN)
);
For the third table definition:
create table HomeMortgageCompany (
HomeID INT,
CompanyID INT,
foreign key CompanyID references MortgageCompany(CompanyID),
foreign key HomeID references Home(HomeID),
Primary key (HomeID,CustomerID)
)
Problems with this code:
the column name in the defina of the foreign key needs to be surrounded with parentheses
the primary key refers to column CustomerID, but there is no column in the table
Presumably, you meant:
create table HomeMortgageCompany (
HomeID INT,
CompanyID INT,
foreign key (CompanyID) references MortgageCompany(CompanyID),
foreign key (HomeID) references Home(HomeID),
Primary key (HomeID,CompanyID)
)
Here is the database query needed to create the database:
create table Area
(
AreaId int primary key identity(1,1),
Name nvarchar(64) not null
)
create table Level
(
LevelId int primary key identity(1,1),
Name nvarchar(32) not null,
Principle nvarchar(512) not null
)
create table Subject
(
SubjectId int primary key identity(1,1),
AreaId int foreign key references Area(AreaId),
LevelId int foreign key references Level(LevelId),
Name nvarchar(32) not null,
Abbreviation nvarchar(16)
)
create table StaffType
(
StaffTypeId int primary key identity(1,1),
Name nvarchar(64) not null
)
create table Staff
(
StaffId int primary key identity(1,1),
StaffTypeId int foreign key references StaffType(StaffTypeId),
Name nvarchar(128) not null,
LastNameFather nvarchar(256) not null,
LastNameMother nvarchar(256) not null,
DateOfBirth datetime,
PlaceOfBirth nvarchar(256),
Sex nvarchar(8) not null,
Carnet nvarchar(64),
Telephone nvarchar(64),
MobilePhone nvarchar(64),
Address nvarchar(256),
FatherName nvarchar(256),
MotherName nvarchar(256),
FatherContactNumber nvarchar(64),
MotherContactNumber nvarchar(64),
FatherPlaceOfWork nvarchar(64),
MotherPlaceOfWork nvarchar(64),
DateOfHiring datetime,
YearsOfService int,
Formation nvarchar(128)
)
create table Grade
(
GradeId int primary key identity(1,1),
Name nvarchar(32) not null,
LevelId int foreign key references Level(LevelId),
Observation nvarchar(256)
)
create table GradeInstance
(
GradeInstanceId int primary key identity(1,1),
StaffId int foreign key references Staff(StaffId),
GradeId int foreign key references Grade(GradeId),
Name nvarchar(32) not null,
Year datetime
)
create table Student
(
StudentId int primary key identity(1,1),
RUDE int,
Name nvarchar(64) not null,
LastNameFather nvarchar(256) not null,
LastNameMother nvarchar(256) not null,
DateOfBirth datetime not null,
PlaceOfBirth nvarchar(128),
Sex nvarchar(8),
Carnet nvarchar(32),
Telephone nvarchar(64),
MobilePhone nvarchar(64),
Address nvarchar(256),
FatherName nvarchar(512),
MotherName nvarchar(512),
FatherMobilePhone nvarchar(64),
MotherMobilePhone nvarchar(64),
FatherProfession nvarchar(64),
MotherProfession nvarchar(64),
FatherPlaceOfWork nvarchar(256),
MotherPlaceOfWork nvarchar(256),
Observations nvarchar(3000)
)
create table StudentInstance
(
StudentInstanceId int primary key identity(1,1),
GradeInstanceId int foreign key references GradeInstance(GradeInstanceId),
StudentId int foreign key references Student(StudentId)
)
create table StudentGradeReport
(
StudentGradeReportId int primary key identity(1,1),
StudentInstanceId int foreign key references StudentInstance(StudentInstanceId),
SubjectId int foreign key references Subject(SubjectId),
FirstTrimester int,
SecondTrimester int,
ThirdTrimester int,
FinalGrade int
)
If you find an attribute that should be null checked, please disregard it. I've gone over this with the client and they want certain things to left blank if so chosen by the end user.
My main concern when designing this database was how to associate a student with a grade here and now, yet keep a record for previous years and manage to see what grade he got back in 2009. See?
I think I've done a good job but you never know - the hivemind here will probably find a flaw and I'd love some feedback.
No longer using SQLite, but MSSQL.
I've tried to normalize the database as much as I could, but not religiously, just as much as it took to make it 'click'.
Before a Student, was directly related to a Classroom (Grade) instance. Now I create 'instances' of Students and assign them to a Classroom (Grade) instance. Each instance has a date saying which year it belongs to. This helps me keep a record of previous years without dirty hacks.
create table Area
(
AreaId int primary key identity(1,1),
Name nvarchar(64) not null
)
"Name" should be UNIQUE.
create table Level
(
LevelId int primary key identity(1,1),
Name nvarchar(32) not null,
Principle nvarchar(512) not null
)
"Name" should be UNIQUE.
create table Subject
(
SubjectId int primary key identity(1,1),
AreaId int foreign key references Area(AreaId),
LevelId int foreign key references Level(LevelId),
Name nvarchar(32) not null,
Abbreviation nvarchar(16)
)
Odds are good that one or more of these apply. Impossible to tell without representative sample data.
The set {AreaId, LevelId} should be
UNIQUE.
"Name" should be UNIQUE.
"Abbreviation" should be UNIQUE.
I'm running out of time.
create table StaffType
(
StaffTypeId int primary key identity(1,1),
Name nvarchar(64) not null
)
"Name" should be UNIQUE.
More later if I have time.