fixed digits data type in sql - mysql

I want user to input only 11 digits , if not, an error should be occured on sql, what type of variable should I use in my sql code.
My current code :
st.executeUpdate("CREATE TABLE User(idUser numeric(11,0) not NULL,uName char(30), uSurname char (30), uAddress varChar(500), primary key(idUser) )");

If you want to make this kind of check on db side in MySQL you can use a trigger
CREATE TRIGGER tg_bi_user
BEFORE INSERT ON user
FOR EACH ROW
SET NEW.idUser = IF(NEW.idUser BETWEEN 10000000000
AND 99999999999, NEW.idUser, NULL);
The trick is to violate NOT NULL when NEW.idUser not in a proper range. If you are on MySQL 5.5 or higher you can also use SIGNAL.
Here is SQLFiddle demo. Try to uncomment the last insert statement and see it won't succeed.

If the variable is numeric, you can use a check constraint:
CREATE TABLE User(idUser numeric(11,0) not NULL,uName char(30), uSurname char (30),
uAddress varChar(500), primary key(idUser),
CHECK(idUser > 9999999999 AND idUser <= 99999999999) )'
Though you'll want to check if the datatype numeric accepts numbers large enough. If not you could to use varchar

Related

I need to create a table but i am getting error of invalid option or missing

