I am trying to create a table in SQL and keep getting an error - sql

I'm using Teradata SQL assistant to create a table. The code I am using is as follows:
CREATE TABLE calendar
(
CalendarKey INT NOT NULL,
FullDate DATE NOT NULL,
DayOfWeek VARCHAR(20) NOT NULL,
DayOfMonth INT NOT NULL,
Month VARCHAR(20) NOT NULL,
Qtr VARCHAR(2) NOT NULL,
Year VARCHAR(4) NOT NULL
PRIMARY KEY (CalendarKey)
);
I get this error when I try to execute the command:
[Teradata Database] [3707] Syntax error, expected something like a 'CHECK' keyword between ',' and the 'Month' keyword.
Does anyone know what the issue is?

As the error implies month (and year, too) is a reserved keyword in Teradata, which can't be used as column name.
You might double quote it (but then you have to double quote it in every query, too) or you change the name. There's another issue, a missing comma before the primary key constraint:
CREATE TABLE calendar
(
CalendarKey INT NOT NULL,
FullDate DATE NOT NULL,
DayOfWeek VARCHAR(20) NOT NULL,
DayOfMonth INT NOT NULL,
"Month" VARCHAR(20) NOT NULL,
Qtr VARCHAR(2) NOT NULL,
"Year" VARCHAR(4) NOT NULL,
PRIMARY KEY (CalendarKey)
);

Try this way
CREATE TABLE calendar (
CalendarKey INT NOT NULL,
FullDate DATE NOT NULL,
DayOfWeek VARCHAR(20) NOT NULL,
DayOfMonth INT NOT NULL,
Month VARCHAR(20) NOT NULL,
Qtr VARCHAR(2) NOT NULL,
Year VARCHAR(4) NOT NULL
)
and
ALTER TABLE `calendar` ADD PRIMARY KEY (`CalendarKey`);

Try this, to have a table with PK as part of Table Definition
CREATE TABLE calendar
(
CalendarKey INT PRIMARY KEY NOT NULL,
FullDate DATE NOT NULL,
[DayOfWeek] VARCHAR(20) NOT NULL,
[DayOfMonth] INT NOT NULL,
[Month] VARCHAR(20) NOT NULL,
Qtr VARCHAR(2) NOT NULL,
[Year] VARCHAR(4) NOT NULL
);
Or this to have Primary Key PK with an Identity [auto counter]
CREATE TABLE calendar
(
CalendarKey INT PRIMARY KEY Identity(1,1) NOT NULL,
FullDate DATE NOT NULL,
[DayOfWeek] VARCHAR(20) NOT NULL,
[DayOfMonth] INT NOT NULL,
[Month] VARCHAR(20) NOT NULL,
Qtr VARCHAR(2) NOT NULL,
[Year] VARCHAR(4) NOT NULL
);
- Note, it's recommended to use prackets [] if the col name is a reserved key-word , like [year]

Related

Error relate SQL Server tables

I have the following problem: when we run the code, it displays the following error:
There are primary keys or candidates in the reference Ticket table
that match the list of referencing columns in foreign key '
FK__Payment__PkTicke__1A14E395
Code:
create table SystemUser
(
PkUser int identity(1,1),
UserLogin nvarchar(20) not null unique,
UserPassword nvarchar(50) not null,
UserName nvarchar(50) not null,
UserCpf nvarchar(50) not null,
UserBirth datetime not null,
UserGender nvarchar(15) not null,
AddressCep int not null,
AddressStreet nvarchar(50) not null,
AddressNumber nvarchar(20) not null,
AddressComplement nvarchar(50) not null,
AddressCity nvarchar(50) not null,
AddressState nvarchar(50) not null,
primary key(PkUser)
)
create table Attractions
(
PkAttraction integer identity(1,1) ,
AttractionName nvarchar(50) not null unique,
AttractionDate datetime not null,
AttractionDescription nvarchar(150) not null
primary key(PkAttraction)
)
create table Ticket
(
PkTicket int identity(1,1),
PkUser int not null,
PkAttraction int not null,
TicketPrice decimal not null,
primary key(PkTicket, PkUser, PkAttraction),
foreign key(PkUser) references SystemUser(PkUser),
foreign key(PkAttraction) references Attractions(PkAttraction)
)
create table Payment
(
PkPayment int identity(1,1),
PkTicket int not null,
Portion int not null,
IdTransaction nvarchar(100) not null,
Payday datetime not null,
primary key(PkPayment, PkTicket),
foreign key(PkTicket) references Ticket(PkTicket),
)
create table FormPayment
(
PkFromPayment int identity(1,1),
PkPayment int not null,
ShareValue decimal not null,
ExpirationDate datetime not null
primary key(PkFromPayment, PkPayment),
foreign key(PkPayment) references Payment(PkPayment),
)
Your Ticket table as a primary key made up from 3 columns:
create table Ticket
(
.....
primary key(PkTicket, PkUser, PkAttraction),
....
)
Any table that wants to reference that table Ticket must also provide all 3 columns for the foreign key.
You cannot reference only part of a primary key - if you want to reference it, you must have all columns that it contains - otherwise you cannot establish a FK relationship.
So you must add the PkUser and PkAttraction columns to your Payment table so that you can establish this FK relationship:
create table Payment
(
PkPayment int identity(1,1),
PkTicket int not null,
PkUser int not null, // add this
PkAttraction int not null, // add this
Portion int not null,
IdTransaction nvarchar(100) not null,
Payday datetime not null,
primary key(PkPayment, PkTicket),
// change to this
foreign key(PkTicket, PkUser, PkAttraction) references Ticket(PkTicket, PkUser, PkAttraction)
.....
)
When you not specify a name for FK and PK, SQL server generates a name. in this case, looks like SQL server generates duplicate name.
If you specify name for FK and PK, it will work.

