If I have the table FOO with the columns NUMBER - FOO.ID, VARCHAR - FOO.LABEL is there a way I can have SQL set a calculated DEFAULT VALUE for FOO.LABEL to be like "DFLT_LBL_" + FOO.ID
I.E a record with ID = 1 would have LABEL set to "DFLT_LBL_1" and a record with ID 2 would have LABEL set to "DFLT_LBL_2" and so on.
One method that works in all databases is to use a view:
create view v_table as
select t.*, concat(label, id) as label
from foo t;
This would not allow you to change it. You could have a column in the table called something like override_label, which has a NULL default and then the view is:
create view v_table as
select t.*, coalesce(override_label, concat(label, id)) as label
from foo t;
In some databases, you can do something similar with a computed column:
alter table foo add label as (coalesce(override_label, concat(label, id)));
Related
How can i create a customized auto increment in mysql database using phpadmin I want the auto increment to start from something like id_0001, id_0002, id_0003 not the default 1,2,3.How to auto increment and auto_generate the row"tagId" following that same sequence
You can use a VIEW to get the ID values like this based on the id column:
-- creating the VIEW
CREATE VIEW view_name AS
SELECT CONCAT('id_', RIGHT(CONCAT('0000', id), 4)) AS custom_id, * FROM table_name;
-- using the VIEW
SELECT * FROM view_name
You shouldn't change the default behaviour of the the auto increment to get a id value like id_001. A id value should be a numeric value.
In an attempt to copy all the columns of an existing row where only ONE column will change (called "field") and the auto generated primary key field (called "id") should be generated as usual by the insert statement, my naive approach is this:
drop table if exists temp1;
create table temp1 as
(
SELECT *
FROM original
WHERE field="old value"
)
;
# Update one of the many fields to a new value
UPDATE temp1
SET field = "new value"
;
# Getting rid of the primary key in the hope that a new one will be auto generated in the following insert
ALTER TABLE temp1 DROP id;
# Fails with syntax error. How can I specify "generate new id" ?
INSERT INTO original
SELECT NULL as id, *
FROM temp1
;
This fails with syntax error as it is not allowed to put NULL as id in the last select statement. So how would I go about doing this? Is there a simpler way?
SELECT * is antipattern. You should explicitly set columns:
INSERT INTO original(col1, col2, ...) --skip id column
SELECT col1, col2, ...
FROM temp1;
Or even better skip the temp1 table at all. Single statement solution:
INSERT INTO original(col1, col2, ..., field) -- skip id column
SELECT col1, col2, ..., 'new_value'
FROM original
WHERE field='old value';
To get what you want you need mechanism like Oracle DEFAULT Values On Explicit NULLs.
But even then you need to drop column from temp1, because:
INSERT INTO original -- only N column
SELECT NULL as id, * -- this will return N+1 columns
FROM temp1;
So you have to use:
ALTER TABLE temp1 DROP COLUMN id;
-- now columns match
INSERT INTO original -- only N column
SELECT NULL as id, * -- this will return N columns and
-- NULL is handled by DEFAULT ON NULL
FROM temp1;
I have a MySQL table containing default and override values that, when simplified, looks like so:
create table t (
identifier varchar(20),
override varchar(20),
data1 int,
data2 varchar(2),
...
)
I want to get all items out of the table that have a blank in the override column. Unless there is a row with the same identifier and a non blank override. In that case I want the override to be returned instead.
I suspect I want to do something with GROUP BY to select the row but I can't figure out how to ensure I'm getting the right values in the datax columns.
Here is how I would achieve the solution ... perform a sub query on and get sameIdentifierWithOverride - this is the number of rows in the table with the same identifier but with an override that isn't empty. You can then use having to filter down the results ..
select *,
(select count(*) from t t2 where t2.identifier=t.identifier and t2.override != '') sameIdentifierWithOverride
from t where override='' having sameIdentifierWithOverride=0
Suppose I have a table with one column - called 'person' that contains a list of names. I want to find a specific person based off his index.
I tried using a sql variable to track each column index but the issue is - is that if I have a table of 5 records this will always output the 5th record.
SET #row_num = 0; SELECT #row_num := #row_num + 1 as row1 ,person FROM table;
SELECT row1 from table WHERE person = 'name'
I would recommend changing your database to add a second column for row_id. This is a fairly common practice. Then you can just use
SELECT * from table WHERE row_id = 3;
This will return the third row.
Another best possible way would be by means of a TEMPORARY TABLE as explained below
create a temp table
create temporary table temptab(ID INT NOT NULL AUTO_INCREMENT,Person VARCHAR(30))
Then insert data to temp table as
insert into temptab(Person) select Person from mytable
Then select the specific index person name from temp table like
select Person from temptab where ID = 5
Two questions:
1)
There are several tables that are used as an archive for other tables.
To do so, there is a
INSERT INTO data_archive_table (SELECT * FROM data_table)
The problem is that the data_table.id should be kept as data_archive_table.old_id.
Is there a way to write a query that will look like: SELECT *, id AS old_id FROM data_table, while the results columns will have ONLY the old_data column, and NOT the original id column?
Using all column names is the only option I see, but I prefer to avoid it.
2)
I want to add a virtual column named deleted_time to the insertion query, that will hold the current time.
Can it be done? if so - how ?(tutorials will be great)
Try this:
1.) You can use something like this query:
INSERT INTO data_archive_table
SELECT id AS old_id -- be sure that data_archive_table has column oldID
,... -- You need to specify the names of the columns
FROM data_table
WHERE id = 'IDHERE' -- If you want to have condition.
2.) For this, you can add the value directly in you select statement
INSERT INTO `tableName`
SELECT colA,
colB,
, ...
, NOW() as deleted_time -- NOW() is a function in MySQL
FROM `sourceTable`
WHERE colA = 'IDHERE' -- If you want to have condition.
NOW() in MySQL