CREATE TABLE deposit-1035(
Actno Varchar2(25),
Cname Varchar(25),
Bname Varchar2(25),
Amount Number(8,2),
Adate date);
ORA-00922: missing or invalid option
This is error i am getting why?
There're two problems here:
- is the subtraction operator. If you want to use it in an identifier you need to quote it:
CREATE TABLE "deposit-1035" (
Actno Varchar2(25),
Cname Varchar(25),
Bname Varchar2(25),
Amount Number(8,2),
Adate date
);
You are not using MySQL but Oracle database. The syntax and features are completely different, and so is the documentation.
You can't use minus sign in object name deposit-1035
try use an underscore
CREATE TABLE deposit_1035
or if you really need minus for mysql use backtics arount the object name `
`deposit-1035`
or for oracle use double quote
d "deposit-1035"

Converting INT to Unsigned INT

I have a large table in prod with INT datatype and it is both primary key and identity column(1,1).The number of rows is 2147479257.
During the daily job run the ETL failed because of arithmetic overflow error,as it cant fit any more rows in to the destination table.
Can you please how can i change the column to unsigned int.
Change the datatype to bigint.
bigint -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)
You've overflown an int, which means you have approximately 2B rows in the table. By switching to an unsigned int, you hope to gain another 2 billion rows of addressable space. The problem with this approach is that SQL Server does not support an unsigned int as a data type.
Your gut reaction might be to reach for Greg's approach of changing to a bigint data type. The challenge with this approach is that while your processing is dead in the water, you take the quick fix and change to bigint, any other consumer of that table is now going to fail. I went through this in 2011, by the way. We fixed the database only to have all the reporting and .NET applications fail. At that job, it'd have been far less catastrophic to have queued up the processing for a N days while we gave the appearance of normalcy than to remove all doubt by having every external facing application fail.
With the general implementation of an identity column, you can get an easy another 2B without making a single code change - simply set the identity value to the lower bound and you've bought yourself sufficient time to plan the migration to bigint. The command for this is dbcc checkident
You'll also likely want to ensure the identity column is specified as a unique value. People often set the identity column as a primary key but otherwise, you'd run code similar to the following.
SET NOCOUNT ON;
IF EXISTS
(
SELECT * FROM sys.tables AS T INNER JOIN sys.schemas AS S ON S.schema_id = T.schema_id WHERE S.name = 'dbo' AND T.name = 'IntOverfloweth'
)
BEGIN
DROP TABLE
dbo.IntOverfloweth;
END
CREATE TABLE
dbo.IntOverfloweth
(
IntOverflowethID int IDENTITY(2147483647,1) NOT NULL
, SomeValue varchar(30)
);
INSERT INTO
dbo.IntOverfloweth
(SomeValue)
OUTPUT
Inserted.*
VALUES
('Before');
BEGIN TRY
INSERT INTO
dbo.IntOverfloweth
(SomeValue)
OUTPUT
Inserted.*
VALUES
('Overflow');
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH
-- Push the pointer back around to the begining
DBCC CHECKIDENT('dbo.IntOverfloweth', RESEED, -2147483648);
-- Ensure uniqueness
CREATE UNIQUE INDEX UQ_IntOverfloweth
ON dbo.IntOverfloweth
(
IntOverflowethID
);
INSERT INTO
dbo.IntOverfloweth
(SomeValue)
OUTPUT
Inserted.*
VALUES
('Does not Overflow');

SQL server smallmoney data type error

CREATE TABLE GroovyExps_Tgt
(EMPNO SMALLINT,
FIRSTNAME VARCHAR(20) NOT NULL,
MIDINIT CHAR(1) NOT NULL,
LASTNAME VARCHAR(15) NOT NULL,
SALARY_INT INT,
SALARY_Decimal DECIMAL,
SALARY_Numeric NUMERIC,
SALARY_FLOAT FLOAT(9),
SALARY_MONEY MONEY,
SALARY_SMALLMONEY SMALLMONEY,
BIRTHDATE DATE,
HIREDATE_DATETIME DATETIME,
JOIN_TIME TIME,
JOINTIME DATETIME)
insert into GroovyExps_Tgt values(000080,'LEE','B','BRETT',11111111.11111111,
11111111.11111111,11111111.11111111,
11111111.11111111,11111111.11111111,
11111111.11111111,'1985-05-10',
'2014-04-22 20:25:48.002','20:25','2014-04-22 20:25:48.002')
Error:
Msg 8115, Level 16, State 4, Line 1 Arithmetic overflow error
converting numeric to data type smallmoney. The statement has been
terminated.
I need to insert exact data into table without any change in table definition!!!!
As the error message suggests, this error happens when converting a character or string value, whether of char, varchar, nchar or nvarchar data type, into a smallmoney data type and the character value has invalid characters that cannot be converted to smallmoney data type.
As per your insert statement 11111111.11111111 this value you are
using as a smallmoney value. Smallmoney datatype takes 4 bytes to
store the value in range of -214,748.3648 to +214,748.3647. Now you
see you are using large value for smallmoney that's why it's giving
you error.
The problem is that value 11,111,111 exceeds smallmoney's maximum allowed value which is between - 214,748.3648 and 214,748.3647. For more details you can refer to:
http://msdn.microsoft.com/en-us/library/ms179882.aspx
It is not possible to insert that value in that table without changing its definition, using the money data type yo can accomplish it... could you please tell me what you are trying to accomplish there, so I may help you better?

Trying to insert numeric info in database

I'm trying to insert a number in a field of my sql database. The table is:
Create table IF NOT EXISTS LJugador(
Fecha date,
vm int,
totalpuntos int,
nom_jugador varchar(80),
FOREIGN KEY (nom_jugador) REFERENCES Jugadores(nombre),
PRIMARY KEY(Fecha, nom_jugador)
)engine=innodb;
The PHP code for insert:
$ljuga_in = "INSERT INTO ljugador VALUE( now(), '{$jugadores[$m][2]}','{$jugadores[$m][3]}', '{$jugadores[$m][1]}' )";
when I do echo of $ljuga_in to see if it is being inserted correctly, I get:
INSERT INTO ljugador VALUE( now(), '4.130.000','92', 'Osman' );
You can see the number is being inserted correctly, but when I make a select query to see the information, VM has 4130 - the last three zeros are not there.
The field is int, but I have tried with numeric and double or float and nothing is working.
Try using DECIMAL(n, p) where n is the overall number of digits and p the number of digits after the comma, so e.g. for DECIMAL(10, 2) the maximum that can be represented would be 99.999.999,99

MySql insert statement to binary datatype?

I am using MySQL database.
I have one table having column with datatype binary(16).
I need help with the insert statement for this table.
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9e-efdf-b449');
Error : Lookup Error - MySQL Database Error: Data too long for column 'distid' at row 1
How to resolve this issue?
You should remove the hyphens to make the value match the length of the field...
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9eefdfb449');
Also, MySQL standard is to use this notation to denote the string as binary... X'9fad5e9eefdfb449', i.e.
insert into assignedresource values (X'9fad5e9eefdfb449');
Well, assuming that you want to strictly insert a hexadecimal string, first you need to remove the dashes and then "unhex" your string before inserting it into a binary(16) data type column, the code would go like this:
INSERT INTO `assignedresource` VALUES(UNHEX(REPLACE('9fad5e9e-efdf-b449','-','')));
Also... the "usable" data you are inserting is actually 8 bytes after undashing it, so binary(8) would do fine if you plan on not storing the dashes.
You can strip the hyphens and perpend 0x to the value unquoted, like this:
insert into assignedresource values (0x9fad5e9eefdfb449);
As well as, as this (mentioned in other answers):
insert into assignedresource values (X'9fad5e9eefdfb449');
Both are valid notation for a hexadecimal literal.
Your string is 18 char long, change the database
CREATE TABLE `assignedresource` (
`distid` binary(18) NOT NULL
)