Row decrementing/incrementing, in PHPMyadmin - sql

I have a table in my database, newvehicles which has the following fields:
id (AUTO_INCREMENT)
make
model
doors
bodystyle
price
and this is an example:
1 Volkswagen Golf 2.0 GTI 3 Hatchback $39,490
2 Ford Mondeo 2.3 Zetec 4 Sedan $54,450
3 BMW 3-Series 318i 4 Sedan $62,667
4 Renault Clio 1.2 Base 3 Hatchback $22,686
5 Volvo S60 3.2T SE 4 Sedan $49,460
6 BMW 5-Series 540i 4 Sedan $89,990
If I deleted, say, row 4, it would have rows 1, 2, 3, 5 and 6, and in order to reset the increment I use this code:
UPDATE automobiles SET id=id-1 WHERE id > 3
However, is there any way I can automatically get phpMyAdmin to reset the increment values for the id field (since it has an auto increment in) and I don't really want to keep using the above code every time.
By the way, not sure if this relevant, but the database is stored as InnoDB, just in case I have to do foreign keys for other databases that may link to it.
I'm fairly new to this, so would appreciate the help!

The auto-increment value is used as a unique identifier. So later if you save that key elsewhere you will always pull that particular record.
If you want a number that is always sequential, I would suggest using a counter when you display them. I think that would save work in the long run.

Related

Delete table column content

I have the following table:
car
score
description
Opel
30
43
Volvo
500
434
Kia
50
3
Toyota
4
4
Mazda
5000
4
How I can delete all the content of table column score without changing the table structure?
Expected result:
car
score
description
Opel
43
Volvo
434
Kia
3
Toyota
4
Mazda
4
As pointed out by Bergi, you have the option of setting all values in the column to NULL or 0, depending on what you need, or you can delete the entire column.
Solution 1:
UPDATE cars SET score = NULL;
or
UPDATE cars SET score = 0;
This will preserve the score column but set all the values to NULL or 0 respectively. Note that NULL and 0 are different things. NULL means the field is empty but 0 means the field has the numerical value 0.
If you don't need the score column anymore, you can delete it like this:
ALTER TABLE cars
DROP COLUMN score;
This will delete the column score and you will not be able to use it anymore.
I think the answer by gowner is ok.
However in case you have no permission to alter table structure, you cannot delete column.
And given the score field is not nullable,
you cannot update the field to null.
You must be careful that updating the score to 0 may not be ideal.
0 may have different meaning in your table. Maybe minimum score is 1 and 0 is not a possible value in the field. Or a consensus in your organization that -1 means "no value". They should be relfected in the default constraint or guidelines of your organization.
I would prefer to be safe
UPDATE cars SET score = DEFAULT;

Custom Sort Order in CAML Query

How would one go about telling a CAML query to sort the results in a thoroughly custom order?
.
For instance, for a given field:
-- when equal to 'Chestnut' at the top,
-- then equal to 'Zebra' next,
-- then equaling 'House'?
Finally, within those groupings, sort on a second condition (such as 'Name'), normally ascending.
So this
ID Owns Name
————————————————————
1 Zebra Sue
2 House Jim
3 Chestnut Sid
4 House Ken
5 Zebra Bob
6 Chestnut Lou
becomes
ID Owns Name
————————————————————
6 Chestnut Lou
3 Chestnut Sid
5 Zebra Bob
1 Zebra Sue
2 House Jim
4 House Ken
In SQL, this can be done with Case/When. But in CAML? Not so much!
CAML does not have such a sort operator by my knowledge. The workaround might be that you add a calculated column to the list with a number datatype and formula
=IF(Owns="Chestnut",0,IF(Owns="Zebra",1,IF(Owns="House",3,999))).
Now it is possible to order on the calculated column, which translates the custom sort order to numbers. Another solution is that you create a second list with the items to own, and a second column which contains their sort order. You can link these two lists and order by the item list sort order. The benefit is that a change in the sort order is as easy as editing the respective listitems.

Rows to mimic Columns

