Disallow Nulls in mysql - mysql

I am creating a database that has a table called tblOne
I added a the column 'ID' and i forgot to disallow Nulls.
I am trying to change that with the query
ALTER TABLE tblOne
ALTER COLUMN ID int NOT NULL;
I am seeing the following error:
Cannot insert the value NULL into column 'ID', table 'city.dbo.tblOne'; column does not allow nulls. UPDATE fails.
Ive used this query before. So I'm sure there is some small mistake I'm not seeing.
The statement has been terminated.

Cannot insert the value NULL into column 'ID', table 'city.dbo.tblOne'; column does not allow nulls. UPDATE fails.
By the sounds of it the table already contains data. With that column containing NULL's already. If you try to change the column to not allow nulls, but there is already a null value in, then the modification will fail. First change the data in that column so that it is not null, and then run your ALTER statement again

Related

MySQL: update column if it exists else do nothing?

How to do the following:
if table has column 'name' - update 'name', else - do nothing?
My working sql in a transaction is
UPDATE tmp set name = NULL
tmp table kees the record to update and turning name into NULL is required for duplicate procedure. However, some tables do not have 'name' field.
So, is it possible to update column to null if exists, otherwise, just do nothing?
As far as I know it is not possible. Moreover I would recommend never make any modifications on a number of tables at once. In general each table is unique and setting fields to NULL basing on just a field name could lead to loss of important data.

adding computed column in destination table from temporary table

I have a stored procedure in which the data is populated into the destination table using a temporary table(in this i perform all the computation).
the problem i am facing is i have two columns which are getting its value by concatenating two other columns.
i.e.
concat(status_code|status_reason) as STATUS
concat(prior_status_code|prior_status_reason) as PRIOR_STATUS
Now this concatenation i have done it in temporary table.
i have to load its data into a destination table and the destination table doesn't have the columns STATUS and PRIOR_STATUS.
for loading the data i am using
insert into Destination( column_names,Status,prior_status)
select (column_names) from temporary table
so when i execute this part it throws me an error that columns are invalid as not present in the table.
P.S. I have used alter command to add those two columns in the destination. Still not getting the desired result.
Is there any other way to do this and where am i going wrong in this?
please help.
Thanks in advance
try this to add you field. it is important that you put the fieldnames in Backquotes. status is a reserved word.
ALTER TABLE youTable
ADD COLUMN `status` VARCHAR(100),
ADD COLUMN `PRIOR_STATUS` VARCHAR(100);
you can also specify the position of the field in the table like:
ALTER TABLE youTable
ADD COLUMN `status` VARCHAR(100) AFTER fieldxx,
ADD COLUMN `PRIOR_STATUS` VARCHAR(100) BEFORE fieldzz;

Mysql ,how to add two new columns and insert data at the same time?

It's my code
alter table num_of_people add column date_pre varchar(40);
alter table num_of_people add column time_pre varchar(40);
The result like this:
enter image description here
But I can't insert new data into these two columns:
insert into num_of_people(date_pre,time_re) select left(Deal_time,8),right(Deal_time,2) from num_of_people;
Nothing happened:
enter image description here
I create a new table,that's worked:
create table Date_time(
date_p varchar(40),
time_p varchar(40)
);
insert into date_time(date_p,time_p) select left(Deal_time,8),right(Deal_time,2) from num_of_people;
So,Why?
I just want to split the charactor in column Deal_time of table num_of_people. What's the better way? I am a novice in MySQL.Thanks a lot.
According to your images, you should perform an Update query instead of Insert to the table num_of_people.
To explain why you can't insert new data into those two columns, my best guess is that at least one of your existings columns must be a NOT NULL column that has no explicit DEFAULT value defined. Even if you succeeded in executing the Update, the total number of your table's records would be doubled.
Ref: http://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html
For data entry into a NOT NULL column that has no explicit DEFAULT
clause, if an INSERT or REPLACE statement includes no value for the
column, or an UPDATE statement sets the column to NULL, MySQL handles
the column according to the SQL mode in effect at the time:
If strict SQL mode is enabled, an error occurs for transactional
tables and the statement is rolled back. For nontransactional tables,
an error occurs, but if this happens for the second or subsequent row
of a multiple-row statement, the preceding rows will have been
inserted.
If strict mode is not enabled, MySQL sets the column to the implicit
default value for the column data type.

how to change a lot of records at once in phpmyadmin

I'd like to know how to update several records at once where a certain column type is selected.
I have found how to select records. I have found how to update records. But i don't know how to do it both together for it to work.
Selecting:
SELECT * FROM users WHERE 'type'='new'
Updating:
update table
set column = 937
So basically i want to change the info in the 'column' to 937 in the 'table' if another column 'type' is 'new'.
Thanks,
You can do this by simply adding a WHERE clause to your UPDATE statement:
UPDATE `users`
SET `myColumn` = 937
WHERE `type` = 'new'
Of course, change myColumn to match your column name
You can do this with a subquery :
update users
set column = 937
where id in (select id from users where type='new')
Just change the columns name if I got them wrong.
After researching for a while I found another solution to this problem. When referencing the same table or field name in a query, the name space in MySQL ends up with duplicates and fails. To overcome this use the "AS" syntax to name the duplicate items. Also, update queries often require a key field to be used as the parameter for the where clause, this is often your auto-incremented ID field. This example provides a solution for both of these problems.
UPDATE tbl
SET field=newValue
WHERE tbl.key IN
(SELECT idNew FROM
(Select tbl.key AS idNew
FROM tbl
WHERE field=editValue) AS tblNew);
tbl - Table name being updated
field - Target field for update
newValue - Value to replace items in the target field
tbl.key - Field name for the key in your table
idNew - New name for key in your table, to replace the name and prevent failure from duplicating names in query. could be any alphanumeric string. 'id' or 'id_new' or 'id_n'
editValue - The value to change
tblNew - New name for query, to prevent duplicate table names in the query. Could be any alphanumeric string 'tbl_n' or 'tbl_new' or 'tbl_test'
This query gets the key values for all the records that match records that have values you want edited, then changes the names of the key and tbl so they can be processed by MySQL, lastly the update query runs and changes the values based on the keys provided in the sub-query.
Hopefully this saves someone else a few hours!

MySQL insertion with all values at once

I was doing an INSERT INTO TABLE(...,...,...,...,...) VALUES(...,...,...,
When I closed by mistake my MySQL Query Browser. The table has too many columns, so I was wondering, is there a command that you don't need to type all names of the columns table?
If so, how?
THere is
INSERT INTO TABLE VALUES(...,...,...)
You just need to specify ALL fields in EXACTLY same sequence as they're in table definition.
For AUTO_INCREMENT column, or for columns where you want to use DEFAULT value as defined in table definition (also TIMESTAMPs) use null as a value.
If you are insterting into all the columns you can write:
insert into tablename values(...,...,etc.)