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.
Related
I am building a database and one of the tables contains the columns "sensor_id" and "station_id". When someone tries to insert a new row in the table we can have a NULL value in the "sensor_id" column but then we can not have a NULL value in the "station_id" under no circumstances. Vice versa, when the "station_id" is NULL, the "sensor_id" must not be NULL. If a value is entered at both columns that is not a problem.
I am currently working in MySQL Workbench and it seems that my choices are to set both columns as NN(Not NULL) which is too strict implementation as one of them is sufficient, to set just one of them as NN which means that one specific column must always be filled(not the case either) or set none of them NN which is too loose as at least one of both values must be given.
Visually the table looks like this(sorry for the Microsoft Word substitute but I have problems with MYSQL server and can not acces the database):
Alert_id is the primary key of the table, so duplicate values are allowed for the other two columns.
How could I implement this?
You should add contstraint to this table, like that:
CONSTRAINT CheckSensorStationNotNull CHECK (station_id is not null or sensor_id is not null)
When I delete a row and create a new one, the primary key doesn't start over at 1, but at 2. I guess that's how it was meant to work when I made it auto_increment. But is it possible to update the primary key whenever a row above it is deleted and all rows under that.
If that isn't possible can I just make another column called rank and make it update depending on what order the rows are in?
There is no reason to care about what the primary keys id is, all that matters is that it's unique. We use sort queries and stuff to worry about order if that's your issue.
If you have code that always requires the primary key to be in the range of 1..number_of_rows with no gaps there is a problem with the design of your application.
You should not make a column called rank, its easy just to do a SELECT * FROM things ORDER BY some_column;
The difference to this is if you need calculated values to sort your table. E.g., an orders total. In order for you to sort orders by their total they need a calculated value on the table. The query is still simple as the one I mentioned.
A MySql 5.3 table with 100K rows has a primary key.
There is also an integer column which is not part of the key. I would like to update this column to contain a unique number for the table. E.g. for the first record it should contain 1, for the second 2 etc.
This could as well be an auto-increment column, but MySql does not allow auto-increment on non-key columns. I don't want this column to be part of the key, because of the way it gets populated from a file etc.
So how such a query would look like?
I don't know why do you want to do something like this, but a possible solution is this:
set #rownum:=0;
update <table> set column = #rownum:=rownum+1 order by <field>
I have a table with several hundred million rows. One of the columns is `status` varchar(10).
Most values in the status are 1 character, some varying up to 10. However a subset of the values has a pattern of its own. A whole group of status values begin with a single character c followed by a number ranging from 0 to 10,000.
I would like to index this column with the following:
ALTER TABLE tbl ADD KEY (status(1), status);
This would be better than having two individual keys, one on status(1) (first character of the whole column) and second status. Together they would always be faster.
However MySQL prohibits me from creating such:
ERROR 1060 (42S21): Duplicate column name 'status'
How can I solve this?
There's really no reason to index status(1) independently of status. One index created on status should handle both situations equally well.
You could create a second column in your table and populate it with the first character of the other column and then create an index on each. However, this might have poor selectivity and not be all that useful.
I'm trying to create a UNIQUE INDEX constraint for two columns, but only when another column contains the value 1. For example, column_1 and column_2 should be UNIQUE only when active = 1. Any rows that contain active = 0 can share values for column_1 and column_2 with another row, regardless of what the other row's value for active is. But rows where active = 1 cannot share values of column_1 or column_2 with another row that has active = 1.
What I mean by "share" is two rows having the same value(s) in the same column(s). Example: row1.a = row2.a AND row1.b = row2.b. Values would be shared only if both columns in row1 matched the other two columns in row2.
I hope I made myself clear. :\
You can try to make multi-column UNIQUE index with column_1, column_2 and active, and then set active=NULL for the rows where uniqueness not required. Alternatively, you can use triggers (see MySQL trigger syntax)
and check for each inserted/updated row if such values are already in the table - but I think it would be rather slow.
I'm trying to create a UNIQUE INDEX constraint for two columns, but only when another column contains the value 1
You can set the value of "another column" to a unique value that does not equal to 1. for example the id of a record.
Then the unique index constraint could be applied to all three columns including the "another column". Let's call the "another column" columnX.
Set the value of columnX to 1 if you want to apply the unique constraint to a record. Set the value of columnX to a unique value if you don't want to apply the unique constraint.
Then no extra work/triggers needed. The unique index to all three columns could solve your problem.
I am not sure about MySQL syntax, but it should have pretty much the same thing that SQL Server has:
CREATE UNIQUE INDEX [UNQ_Column1Column2OnActive_MyTable]
ON dbo.[MyTable]([column1,column2)
WHERE ([active] = 1);
This index will make sure if active=1 then column1 and column2 combination is unique across the table.
In SQL Server this could be accomplished with check constraints, however I do not know if MySQL supports anything similar.
What will work on any database, is that you can split the table in two. If the records where active =0 are just history records, and will never become active again, you could just move them to another table, and set a simple unique constraint on the original table.
I am not sure I understand you 100% but lets say you have a table that has a status column and you want to make sure there is only one raw with a status of 'A' (Active). You are OK with many rows with statuses of 'I' or 'Z' or anything else. Only one row is allowed with status of 'A'.
This will do the trick.
CREATE UNIQUE INDEX [Idx_EvalHeaderOnlyOneActive]
ON [dbo].[EvalHeader]([Hdr Status])
WHERE [Hdr Status] = 'A';
indexes are agnostic of external influences. This kind of constraint would have to be implemented outside your database.