I want to structure a table to mimic column level filters as row level filter just to avoid adding new columns.
Let's say i have following table to store cars' details
-------------------------------------
Type Color Year
-------------------------------------
Mini Silver 2010
Standard Silver 2011
Fullsize White 2011
Luxury Black 2010
Sports Red 2011
Convertible Red 2009
If i want to store Make of these cars as well and for this i have to add an additional column and another column if i have automobiles other than cars.
So the question is how can i structure this table to avoid adding new columns? The structure should require only to add rows to define properties of my records.
[Hint] The structure may have multiple tables, one to store rows/records and other to store columns/properties and then some kind of mapping between them OR entirely new structure.
EDIT
Some of the properties of my data are fixed and some are dynamic. Fixed properties can be mapped to the given sample Car model as Availability, Condition and the dynamic could be anything which a person may ask about an automobile. Now i don't need all columns to be mapped as rows but few and these are dynamic and i don't even know all of them. My apologies that i didn't mention this earlier.
You could use the entity-attribute-value design (EAV).
entity attribute value
1 Type Mini
1 Color Silver
1 Year 2010
1 Make Foobar
2 Type Standard
2 Color Silver
etc...
You may also wish to store the attribute names in a separate table.
However you should consider carefully if you really need this, as there are a few disadvantages. The value column must have a type that can store all the different types of values (e.g. string). It is much more cumbersome to write queries as you will need many joins, and these queries will run more slowly as compared to a traditional database design.
To give you a head start: Think about redesigning to allow multi-colored vehicles like motorbikes:
vehicle
Id Year vehicle_type vehicle_make
-------------------------------------------
1 2010 1 1
2 2011 2 2
color
Id Name
-----------
1 Black
2 White
3 Red
4 Blue
vehicle_color
vehicle_id color_id
-----------------------
1 3
2 1
2 2
vehicle_type
Id Name
-----------
1 Car
2 Motorbike
vehicle_make
Id Name
-----------
1 Porsche
2 BMW
Bonus
Since I'm quite familiar with the car domain, I'll throw in an extension for your vehicle colors: There are tons of color names ("Magentafuzzyorangesunset") invented by manufacturers and you'll want to map them to "real" base color names ("Red, "Blue", "Green", etc.) to enable searching for both.
Your color table then could look like that
Id Name base_color
-----------------------------
1 Midnight 1
2 Snow 2
and you'll add a base_color table
Id Name
-----------
1 Black
2 White

Manipulate the sort result considering the user preference - database

Suppose we have a simple database containing following data:
name
apple
pear
banana
grape
User want to sort those fruits by the name, and we will have, with no surprise
apple
banana
grape
pear
However, for some reason, user would like to place pear as the 3rd fruit ,that means he would like to have :
apple
banana
pear
grape
And, importantly, the user want to preserve this order when he want to sort the fruits by name thereafter.
How should we tackle this problem? Of top of my head, we could add a field user_sort_id, which will be updated when user sort and manipulate the sort result and we will use that filed as the sort key.
init value -> sort by name ->place pear as the seconds
name user_sort_id
apple 0 0 0
pear 1 3 2
banana 2 1 1
grape 3 2 3
This approach should work in theory. However, in practice, I can not think of an elegant and fast SQL statement that could accomplish this. Any ideas or alternatives?
If you want each user to have independent sort orders, you need another table.
CREATE TABLE user_sort_order (
name VARCHAR(?) NOT NULL REFERENCES your-other-table (name),
user_id INTEGER NOT NULL REFERENCES users (user_id),
sort_order INTEGER NOT NULL -- Could be float or decimal
);
Then ordering is easy.
SELECT name
FROM user_sort_order
WHERE user_id = ?
ORDER BY sort_order
There's no magic bullet for updating.
Delete all the user's rows, and insert rows with the new order. (Brute force always works.)
Update every row with the new order. (Could be a lot of UPDATE statements.)
Track the changes in your app, and update only the changed rows and the rows that have to be "bumped" by the changes. (Parsimonious, but error-prone.)
Don't let users impose their own sort order. (Usually not as bad an idea as it sounds.)

MySQL: Getting connected (similar) data with lef/right fields

In MySQL Im having two tables:
PRODUCTS (id, Name)
SEEALSO (id, prodLeft, prodRight)
SEEALSO defines which PRODUCTS are related together and are represented as binded fileds "prodLeft"-"prodRight".
For Example:
PRODUCTS:
1 Desk
2 Table
3 Chair
4 Doors
5 Tree
6 Flower
SEEALSO
1 1 2
2 2 3
3 3 4
4 5 6
From that we can see binding of Desk-Table-Chair-Doors and Tree-Flower.
I would now want to write SQL statement where I could specifie PRODUCT name (e.g. Chair) and i would get result of binded fields that are connected with it (e.g. Chair: Desk-Table-Chair-Doors).
From this point on i would like to know if this is even possible for my data presentation concept in SEEALSO and if it is if you could help me solve my problem.
As you're wondering whether it's even possible, you could look into this information on Nested Sets, which is the MySQL way of doing this (I gather).
I could not give you a worked sample, as I'm no MySQL expert: perhaps this will help you enough given the general nature of your question.