String or binary data would be truncated on INSERT statement - sql

I am doing school project and I have to create table and insert data by myself. The following is my SQL statements.
CREATE TABLE Package
(pkgcode char(4) primary key,
pkgname varchar(30) not null,
pkgdescription varchar(60) not null,
pickuplocation varchar(20) not null,
costperadult decimal (7,2) not null,
costperchild decimal (7,2) not null,
pkgremarks varchar(30) null)
INSERT INTO Package
VALUES
('K009',
'8D7N FUJIAN/MT.WUYI',
'A whole new experience: enjoy multi nation architecture in Gulangyu Island and UNESCO Mt.Wuyi',
'Jurong East',
700.00,
500.00,
'Hotel accommodation will be 4 stars.')
But I got the following error which is at the insert statement.
(Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.)
I would appreciate if anyone of you can help me. Thank you.

You're trying to insert a description that's too long for the column's specification. Just create the column with a larget size (e.g., here 100 characters should suffice) and you should be OK:
CREATE TABLE Package
(pkgcode char(4) primary key,
pkgname varchar(30) not null,
pkgdescription varchar(100) not null, -- Here!
pickuplocation varchar(20) not null,
costperadult decimal (7,2) not null,
costperchild decimal (7,2) not null,
pkgremarks varchar(30) null
)

Related

what does the error "missing right parenthesis" in oracle sql means

I'm trying to run this code and it seems correct to me but I'm getting an error stating that there's a right parenthesis missing.
The code is the following:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
DATE_OF_BIRTH DATE,
PHONE_NB CHAR(8) NOT NULL,
ADDRESS VARCHAR(50),
TOTAL_SPENDING FLOAT NOT NULL DEFAULT 0.0,
PRIMARY KEY(CUSTOMER_ID)
);
Can anyone help me in solving my problem?
Since you tagged SQL Developer...
...the tool tries to give you a heads-up there will be a problem before you even hit the Execute button
The default value for the column is confusing the parser because it's not expected at that point.
Move it to after the data type and you'll be good
CREATE TABLE customer (
customer_id INT NOT NULL,
name VARCHAR2(30) NOT NULL,
date_of_birth DATE,
phone_nb CHAR(8) NOT NULL,
address VARCHAR(50),
total_spending FLOAT DEFAULT 0.0 NOT NULL,
PRIMARY KEY ( customer_id )
);
PS In oracle, use VARCHAR2, not VARCHAR. While VARCHAR will 'work', it's reserved and could mean something different in a future release.
You are using wrong order of column definition clauses: the constraint (NOT NULL) should follow the default value.
This is the right way:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
DATE_OF_BIRTH DATE,
PHONE_NB CHAR(8) NOT NULL,
ADDRESS VARCHAR(50),
TOTAL_SPENDING FLOAT DEFAULT 0.0 NOT NULL ,
PRIMARY KEY(CUSTOMER_ID)
);

Annoying error when trying to run SQL exported from db browser for sqlite

