sql/sybase varchar length - sql

so i was trying to make a code for a document. So if i put 01234567890 and 1234567890 on a table, they should stay there. But when i put 12345667890 and then look the table, that number is different like: 1234566789 (the zero is gone!)
Plus the length must be 11.
If you can help me/understand me, thank you.
create table DOCTORS(
fullname varchar(35) not null,
document varchar(11) not null,
salary bigint null,
homeaddress varchar(35) null,
phone bigint not null,
cellphone bigint not null,
speciality varchar(20) not null,
birthdate date not null,
gradedate date not null,
workingdate date not null,
constraint pk primary key (document) and check(document between '0' and '99999999999'),
constraint validCellphone check (cellphone between 3000000000 and 3029999999 or cellphone between 3100000000 and 3129999999 or cellphone between 3150000000 and 3169999999),
constraint validSpeciality check (speciality in('Medicina general','Ginecologia','Traumatologia','Pediatria')),
constraint validDates check (birthdate < gradedate and birthdate < workingdate and gradedate< getdate() and workingdate< getdate() and gradedate<workingdate),
constraint antiquity check (datediff(dd, gradedate, getdate() )>1461),
constraint validPhone check(phone between 1000000 and 9999999)
);
GO>

If you tried INT previously,
01234567890
is not a number. It's a string.
1234567890
is the number.
SQL is able to recognize that those extra zeros are not needed to define the number, so it ignores them. If you want to dispaly a value as 01234567890 to the application, I'd suggest doing the formatting on the application side. That way the number stored in SQL is still a number if you want do addition, aggregation, or other calculations. If you really have to store it as '01234567890', you need to convert it to a character field; char, varchar, nvarchar, nchar.

Related

ORA-01858 FOR BEGINNER

CREATE TABLE Pizza
(
pizza_id DECIMAL(12) NOT NULL PRIMARY KEY,
name VARCHAR(32) NOT NULL,
date_available DATE NOT NULL,
price DECIMAL(4,2) NOT NULL
);
CREATE TABLE Topping
(
topping_id DECIMAL(12) NOT NULL,
topping_name VARCHAR(64) NOT NULL,
pizza_id DECIMAL(12)
);
ALTER TABLE Topping
ADD CONSTRAINT topping_pk PRIMARY KEY(topping_id);
ALTER TABLE Topping
ADD CONSTRAINT Topping_pizza_fk
FOREIGN KEY(pizza_id) REFERENCES Pizza(pizza_id);
INSERT INTO pizza (pizza_id, name, date_available, price)
VALUES (1, 'Plain', CAST('27-Feb-2021' AS DATE), 6);
Error:
ORA-01858: a non-numeric character was found where a numeric was expected
I cannot figure out which part is wrong, I'm just a beginner for SQL, it seems related with date, can someone help me?
This works for me: SQL Fiddle. Don't use CAST to convert strings to dates. That's the only thing that looks off about your example. It may be using a different default date format than your string. Instead use TO_DATE( '27-Feb-2021', 'DD-Mon-YYYY') which converts a string to a date, or DATE '2021-02-27', which is a date literal and only takes the yyyy-mm-dd format.
Additionally, I'd suggest using NUMBER instead of DECIMAL just because it's more standard in the Oracle world. And always use VARCHAR2 instead of VARCHAR, which is officially discouraged.

Using like for numeric in a constraint

