Initially, the table "MyTable" has been defined in the following way:
CREATE TABLE IF NOT EXISTS `MyTable` (
`Col1` smallint(6) NOT NULL AUTO_INCREMENT,
`Col2` smallint(6) DEFAULT NULL,
`Col3` varchar(20) NOT NULL,
);
How to update it in such a way that the column "Col 3" would be allowed to be NULL?
The following MySQL statement should modify your column to accept NULLs.
ALTER TABLE `MyTable`
ALTER COLUMN `Col3` varchar(20) DEFAULT NULL
ALTER TABLE MyTable MODIFY Col3 varchar(20) NULL;
This works in PSQL, not sure if it also works in normal SQL.
ALTER TABLE tablename
ALTER COLUMN columnname DROP NOT NULL;
ALTER TABLE school MODIFY COLUMN school_van varchar(36) DEFAULT NULL;
"ALTER" keyword didn't work but "MODIFY" worked fine in MySQL 8.0.26
Related
I want to alter a field to nullable and add the default value null.table name 'other_details' column name used_asset which is varchar(100)
ALTER TABLE `other_details` ALTER COLUMN `used_asset` varchar(100) DEFAULT NULL
above query shows error 'syntax error near varchar(100) DEFAULT NULL'
This came from phpmyadmin as preview SQL and it seems to work:
ALTER TABLE `other_details` CHANGE `used_asset` `used_asset` VARCHAR(100) NULL DEFAULT NULL;
Try this
1) Using MODIFY
ALTER TABLE `other_details` MODIFY `used_asset` varchar(100) null;
2) Using CHANGE
Syntax
ALTER TABLE table_name CHANGE column_name column_name type DEFAULT NULL
Example
ALTER TABLE `other_details` CHANGE `used_asset` `used_asset` varchar(100) DEFAULT NULL;
ALTER TABLE Table_Name MODIFY Column_Name DataType DEFAULT NULL;
I have a MYSQL table, with 5 columns in it:
id bigint
name varchar
description varchar
slug
Can I get MySQL to automatically generate the value of slug as a 256 Bit Hash of name+description?
I am now using PHP to generate an SHA256 value of the slug prior to saving it.
Edit:
By automatic, I mean see if it's possible to change the default value of the slug field, to be a computed field that's the sha256 of name+description.
I already know how to create it as part of an insert operation.
MySQL 5.7 supports generated columns so you can define an expression, and it will be updated automatically for every row you insert or update.
CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug varchar(64) AS (SHA2(CONCAT(name, description), 256)) STORED NOT NULL,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
If you use an earlier version of MySQL, you could do this with TRIGGERs:
CREATE TRIGGER MySlugIns BEFORE INSERT ON MyTable
FOR EACH ROW SET slug = SHA2(CONCAT(name, description));
CREATE TRIGGER MySlugUpd BEFORE UPDATE ON MyTable
FOR EACH ROW SET slug = SHA2(CONCAT(name, description), 256);
Beware that concat returns NULL if any one column in the input is NULL. So, to hash in a null-safe way, use concat_ws. For example:
select md5(concat_ws('', col_1, .. , col_n));
Use MySQL's CONCAT() to combine the two values and SHA2() to generate a 256 bit hash.
CREATE TABLE IF NOT EXISTS `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`description` varchar(50) NOT NULL,
`slug` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `mytable` (`name`,`description`,`slug`)
VALUES ('Fred','A Person',SHA2(CONCAT(`name`,`description`),256));
SELECT * FROM `mytable`
OUTPUT:
COLUMN VALUE
id 1
name Fred
description A Person
slug ea76b5b09b0e004781b569f88fc8434fe25ae3ad17807904cfb975a3be71bd89
Try it on SQLfiddle.
How can i make a table field with default value "none" and null value "no"?
When i create a table with following code it always add default value NULL and null to yes
ALTER TABLE `table_name` ADD `test` INT(11) AFTER `test1`
ALTER TABLE `table_name` ADD `test` INT NOT NULL AFTER `test1`;
ALTER TABLE myTable
ALTER column SET DEFAULT 'none'.
For replacing NULL with NO you don't have to alter the table just do it in your query :
SELECT COALESCE(columnWhichisNull,'no'))
FROM myTable ;
COALESCE checks if a value from a column is null and replaces it with your desired character
I am trying to add multiple columns to an existing table in phpMyAdmin, but I keep getting the same error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax ...
I am writing:
ALTER TABLE `WeatherCenter`
ADD COLUMN
BarometricPressure SMALLINT NOT NULL,
CloudType VARCHAR(70) NOT NULL,
WhenLikelyToRain VARCHAR(30) NOT NULL;
I have referred to past posts on StackOverflow, and I am following the experts' recommendation, so why am I getting an error?
ALTER TABLE table_name
ADD COLUMN column_name datatype
correct syntax
ALTER TABLE `WeatherCenter`
ADD COLUMN BarometricPressure SMALLINT NOT NULL,
ADD COLUMN CloudType VARCHAR(70) NOT NULL,
ADD COLUMN WhenLikelyToRain VARCHAR(30) NOT NULL;
check syntax
You need to specify multiple ADD COLUMN
ALTER TABLE `WeatherCenter`
ADD COLUMN BarometricPressure SMALLINT NOT NULL,
ADD COLUMN CloudType VARCHAR(70) NOT NULL,
ADD COLUMN WhenLikelyToRain VARCHAR(30) NOT NULL;
You can alter a table and add multiple columns in one statement by doing it like this.
alter table WeatherCenter add column (BarometricPressure SMALLINT NOT NULL, CloudType VARCHAR(70) NOT NULL, WhenLikelyToRain VARCHAR(30) NOT NULL);
This will help you:
alter table A add first_name varchar(10),last_name varchar(10);
As you're adding columns to an existing table I don't think you're meant to declare NOT NULL in the statement. Also, you don't need to use ADD COLUMN, you can just use ADD.
ALTER TABLE WeatherCentre
ADD BarometricPressure SMALLINT,
ADD CloudType VARCHAR(70),
ADD WhenLikelyToRain VARCHAR(30);
This is from Official MySQL Documentation
ALTER TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]
alter_specification:
table_options
| ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| ADD [COLUMN] (col_name column_definition,...)
Possible duplicate of alter table add MULTIPLE columns AFTER column1
alter table table_name add (product varchar(20) not null, price int(10))
this is also working fine
I create my borrowed table that keeps which users borrowed which books records of a library application.
I forgot to set not null When i create userID and bookID fields,
how i can add this feature ti this two fields?
I try this but failed:
Alter table borrowed set not null (userID,bookID);
In mysql you can add a costraint in an existing column with the command MODIFY:
ALTER TABLE borrowed MODIFY userID INT(11) NOT NULL;
ALTER TABLE borrowed MODIFY bookID INT(11) NOT NULL;
You can do so by using the CHANGE syntax separated by comma for multiple columns
ALTER TABLE `borrowed`
CHANGE `userID` `userID` INT(11) NOT NULL,
CHANGE `bookID` `bookID` INT(11) NOT NULL;