I am simply trying to run a basic SQL script to recreate a database.
The database was initially created in SQLite, and I exported it using DB Browser for SQLite.
The start of the file looks like this:
BEGIN TRANSACTION;
CREATE TABLE "AspNetUsers"
(
`Id` varchar(128) NOT NULL,
`Email` varchar(256) DEFAULT NULL,
`EmailConfirmed` tinyint(1) NOT NULL,
`PasswordHash` longtext,
`SecurityStamp` longtext,
`PhoneNumber` longtext,
`PhoneNumberConfirmed` tinyint(1) NOT NULL,
`TwoFactorEnabled` tinyint(1) NOT NULL,
`LockoutEndDateUtc` datetime DEFAULT NULL,
`LockoutEnabled` tinyint(1) NOT NULL,
`AccessFailedCount` int(11) NOT NULL,
`UserName` varchar(256) NOT NULL,
`IsActivated` tinyint(1) NOT NULL DEFAULT (0),
`Organisation` TEXT NOT NULL,
PRIMARY KEY(`Id`)
);
I created a new db and when running the query in SSMS I get this annoying error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '`'.
I tried deleting all the whitespace between the first ( and 'Id' but then I just get
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '`'.
I also tried replacing the `s with 's but with the same result....
I'm pretty sure the server I'm trying to execute this on is running SQL Server Express - not sure if that makes a difference
Why must life be so difficult?
The code is rather specific to SQLite in several respects:
The use of backticks is non-standard.
Having a length for integer columns in non-standard.
text and longtext are non-standard.
The equivalent create table statement in SQL Server would be:
CREATE TABLE AspNetUsers (
Id varchar(128) NOT NULL,
Email varchar(256) DEFAULT NULL,
EmailConfirmed tinyint NOT NULL,
PasswordHash varchar(max),
SecurityStamp varchar(max),
PhoneNumber varchar(max),
PhoneNumberConfirmed tinyint NOT NULL,
TwoFactorEnabled tinyint NOT NULL,
LockoutEndDateUtc datetime DEFAULT NULL,
LockoutEnabled tinyint NOT NULL,
AccessFailedCount int NOT NULL,
UserName varchar(256) NOT NULL,
IsActivated tinyint NOT NULL DEFAULT (0),
Organisation varchar(max) NOT NULL,
PRIMARY KEY (Id)
);
Except for the varchar(max), this would be pretty standard for any database.
Some notes:
You probably don't need varchar(max) for any of these fields. Although you can use it, it looks awkward to have a phone number that could occupy megabytes of data.
You could probably replace the tinyints with bits.
DEFAULT NULL is redundant.

Missing Right parenthesis error?

I'm trying to create a table in sqldeveloper however I keep getting a missing right parenthesis error when there are no missing right parenthises. Any fixes for this or am i just trying to create a table the wrong way?
CREATE TABLE Patient_T1(
PATIENT_ID INT(100) NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
DOB CHAR(10) NOT NULL,
P_STREET_ADRESS VARCHAR(50) NOT NULL,
PATIENT_CITY VARCHAR(30) NOT NULL,
PATIENT_STATE CHAR(2) NOT NULL,
PATIENT_ZIP CHAR(5) NOT NULL,
PATIENT_PHONE CHAR(12) NOT NULL,
PATIENT_ROOM INT(1000) NOT NULL,
CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID));
Not sure why Oracle gives that error message instead of something more helpful, but the cause is the precision applied to INT, switch from INT(100) and INT(1000) to just INT:
CREATE TABLE Patient_T1(
PATIENT_ID INT NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
DOB CHAR(10) NOT NULL,
P_STREET_ADRESS VARCHAR(50) NOT NULL,
PATIENT_CITY VARCHAR(30) NOT NULL,
PATIENT_STATE CHAR(2) NOT NULL,
PATIENT_ZIP CHAR(5) NOT NULL,
PATIENT_PHONE CHAR(12) NOT NULL,
PATIENT_ROOM INT NOT NULL,
CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID));
There are multiple issues with your table DDL:
INT(100) - In Oracle, an INTEGER is an ANSI SQL data type which refers to numeric values which have only an integer portion and no floating point or decimal part. That is, an INTEGER will only store whole numbers ONLY.
VARCHAR(20) - Oracle strongly recommends to use VARCHAR2.
From documentation,
VARCHAR Datatype
The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To
avoid possible changes in behavior, always use the VARCHAR2 datatype
to store variable-length character strings.
CHAR(10) - better use VARCHAR2 as CHAR is blank-padded to the fixed length. That's a wastage of storage.
From documentation,
CHAR Datatype
The CHAR datatype stores fixed-length character strings. If you give a
shorter value, then the value is blank-padded to the fixed length.
Only the issue# 1 would throw an error, anyway fixing all the above issues would let you create the table.
For example,
SQL> CREATE TABLE Patient_T1
2 (
3 PATIENT_ID NUMBER NOT NULL,
4 FIRST_NAME VARCHAR2(20) NOT NULL,
5 LAST_NAME VARCHAR2(30) NOT NULL,
6 DOB DATE NOT NULL,
7 P_STREET_ADRESS VARCHAR2(50) NOT NULL,
8 PATIENT_CITY VARCHAR2(30) NOT NULL,
9 PATIENT_STATE VARCHAR2(2) NOT NULL,
10 PATIENT_ZIP VARCHAR2(5) NOT NULL,
11 PATIENT_PHONE VARCHAR2(12) NOT NULL,
12 PATIENT_ROOM NUMBER NOT NULL,
13 CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID)
14 );
Table created.

Create Table - Time Statement

I am having trouble trying to create a table using MS Access.
I know for sure my problem lies in the "datetime" field but I can not figure what I am missing or doing wrong.
When I click "run" I get the
"Syntax Error in Field Definition"
Create Table Appointments
(DocID char(4) not null primary key,
PatID char(8) not null,
Day varchar(8) not null,
Time datetime not null,
Procedure varchar(50) null);
Time and procedure are reserved words, and therefore should be escaped:
Create Table Appointments
(DocID char(4) not null primary key,
PatID char(8) not null,
[Day] varchar(8) not null,
[Time] datetime not null,
[Procedure] varchar(50) null);
Or better yet, find names that aren't reserved words:
Create Table Appointments
(DocID char(4) not null primary key,
PatID char(8) not null,
AppointmentDay varchar(8) not null,
AppointmentTime datetime not null,
MedicalProcedure varchar(50) null);
Here Procedure and Time are reserved words and so need to be escaped using [] like below. See Documentation for more information
Create Table Appointments
(DocID char(4) not null primary key,
PatID char(8) not null,
[Day] varchar(8) not null,
[Time] datetime not null,
[Procedure] varchar(50) null);
As Time & Procedure are reserved keyword, so wrap Time & Procedure column in brackets [] or choose alternate names, if possible. See List of Keyword
[Time] and [Procedure]

MSG 2714 There is an object already named

I'm using sql server 2008 management studio. Im trying to create two seemingly simple tables but continue getting this error at execution. I have tried changing the names of these tables to many different variations. Nothing is helping. Here below is what I am typing.
USE POS410;
go
CREATE TABLE [empl]
(
employeeID INT NOT NULL,
lname VARCHAR(15) NOT NULL,
fname VARCHAR(15) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(15) NOT NULL,
state VARCHAR(10) NOT NULL,
areacode INT NOT NULL,
tnumber INT NOT NULL,
EEO1classification VARCHAR(10) NOT NULL,
hiredate DATE NOT NULL,
salary INT NOT NULL,
gender VARCHAR(1) NOT NULL,
age INT NOT NULL,
clocknumb INT NOT NULL,
PRIMARY KEY(employeeID),
UNIQUE(employeeID),
UNIQUE(clocknumb),
FOREIGN KEY(clocknumb) REFERENCES [jb_ttl](Empnum),
);
go
CREATE TABLE [jb_ttl]
(
EEO1 VARCHAR(10) NOT NULL,
JobTitle VARCHAR(15) NOT NULL,
JobDscrpt TEXT NOT NULL,
ExemptNonExemptStatus VARCHAR NOT NULL,
Empnum INT NOT NULL,
PRIMARY KEY (Empnum),
);
go
Here is the error report:
Msg 2714, Level 16, State 6, Line 2 There is already an object named 'empl' in the database. Msg 2714, Level 16, State 6, Line 2 There is already an object named 'jb_ttl' in the database.
I would say that you've tried to run this script more than once. It would have failed the first time because you're trying to create the first table with a foreign key to the second, which didn't exist yet. I use the word "didn't" because if my guess is correct, it does now, and that's why you're getting the object already exists error.
As a side note, SQL has supported wrapping DDL (creating/modifying objects) at least going back to 2005. I normally wrap table changes in transactions, and don't commit the transaction until everything goes through with no error messages.
Give this a run to confirm if jb_ttl already exists:
select * from master.dbo.sysobjects where [name] = 'jb_ttl'
That will show you if anb object named jb_ttl already exists. Could it already exist as a procedure or trigger perhaps?