I am creating a table which is like this:
CREATE TABLE Peeps
(
Name VARCHAR(255) NOT NULL,
PhoneNum BIGINT NOT NULL,
CONSTRAINT PhoneNum_Check CHECK (PhoneNum LIKE '08%')
)
Every phone number has to start with 08. However when I tried insert there's an error because LIKE can't be used for numeric (or that's what my friend said). The alternative would be using VARCHAR for PhoneNum, but this is an assignment and we have to use numeric for the phone number.
If a phone number can start with a 0 then you need to use a string:
CREATE TABLE Peeps (
Name VARCHAR(255) NOT NULL,
PhoneNum VARCHAR(255) NOT NULL,
CONSTRAINT PhoneNum_Check CHECK (PhoneNum LIKE '08%')
);
Although you can use LIKE on a number, it is highly not recommended. What happens is that the number is converted to a string. However, that string will never start with a 0 -- well, at least never when the value is greater than 1.

How to prevent a input of certain letters using Oracle

The code is the category of the video, it is represented by one upper case character, excluding I, O,
Q, V, Y and Z, followed by a numeric character.
So far, I took a guess and got this. Any suggestions on how to fix it?
create table channelTable (
channelID number NOT NULL,
ChannelName varchar(100) NOT NULL,
ChannelDate date NOT NULL,
UserName varchar(100) NOT NULL UNIQUE,
TopicCode varchar(4) NOT NULL);
CONSTRAINT channelID_pk PRIMARY KEY (channelID)
CONSTRAINT c_topicCode LIKE '[A-Za-z][0-9] NOT (I,O,Q,N,Y,Z)
);
Some comments:
NOT NULL is not needed for PRIMARY KEY columns.
In Oracle, use VARCHAR2().
Then, I would suggests regular expressions. If the value is supposed to be exactly two characters, then declare it as such:
create table channelTable (
channelID number,
ChannelName varchar(100) NOT NULL,
ChannelDate date NOT NULL,
UserName varchar2(100) NOT NULL UNIQUE,
TopicCode char(2) NOT NULL;
CONSTRAINT channelID_pk PRIMARY KEY (channelID)
CONSTRAINT check (REGEXP_LIKE(c_topicCode, '^[A-HJ-NPR-UYZ][0-9]$')
);
Or perhaps more simply:
CONSTRAINT REGEXP_LIKE(c_topicCode, '^[A-Z][0-9]$') AND NOT REGEXP_LIKE(c_topicCode, '^[IOQNYZ]'))
All that said, I would rather see a table of TopicCodes that is populated with the correct values. Then you can just use a foreign key relationship to define the appropriate codes.
Use the regular expression ^[A-HJ-MPR-X]\d$ to match an upper-case character excluding I,O,Q,N,Y,Z followed by a digit:
CREATE TABLE channels (
id number CONSTRAINT channel__id__pk PRIMARY KEY,
Name varchar(100) CONSTRAINT channel__name__nn NOT NULL,
DateTime date CONSTRAINT channel__date__nn NOT NULL,
UserName varchar(100) CONSTRAINT channel__username__NN NOT NULL
CONSTRAINT channel__username__U UNIQUE,
TopicCode varchar(4),
CONSTRAINT channel__topiccode__chk CHECK ( REGEXP_LIKE( topiccode, '^[A-HJ-MPR-X]\d$' ) )
);
db<>fiddle
Also, you don't need to call the table channeltable just call it channels and you don't need to prefix the column names with the table name and you can name all the constraints (rather than relying on system generated constraint names which makes it much harder to track down issues when you are debugging).
Consider the following check constrait:
create table channelTable (
...
topicCode varchar(4) not null
check(
substr(c_topicCode, 1, 1) not in ('I', 'O', 'Q', 'V', 'Y', 'Z')
and regexp_like(topicCode, '^[A-Z]\d')
),
...
);
The first condition ensures that the code does not start with one of the forbidden characters, the second valides that it stats with an upper alphabetic character, followed by a number.
To avoid using two conditions, an alternative would be to list all allowed characters in the first position:
check(regexp_like(topicCode, '^[ABCDEFGHJKLMNPRSTUVWX]\d'))
This works in Oracle, and in very recent versions of MySQL.

Literal does not match format string when creating a table

I keep trying to create a basic table. Every time I try to create the table I get the error
literal does not match format string
I'm trying to limit the years of the tournament between 2005 and 2100. The error is between DATE '2005'.
This is my code:
Create table Tournament_T
(Tournament_name VARCHAR (50) PRIMARY KEY NOT NULL,
Tournament_year INTEGER NOT NULL,
CONSTRAINT RANGE CHECK (Tournament_year BETWEEN DATE '2005' AND '2100'),
Tournament_rules CLOB NOT NULL,
Tournament_fee VARCHAR(1000) NOT NULL,
Tournment_eligibility VARCHAR (1000) NOT NULL );
COMMIT;
Your constraint is using the date keyword, but you don't need it. Just do:
CONSTRAINT RANGE CHECK (Tournament_year BETWEEN 2005 AND 2100),
Your column is an integer, not a date.

ORA-001722 Invalid Number when insert in Oracle SQL

CREATE TABLE the_user( Name VARCHAR(40) not null,
Address VARCHAR(255) not null,
Delivery_address VARCHAR(255),
Email VARCHAR(25) not null,
Phone INTEGER not null,
Status INTEGER not null,
Password VARCHAR(25) not null,
DOB DATE not null,
PRIMARY KEY (Email),
FOREIGN KEY (Status) REFERENCES User_Status (Status_Id),
CONSTRAINT check_Password CHECK (Password > 4)
);
INSERT INTO the_user VALUES (
'Pergrin Took',
'12 Bag end, hobbiton, The Shire, Eriador',
'The address, Dublin',
'ptook#lotr.com',
'8679046',
'001',
'treebeard',
TO_DATE('2013/11/04 14:11:34', 'yyyy/mm/dd hh24:mi:ss')
);
I have the above database in Oracle but when I try to run the insert command I get an ORA-1722 error, Invalid Number. There is a entry in the user_status table which corresponds to the 1 in the insert.
I have been stuck on this for days.
Quotes are not a problem - it will be converted implicitly to numbers as far as they are valid.
Check your constraints:
CONSTRAINT check_Password CHECK (Password > 4)
Here you try to compare string and number -> in this comparison Oracle always tries to cast both as numbers -> password fails and you see an error.
Try to use instead of password e.g. '55' and you will see the row is inserted.
Perhaps you wanted to do this?
CONSTRAINT check_Password CHECK (length(Password) > 4)