I have a table with columns: [id, value] (id = integer PK, value= integer). I'd like to update the value column for a given id, retrieveing the old value. Is posible to do this in a single query? or I need to make a query and then an insert?
Thanks in advance
You could create a trigger that puts the old value in another table (or even into another column of the current table). Other than that, you'd need to do two queries.
Related
Is it possible to write a query like the one below?
UPDATE sale SET sale_order='123456789' WHERE **COLUMN_1** = 2
where I don't explicitly pass the column name? Only its position?
I could get the column names but I am trying to avoid querying the database only to get them.
Thanks.
To answer your question, no, there is no syntax in SQL to reference the column by its position. This goes back to relational theory, in the sense that a table is a set of columns, and members of a set are unordered.
You will either have to know the column name, or else query it from the database:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA=SCHEMA() AND TABLE_NAME='sale'
AND ORDINAL_POSITION=1;
It looks like you are trying to design a query that updates a row by primary key, by assuming the first column is the primary key. The primary key isn't necessarily the first column. It isn't necessarily an integer. It isn't necessarily a single column.
So you are already making assumptions about the table definition. You might as well assume the primary key column is named id or some other convention.
Let's say I have a table like this
this table is the result of a query from another larger table stored in my database
All I want is to create a table like this one above and specify for each column a custom format and store it into my database
I know that I could do create table mytab as select ... etc
however i don't know how to specify the column formats that I want in mysql
could you please help ?
If you have the query sql, you should be able to do a select into to store the results in a table. Add a LIMIT clause to just store one row. You could then do SHOW CREATE TABLE tablename (from this SO answer) to get the SQL for creating the table. It would be up to you to figure out what your primary key should be.
Assuming with column formats you mean data types: Use CAST to cast to the desired data type.
create new_table as
select
cast( a.metrique as varchar(100) ) as metrique,
cast( b.nombre_de_lignes as int ) as cote_de_lignes, ...
from ...
You may specify columns properties completely or partially. Like there is no SELECT part, and you simply create empty table.
I.e. like
CREATE TABLE table_name ({any definitions allowed in table creation query:
columns specifications, indices, constraints, FKs, etc.})
SELECT ...
In this form each output column in SELECT must have alias which matches according column name defined in CREATE TABLE part. If alias is absent in the structure then a column with the name==alias will be added to the table definition with dynamically formed properties.
I have a SQL server database design problem.
I have an existing database table with hundreds of records in there. One of the columns is of type NVARCHAR but it should be an integer with all the data in a lookup table.
Is there any clever way in SQL Server to get the data out of the column and into a new lookup table, change the datatype of the column and update the values with the correct ID from the new lookup table??
Thanks in advance.
I'm using SQL Server 2008
No, you have to do it as 3 steps:
Insert the values into the new lookup table
Update the current rows so that the nvarchar column now contains appropriate ID values from the lookup table
Change the column definition to int
(4th of 3 :-)) create a foreign key constraint between this column and the ID column of the lookup table.
Thankfully, int values should always be able to fit in an nvarchar, unless it's an especially small one (in which case you'll have to expand it first).
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.)
what if I wanted to update the records in the table by altering values in one of the columns?
I have records in the table that have one column empty(null values). I want to change these values and insert values from another table into those records.
Basically I have a table with one column empty. I do not want to append to the end of the table but start inserting from record 1.
For the existing records, you would have to use UPDATE to update that one column, WHERE thatColumn IS NULL.
Shouldn't the values in that column have some relation to the rest of the record? I could understand initializing the existing records to a non-null value, or using an UPDATE query to populate data from another table in that column, but all related to the original row...
UPDATE old SET old.badColumn = new.newData
FROM oldTable old
JOIN newTable new on old.someID = new.someID
This would find the related data in newTable matching oldTable, and update the badColumn to some data from newTable... let me know if you need more help.
See the "Using the UPDATE statement with information from another table" section from this page of SQL Server Books Online.