I have the next code of an sql query, but i need to adapt it to mysql query, but it gives me an error the the value "number", and i don't know how to change it correctly, if someone knows how can this code work i will appreciate you to answer
Create table SalarioBase(
IdSalario number constraint pk_salariobase primary key,
Salario number)
Change your code to this
Create table SalarioBase(
IdSalario int constraint pk_salariobase primary key,
Salario int)
You can change int to float ... etc. if you need.
Follows T-SQL92,number is not a basic data type
As far as I know, also your code does not work on ms-sql
you may try:
CREATE TABLE `SalarioBase` (
`IdSalario` INT NOT NULL COMMENT '',
`Salario` INT NULL COMMENT '',
PRIMARY KEY (`IdSalario`) COMMENT '');
Related
I tried to setup a table in my database:
CREATE TABLE products(
id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
info TEXT,
price DOUBLE(10)
);
and got the error #1064, but I think there is no error in my SQL syntax.
This is due to Wrong Datatype specification while creating Table.
You can use FLOAT(10) if required, if you want to use Double, you have to mention the precision value as DOUBLE(10,0).
Can anyone please help me with writing a query to add constraint on BestFigure column with "%/%" i.e. it should be 3/10 format.
Please refer the image.
create table Player(
Player_No int Identity(1,1) Primary Key, Player_Name Varchar(20) Not null,
Category Varchar(20) check (Category='batsman' or Category='bowler' or Category='Allrounder'),
BestFigure Varchar(10) check (Bestfigure like'%/%'))
I think a better solution is to store the two figures separately and then combine them:
BestFigure_left int,
BestFigure_right int,
BestFigure varchar(10) generated always as (concat(BestFigure_Left, '/', BestFigure_Right))
MySQL does not actually enforce check constraints (which is why a trigger is needed). If it did, you would do:
BestFigure Varchar(10) check (Bestfigure regexp '^[0-9]{1,3}/[0-9]{1,6}$')
Or something like that. I am unclear what the "3" and "10" mean in your description.
I have been testing a database i am doing right now and i am noticing that it is letting me insert null values into fields that are part of a primary key, despite stating in the script that the value of the field should be NOT NULL. I am using MAC's MySQL Workbench, and I have been googling around and can't figure out why this is happening. (Maybe I am too brain-fried right now... I am even starting to doubt myself)
Part of the script of the database creation (these are the tables I have tested..):
DROP DATABASE IF EXISTS solytierra ;
CREATE DATABASE IF NOT EXISTS solytierra DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE solytierra ;
DROP TABLE IF EXISTS solytierra.Cliente ;
CREATE TABLE IF NOT EXISTS solytierra.Cliente (
CIF VARCHAR(25) NOT NULL,
Nombre VARCHAR(100) NULL,
EmailGeneral VARCHAR(45) NULL,
Web VARCHAR(45) NULL,
Notas VARCHAR(150) NULL,
insertado Timestamp,
CONSTRAINT pk_Cliente PRIMARY KEY (CIF)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS solytierra.PersonaContacto ;
CREATE TABLE IF NOT EXISTS solytierra.PersonaContacto (
Cliente_CIF VARCHAR(25) NOT NULL,
Nombre VARCHAR(50) NOT NULL,
Apellidos VARCHAR(100) NOT NULL,
Notas VARCHAR(150) NULL,
CONSTRAINT pk_PersonaContacto PRIMARY KEY (Cliente_CIF , Nombre , Apellidos),
CONSTRAINT fk_PersonaContacto_Cliente FOREIGN KEY (Cliente_CIF)
REFERENCES solytierra.Cliente (CIF)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
...
It will let me create Clients without CIF, "PersonaContacto" without Cliente_CIF or without "Nombre"....
I have also tested other databases that i already had that used to work and it is happening the same in an all them.
Got it!!
I don't know what sql mode i was running on by default, but with this:
SET sql_mode = TRADITIONAL;
It is now running perfectly! I didn't know that there were different sql modes! Thanks a lot to everyone for your time and efforts! It really helped me to see that the problem was in my workbench, not the code and look for the answer accordingly! I hope this thread will be useful for future beginners like me!
If the value being stored in the column CIF is actually a NULL, then the expression LENGTH(CIF) should also return NULL. (If it's a zero length string, then LENGTH(CIF) will return 0.
To verify:
SELECT c.CIF, LENGTH(c.CIF) FROM solytierra.Cliente c ;
SELECT c.CIF FROM solytierra.Cliente c WHERE c.CIF IS NULL;
If you are running an INSERT statement, I can't explain the behavior you are observing, either MySQL allowing a NULL value to be stored or MySQL providing an implicit default value.)
If it's a zero length string being stored, that's the behavior we would expect if the columns were not explicitly declared to be NOT NULL but were later declared to part of the primary key. It's also the behavior we'd expect if the column were defined NOT NULL DEFAULT ''.
When the NOT NULL is omitted from the column declaration and the column is later declared to be part of the PRIMARY KEY, MySQL will use an an implicit default value based on the datatype of the column (zero length string for VARCHAR, zero for an integer, etc.)
But I'm not able to reproduce the problem you report, with the table definitions you've posted.
I recommend you check the table definition by getting the output from:
SHOW CREATE TABLE solytierra.Cliente;
I don't seem to be able to get my SELECT statement to work.
This is the table:
CREATE TABLE clients(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR (70) NOT NULL,
mobile_number int(11) NOT NULL,
UNIQUE KEY (email)
);
The Select Query
SELECT user_id FROM clients WHERE email='info#candy.co.uk';
Whenever I try using this SELECT statement from mysqlADMIN it returns null; this happens even when I enter an email address that I know is in the database.
I would really appreciate some advice on where I am going wrong.
Try the statement without the "WHERE" clause. If it returns the entire table you have narrowed it down to an error in your "email" string.
If it returns nothing and you know there is data in this table then check your connection string and make sure you are using the correct DB.
I think there is some error in your SQL create statement. You should make the unique key to which you have applied auto increment. In this case the database will give an error.
Please try the following creation statement
CREATE TABLE clients(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR (70) NOT NULL,
mobile_number int(11) NOT NULL
);
I am using a VARCHAR as my primary key. I want to auto increment it (base 62, lower/upper case, numbers), However, the below code fails (for obvious reasons):
CREATE TABLE IF NOT EXISTS `campaign` (
`account_id` BIGINT(20) NOT NULL,
`type` SMALLINT(5) NOT NULL,
`id` VARCHAR(16) NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
however, this works:
CREATE TABLE IF NOT EXISTS `campaign` (
`account_id` BIGINT(20) NOT NULL,
`type` SMALLINT(5) NOT NULL,
`id` VARCHAR(16) NOT NULL PRIMARY KEY
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
What is the best way to keep track of incrementation of 'id' myself? (Since auto_increment doesn't work). Do i need to make another table that contains the current iteration of ID? Or is there a better way to do this?
EDIT: I want to clarify that I know that using INT is a auto_increment primary key is the logical way to go. This question is in response to some previous dialogue I saw. Thanks
you have to use an INT field
and translate it to whatever format you want at select time
example of a solution to your problem:
create a file with a unique number and then increment with a function.
the filename can be the prefix and the file binary content represent a number.
when you need a new id to the reg invoque the function
Example
String generateID(string A_PREFIX){
int id_value = parsetoInt(readFile(A_PREFIX).getLine())
int return_id_value = id_value++
return return_id_value
}
where "A_PREFIX-" is the file name wich you use to generate the id for the field.
Or just create a sequence and maintain the pk field using the sequence to generate the primary key value with nextval function. And if perf is an issue, use cache on sequence.
But as others have stated, this is sub-optimal, if your primary key contains a numbered sequence then it's better to use int and auto-increment.
I don't see a use case where pk has to auto-increment but be a varchar data type, it doesn't make sense.
Assuming that for reasons external to the database, you do need that varchar column, and it needs to autoIncrement, then how about creating a trigger that grabs the existing autoIncrement value and uses Convert() to convert that value into a VarChar, dropping the VarChar into the field of interest. As mentioned in a previous answer, you could concatenate the table-name with the new varChar value, if there is some advantage to that.