This question already has answers here:
NULL value in multi-column primary key
(5 answers)
Closed 4 years ago.
I have a requirement wherein I need to change the table structure as per the production environment on a lower environment. The table has a multi-column PRIMARY KEY as (md_biobjectid,projectid,md_mapid), I want to modify column 'md_mapid' to varchar(50) DEFAULT NULL from 'md_mapid' varchar(50) NOT NULL.
When I am running the query :
alter table table_name
modify column md_mapid varchar(50) DEFAULT NULL; it doesn't run and I am getting following error :
Error Code: 1171. All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead.
Other columns structure on both the environment is :
'md_biobjectid' varchar(50) NOT NULL DEFAULT ''
'projectid' varchar(50) NOT NULL DEFAULT ''
MySQL version : 5.7.21-log.
you need to isse without DEFAULT NULL postfix. md_mapid column is part of composite primary key and cannot be set to null.
alter table table_name modify column md_mapid varchar(50)
Related
This question already has answers here:
How do I create a sequence in MySQL?
(6 answers)
Closed 2 months ago.
getting an syntax error in mysql
CREATE SEQUENCE users_id_seq;
CREATE TABLE users (
id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL ('users_id_seq'),
name VARCHAR(50) NOT NULL,
active BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
create a table with id sequence but getting syntax error
The errors you're getting are due to the CREATE SEQUENCE statement, which is Oracle syntax for generating auto-incremental values.
In MySQL you can use the AUTO_INCREMENT keyword inside the table creation statement as follows:
CREATE TABLE users (
id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
active BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
Check the demo here.
This question already has answers here:
How to set initial value and auto increment in MySQL?
(10 answers)
Closed 1 year ago.
Am trying to create a MySQL that tells
auto_increment
Where and how to start incrementing. Each time I run the code, it always tells me I have error near
auto.
Each time I removed the "=", it always work.
This is the code
CREATE TABLE staff(
id into(11) not null primary key auto_increment=001,
Names varchar(109) not null
);
What am I doing wrong
First create table like below:
CREATE TABLE staff( id into(11) not null primary key
auto_increment, Names varchar(109) not null );
then alter to customized auto increment.
ALTER TABLE staff AUTO_INCREMENT=001;
Schema (MySQL v5.7)
CREATE TABLE staff(
id int not null primary key auto_increment,
Names varchar(255) not null
)auto_increment=001;
View on DB Fiddle
The default auto-increment value is an option on the table not on the column (perhaps counterintuitively, but a table is only allowed to have one such column).
The syntax looks like:
CREATE TABLE staff (
id int not null primary key auto_increment,
Names varchar(109) not null
) auto_increment = 10;
Here is a db<>fiddle.
I have the following column
`Equipamento_Solucao_id` VARCHAR(32) NOT NULL,
I would like it to be
`Equipamento_Solucao_id` VARCHAR(32) DEFAULT NULL,
How can I do this without changing my database model, that is, with a sql query?
You would use an alter table statement. A typical method would be:
alter table t alter column Equipamento_Solucao_id VARCHAR(32) DEFAULT NULL;
You could also look through the system tables on your database, find the not-null constraint, and then drop it specifically.
I am trying to add a column to a table.To do so I am trying
ALTER TABLE requirements Modify COLUMN parent_id int(11);
but when I try to execute this query mysql does not respond for long.So each time I have to kill the query.
I have created the table using
CREATE TABLE requirements (requirement_id smallint(6) NOT NULL AUTO_INCREMENT,
product_id smallint(6) NOT NULL,
name varchar(255) CHARACTERSET latin1 NOT NULL DEFAULT '',
PRIMARY KEY (requirement_id),
UNIQUE KEY requirement_product_id_name_idx (product_id,name),
UNIQUE KEY requirement_product_idx (requirement_id,product_id),
KEY requirement_name_idx_v2 (name) )
ENGINE=InnoDB
AUTO_INCREMENT=7365
DEFAULT CHARSET=utf8;
Please help me know why I am not able to execute the Alter table query.I am new to database is there something wrong with my alter table query.
According to your table defintion parent_id seems to be a new column which you want to add so your query should be to add the column not modify.
Try this:
alter table requirements add column parent_id int(11);
SQL FIDDLE DEMO
On a side note:
There needs to be a space between CHARACTERSET here
name varchar(255) CHARACTERSET latin1 NOT NULL DEFAULT '',
should be
name varchar(255) CHARACTER SET latin1 NOT NULL DEFAULT '',
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;