I have created a table using the dynamic columns feature in MariaDB:
CREATE TABLE items
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(100) NOT NULL,
attributes BLOB);
INSERT INTO items (name, attributes) VALUES
('T-shirt', COLUMN_CREATE('color', 'blue', 'size', 'XL', 'price' '30.0'));
The dynamic column contains the columns color, size and price. How would I rename, for instance, the column price to amount, while actually retaining this column's content? I know about COLUMN_ADD and COLUMN_DELETE, but I haven't seen a COLUMN_RENAME function. Is there a way to do this?
My guess is that you would have to use COLUMN_ADD then use COLUMN_DELETE to re-add the same column data under a different name then delete the previous column name from the dynamic columns.
Related
I am trying to create a new Teradata table by copying another table, but also need to add one new column, based on a condition of another column from the old table while copying, can you help me on the code?
create Table new_table as
(select *
from old_table) with data
ALTER TABLE new_table ADD new_col varchar(20) check(new_col in ('National', 'Local')
-- there is a column in the old_table with value ( 'Y', 'N'), how can i create the new column in the new_table with this condition: if Y new_col=national, if N, new_col=local?
Thank you.
You can't create a check constraint that will immediately be violated. Also note that CREATE TABLE AS (SELECT ...) results in all columns being nullable, and if you don't explicitly state a Primary Index the new table will use system default, e.g. first column alone as PI. A CASE expression can be used to populate the new column.
One possible sequence:
create Table new_table as old_table with no data; -- copy index definitions, NOT NULL attributes
ALTER TABLE new_table ADD new_col varchar(20) check(new_col in ('National', 'Local'));
INSERT new_table SELECT o.*,
CASE WHEN o.old_col = 'Y' THEN 'National' ELSE 'Local' END
FROM old_table o;
Given the following example:
CREATE TABLE shapes(
shape_id INTEGER PRIMARY KEY,
background_color TEXT,
foreground_color TEXT,
UNIQUE(background_color,foreground_color)
);
background_color AND foreground_color need to be unique to insert but I don't want that. consider the row exists:
black|blue
and I try to insert:
white|blue
it will insert but if I try to insert another row containing:
black|blue
it will ignore the insert.
Will a primary key of the two cols accomplish this? If so, do I need to also use INSERT OR IGNORE?
First remove the UNIQUE constraint from the definition of the table.
What you can do is create a unique index not on the columns but on the min and max values of the 2 columns:
CREATE UNIQUE INDEX idx_shapes_colors ON shapes(
MIN(background_color, foreground_color),
MAX(background_color, foreground_color)
);
Then, when you try to insert the same combination of colors, like:
INSERT INTO shapes (background_color, foreground_color) VALUES ('black', 'blue');
INSERT INTO shapes (background_color, foreground_color) VALUES ('blue', 'black');
the 2nd statement will fail with an error message:
UNIQUE constraint failed: index 'idx_shapes_colors'
or, if you use INSERT OR IGNORE:
INSERT OR IGNORE INTO shapes (background_color, foreground_color) VALUES ('blue', 'black');
there will be no error but the statement will not insert the row.
See the demo.
I am creating a table in sqlite .Then i insert data in table.Take example I have table A
Having column ID Name.I don't need any duplicate name .If i have already same name .I don't want to insert that name in my table .Mean i don't need duplicate items? How to do that?
Thanks
You can the specify the ID as "PRIMARY KEY" and Name as "UNIQUE". Setting ID as primary key this link helps. If the data base is already there and you want to you only distinct values you follow thislink
You can specify the column to be a "key" or declare the column to be "unique"
Here is an example:
CREATE TABLE myTable (
id INTEGER PRIMARY KEY,
num FLOAT NOT NULL,
cost FLOAT NOT NULL,
paid FLOAT NOT NULL,
date DATE NOT NULL,
UNIQUE(num));
I'm looking to insert data into a column like so:
INSERT INTO Song values (null, 'Truth of a Liar')
into a table set up like this:
CREATE TABLE Song (id integer primary key, name varchar(255), count int not null default 0)
and I get the following error: table Song has 3 columns but 2 values were supplied INSERT INTO Song2 values (null, 'Truth of a Liar')
I was expecting be able to not put in the last column because I occasionally might have to put in the count value. I'm aware that I could explicitly fill the columns like so:
INSERT INTO Song(id, name) values (null, 'msdads')
but I was hoping for an alternative.
Is there another way to INSERT data into tables, using some default values and some set values?
You could do:
INSERT INTO Song values (null, 'Truth of a Liar',0)
when you do not want to insert anything for last column. When you want to occasionally insert a value, you can use that value instead of 0.
My table:
CREATE TABLE methods (
id INTEGER NOT NULL,
version INTEGER NOT NULL,
text TEXT NOT NULL,
PRIMARY KEY (id,version)
);
I would like the column id to auto-increment when null on a new row. I would also like the column version to be set to 1. I would like to store the auto-increment value in sqlite_sequence. When a row is inserted with id I would like version to increase by 1.
I have this at application level but need it at the database level and understand I will have to create a trigger or two.