Can one table have two identity columns in SQL Server?

I am trying to make two columns auto increment but this column shows an error [user_id] as id + 0 PRIMARY KEY NOT NULL saying
Only UNIQUE or PRIMARY KEY constraints can be created on computed columns
What I am trying to do is, if id = 1, make user_id= 1 as well.
CREATE TABLE [dbo.TBL_TXN_USER]
(
[id] int NOT NULL IDENTITY(1,1),
[user_id] as id + 0 PRIMARY KEY NOT NULL ,
[username] varchar(150) NOT NULL,
[fullname] varchar(150) NOT NUll,
[pwd] varchar(50) NOT NUll,
[email] varchar(150) NOT NULL,
[mobile] varchar(150) NOT NULL,
[designation] varchar(150) NOT NULL,
[deleted] int NULL,
[created_date] datetime NULL,
[creator_user_id] int NULL,
[changed_date] datetime NULL,
[changer_user_id] int NULL,
[add_content] int NULL,
[edit_content] int NULL,
[delete_content] int NULL,
[manage_user] int NULL,
[view_log] int NULL,
)
What is wrong in [user_id]? How to solve it?
the error message is because you put the NOT NULL constraint on the computed column.
on sql server 2012 the complete error message is:
Only UNIQUE or PRIMARY KEY constraints can be created on computed
columns, while CHECK, FOREIGN KEY, and NOT NULL constraints require
that computed columns be persisted.
here is a working script (i changed the table name):
CREATE TABLE dbo.[TBL_TXN_USER]
(
[id] int NOT NULL IDENTITY(1,1),
[user_id] as id + 0 persisted not null primary key,
[username] varchar(150) NOT NULL,
[fullname] varchar(150) NOT NUll,
[pwd] varchar(50) NOT NUll,
[email] varchar(150) NOT NULL,
[mobile] varchar(150) NOT NULL,
[designation] varchar(150) NOT NULL,
[deleted] int NULL,
[created_date] datetime NULL,
[creator_user_id] int NULL,
[changed_date] datetime NULL,
[changer_user_id] int NULL,
[add_content] int NULL,
[edit_content] int NULL,
[delete_content] int NULL,
[manage_user] int NULL,
[view_log] int NULL,
);
GO
i have a couple of comments about that question .
- a calculated field with a fixed formula with static values as primary key instead of the id itself is a waste of resources: one of the 2 fields should not be there
- a field with the name of a system function (user_id) is something i would avoid at all costs.
- the question looks like an attempt to put in place a solution (the calculated field as id) for an hidden issue.
Sorry for my misunderstanding, So you want to add auto increments two column in one table. Actually that is not accept at SQL-server so I am going to give you another option below
CREATE TRIGGER [dbo].[insert_triger] ON [dbo].[TBL_TXN_USER]
FOR INSERT
AS
update TBL_TXN_USER set [user_id] = id
where id = (
select MAX(id)
from TBL_TXN_USER
)
column aliases work with select statement not create table, also for [user_id] you didn't provide any data type.
Use the following to create your table :
CREATE TABLE [dbo.TBL_TXN_USER](
[id] int NOT NULL IDENTITY(1,1),
[user_id] int PRIMARY KEY NOT NULL ,
....rest of code
To update [user_id] consider using a trigger.

SQL database management

I'm currently having issues with my database. It shows this error:
Msg 1911, Level 16, State 1, Line 13
Column name 'suffix_desc' does not exist in the target table or view.
Msg 1750, Level 16, State 0, Line 13
Could not create constraint. See previous errors.
This is the code,
create table prefix
(
prefix_desc varchar(4) not null,
primary key (prefix_desc)
)
create table suffix
(
suffix_desc varchar(4) not null,
primary key (suffix_desc)
)
create table member
(
member_id varchar(80) not null,
First_name varchar(50) not null,
Last_name varchar(50) not null,
middle_name varchar(50) null,
Dob date not null,
Email varchar(80) not null,
UserID varchar(80) null,
Password varchar(16) not null,
phone_friendly_ind varchar(3) not null,
SMS_ind varchar(3) not null,
return_ind varchar(3) not null,
fuel_prepay_ind varchar(3) not null,
post_mail_ind varchar(3) not null,
email_ind varchar(3) not null,
privacy_ind varchar(3) not null,
primary key (member_id, suffix_desc, prefix_desc),
foreign key (suffix_desc) references suffix (suffix_desc),
foreign key (prefix_desc) references prefix (prefix_desc)
)
I think you forgot to add this :
create table member(
...
suffix_desc varchar(4) not null,
prefix_desc varchar(4) not null,
...
)

sql query default in oracle

create table dvds(
dvdid number not null primary key,
dvdname varchar(60) not null,
numdisks number not null default 1,
yearrlsd date not null,
mtypeid varchar(4) not null,
stuid varchar(4) not null,
ratingid varchar(4) not null,
formid char(2) not null,
statid char(3) not null,
foreign key (mtypeid) references movietypes (mtypeid)
)
whrn i am running this command in oracle it is giving error:
missing right parenthesis
but after removing default it is working
The default value should come before the constraints (not null);
numdisks number default 1 not null,
SQLfiddle.

postgresql syntax error when creating a table

Hey everyone I need some help with creating tables. I have the script below and it creates a few tables. When i try to run the script it give me this error:
psql:script.sql:10: ERROR: syntax error at or near "Group"
LINE 6: CREATE TABLE Group(
Can anyone tell me what is going on?
CREATE TABLE Group(
name varchar(40) PRIMARY KEY NOT NULL
);
CREATE TABLE Artist(
name varchar(30) PRIMARY KEY NOT NULL,
birthplace varchar(20) NOT NULL,
age int NOT NULL CHECK (age > 0),
style varchar(20) NOT NULL
);
CREATE TABLE Artwork(
title varchar(40) PRIMARY KEY NOT NULL,
artist varchar(30) NOT NULL references Artist(name),
group_name varchar(40) NOT NULL references Group(name),
year int NOT NULL CHECK (year > 0),
type varchar(30) NOT NULL,
price money NOT NULL,
);
CREATE TABLE Customer(
cust_id int PRIMARY KEY NOT NULL,
name varchar(40) NOT NULL,
address varcahr(60) NOT NULL,
amount money NOT NULL CHECK(amount > 0),
like_artist varchar(30) NOT NULL references Artist(name),
like_group varchar(40) NOT NULL references Group(name)
);
it had a lot of problems
this worked for me. In postgres, names that are not all lowercase need to be double quoted. also some of your table names are reserved words, money can't be > and int, and there was a comma out of place.
CREATE TABLE "group"( name varchar(40) PRIMARY KEY NOT NULL );
CREATE TABLE artist( name varchar(30) PRIMARY KEY NOT NULL, birthplace varchar(20) NOT NULL, age int NOT NULL CHECK (age > 0), style varchar(20) NOT NULL );
CREATE TABLE artwork( title varchar(40) PRIMARY KEY NOT NULL, artist varchar(30) NOT NULL references artist(name),
group_name varchar(40) NOT NULL references "group"(name), year int NOT NULL CHECK (year > 0), type varchar(30) NOT NULL, price money NOT NULL );
CREATE TABLE customer( cust_id int PRIMARY KEY NOT NULL, name varchar(40) NOT NULL, address varchar(60) NOT NULL,
amount money NOT NULL CHECK(amount > cast(0.0 as money)), like_artist varchar(30) NOT NULL references artist(name), like_group varchar(40) NOT NULL references "group"(name) );