inserting default value along with other columns - mysql

Hi help in this case i need to insert two columns in a table one column has to be default value as 'Other'(Please don't suggest to put Other as default value) and other column should get inserted from different table.
This is my sample code please suggest necessary change:
INSERT INTO table1 (`ID`,`specialty`)
SELECT `ID`,`here i need to put default value as other` from table2 a WHERE
Id IS NOT NULL

Do you mean this?
INSERT INTO table1 (ID,specialty)
SELECT ID, 'Other'
FROM table2
WHERE ID IS NOT NULL

Related

Auto populate two columns in mysql

I have a MYSql table with columns id,name,description and status. The id column is auto incremented and is primary key. Now, I want to add another column to the table named 'display_priority'. I want the records to be pulled using 'order by' on this column.
How can I auto populate this table(based on ID), while inserting records?
The display_priority values do not matter while inserting. It shall be rearranged later on, at some point of time, before displaying the records. But, the inserted values need to be unique.
Any help? Thanks in advance.
Edit: I know that this can be done by an after insert trigger. What if, I wanted to avoid a trigger. Any other way?
You take max id of the table and add 1 as auto-incremented id prediction.
INSERT INTO `table_name` (col1, col2, col3)
SELECT val1, val2, max(id) + 1 AS val3 FROM `table_name`;
<!-- id is auto-incremented -->
Example
INSERT INTO `users` (name, email, display_priority)
SELECT 'Dave', 'dave#email.com', max(id) + 1 AS display_priority FROM `users`;
If you want to avoid TRIGGER you could use the current UNIX_TIMESTAMP or CURRENT_TIMESTAMP as a default value. But if tow or more INSERT at the same second you have a problem ...
display_priority TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
You could add a TRIGGER witch update the display_priority on insert and the value is the maximum of display_priority plus one.
CREATE TRIGGER table_name BEFORE INSERT ON display_priority
FOR EACH ROW SET #sum = (SELECT MAX(display_priority)+1 FROM table_name);

Insert INTO table where one table has a NOT NULL

I have two identical tables bar one column, (one live, one test)
The test table has an extra not null Column called "MatchOrderNo"
I'm trying to extract data from Live to test, how can i populate this not Null for all rows?
basically my current statement is
INSERT INTO test SELECT * FROM live;
Depends what do you want to insert to the NOT NULL column? For a constant value :
INSERT INTO test
SELECT t.* , 'Val_For_NotNull_Col' FROM live t
You can use a Default value like:
INSERT INTO test SELECT live.col1, live.co2, '1' FROM live;

INSERT INTO ... SELECT if destination column has a generated column

Have some tables:
CREATE TABLE `asource` (
`id` int(10) unsigned NOT NULL DEFAULT '0'
);
CREATE TABLE `adestination` (
`id` int(10) unsigned NOT NULL DEFAULT '0',
`generated` tinyint(1) GENERATED ALWAYS AS (id = 2) STORED NOT NULL
);
I copy a row from asource to adestination:
INSERT INTO adestination
SELECT asource.*
FROM asource;
The above generates an error:
Error Code: 1136. Column count doesn't match value count at row 1
Ok, quite strange to require me to mention generated query. But ok, I add that column to the query:
INSERT INTO adestination
SELECT asource.*, NULL AS `generated`
FROM asource;
This has worked fine in 5.7.10. However, it generates an error in 5.7.11 (due to a fix:
Error Code: 3105. The value specified for generated column 'generated' in table 'adestination' is not allowed.
Ok, next try:
INSERT INTO adestination
SELECT asource.*, 1 AS `generated`
FROM asource;
But still the same error. I have tried 0, TRUE, FALSE but the error persists.
The DEFAULT value which is stated as the only allowed value (specs or docs). However, the following generates a syntax error (DEFAULT is not supported there):
INSERT INTO adestination
SELECT asource.*, DEFAULT AS `generated`
FROM asource;
So, how can I copy a row from one table to another using INSERT INTO ... SELECT if the destination table adds some columns where some of them are GENERATED?
The code calling this query is generic and has no knowledge what columns that particular tables have. It just knows which extra columns the destination table has. The source table is a live table, the destination table is a historical version of the source table. It has few columns extra like user id made the change, what type of the change it is (insert, update, delete) when etc.
Sadly this is just how MySQL works now to "conform to SQL standards".
The only value that the generated column can accept in an update, insert, etc. is DEFAULT, or the other option is to omit the column altogether.
My poor mans work around for these are to just disable the generated column while I'm working with the data (like for importing a dump) and then go back and add the generated column expression afterwards.
You must declare the columns
Insert into adestination (id, generated)
select id, 1
from asource;
It is best practice to list out the columns, and use null as field1 for the auto incremented id field.
INSERT INTO adestination
(id,
field1,
field2)
SELECT
null AS generated,
asource.field1,
asource.field2
FROM asource;

how to copy data from one SQL column table to another SQL column table

Need assistance on the following. How can I copy data from one SQL column table to another sql column table?
I have the following tables
dbo.Thecat62 and dbo.thecase6
inside dbo.Thecat62 , I need to copy the column Work_Order_Job_Start_Date values to dbo.thecase6 column Job_Start_Date. Currently there are null value in the Job_Start_Date column in dbo.thecase6.
I have tried using the following command
INSERT INTO dbo.thecase6 (Job_Start_Date)
SELECT Work_Order_Job_Start_Date
FROM dbo.thecat62
but received the error Cannot insert the value NULL into column 'CaseNo', table 'Therefore.dbo.TheCase6'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Any help will be great!
Thanks!
Because on this table Therefore.dbo.TheCase6 for CaseNo you have specify Not NULL Constraints
something like this
CaseNo int NOT NULL
But you did not select the CaseNo column from the dbo.thecat62 table, so you are explicitly trying to insert nulls into a non-nullable column.
You just need to select the CaseNo column, as well, presuming it does not contain any nulls in teh source table.
INSERT INTO dbo.thecase6 (Job_Start_Date,CaseNo)
SELECT Work_Order_Job_Start_Date,CaseNo FROM dbo.thecat62
The error says it has a column CaseNo which doesn't allow NULL.
Cannot insert the value NULL into column 'CaseNo', table 'Therefore.dbo.TheCase6';
You are inserting rows in the new table which will have just 1 column filled and rest of the columns will be empty
Either
Alter the table in which you are inserting the data and allow the column to allow null values.
Or
if you don't want to allow null values, update the null values to some default values.

how to insert value in single column for multiple rows?

I have registration table with different fields. I have thousands of records in that table. But now I want to add one column called 'gender' to that table. So How Do I insert values for previous thousands of records in the table??
Either use a default value for your new column or add a value afterwards with an update statement.
So either
alter table your_table
add column gender char(1) default '-'
or
update your_table
set gender = '-'
where gender is null
Either way setting a gender to a default value is rather bad since this is a boolean condition which is wrong setting it for all records to a certain value.
DEMO
Adding column:
alter table table_name add column gender char(1)
updating all values:
update table_name set gender = 'M'