Set not null attribute to a existing column - mysql

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;

Related

Auto Increment missing in my users table after importing on production. How should I modify it?

When I run this query :
ALTER TABLE `users` MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=56;
It gives me error like :
#1833 - Cannot change column 'id': used in a foreign key constraint
'designation_user_user_id_foreign' of table
'databasename.designation_user'
Found a solution that worked!
I made id column in users table unique using more option in action column of users table and then executed below query :
ALTER TABLE `users` MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

Change the field with UUID_Short failed?

I'm trying to alter current id field in organization table to UUID_SHORT but failed?
ALTER TABLE `organization` CHANGE `id` `id` BIGINT(16) UNSIGNED
NOT NULL DEFAULT uuid_short();
I don't see any error message?!
I don't think you can set the default value for id like this
Rather you can create a trigger to do this:
CREATE TRIGGER before_insert_organization
BEFORE INSERT ON organization
FOR EACH ROW
SET new.id = uuid_short();

How to change mysql to postgres

as in title any one can help me with conversion from mysql to postgres
ALTER TABLE tbl_roles MODIFY 'roleId' tinyint(4) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=4;
and the second one
ALTER TABLE tbl_users MODIFY 'userId' int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=20;
with error syntax error at near "MODIFY"
i did in a different way
CREATE TABLE IF NOT EXISTS tbl_roles (
roleId tinyint(4) NOT NULL PRIMARY KEY AUTO_INCREMENT,AUTO_INCREMENT=4
);
also in table in second table
CREATE TABLE IF NOT EXISTS tbl_users (
userId int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=20,
);
and there is no errors

Copy/duplicate SQL row with blob/text, How do that?

I would like to copy a SQL's row into the same table.
But in my table, I've a 'text' column.
With this SQL:
CREATE TEMPORARY TABLE produit2 ENGINE=MEMORY SELECT * FROM produit WHERE pdt_ID = 'IPSUMS';
UPDATE produit2 SET pdt_ID='ID_TEMP';
INSERT INTO produit SELECT * FROM produit2;
DROP TABLE produit2;
I get this error :
#1163 - The used table type doesn't support BLOB/TEXT columns
Here is my table :
pdt_ID varchar(6)
pdt_nom varchar(130)
pdt_stitre varchar(255)
pdt_accroche varchar(255)
pdt_desc text
pdt_img varchar(25)
pdt_pdf varchar(10)
pdt_garantie varchar(80)
edit_ID varchar(7)
scat_ID int(11)
pdt_asso1 char(3)
pdt_asso2 char(3)
pdt_online tinyint(4)
It's possible to help me to duplicate row ? How?
You can't store TEXT-columns (which really are blobs) in memory tables. See here
Depending on your ultimate goal, you may insert a md5-hash of the TEXT-column instead to preserve entity identity. Otherwise you need to put pdt_desc and such into another table and refer to it's primary key - that will save you some storage/memory too.

Alter SQL table - allow NULL column value

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