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;
Related
I've been struggling to think of a good way to store this data...
Each row is a doctor's practice. Each practice has a price which differs for each age group. For example one practice might have this pricing structure, where the left is the age and the right is the price:
0: 0
13: 20
21: 35
35: 35
60: 20
However the structure varies from practice to practice. So another practice might have this:
0: 5
25: 50
40: 35
60: 20
I need to be able to fetch the price for any given age.
Currently I have all the prices stored in a JSONB column on each row. I grab the JSON object in nodeJS and run an algorithm to get the price for an age. Surely this isn't ideal, but I can't think of any way to store and query this in postgres.
So far I've considered having another table for fees and storing each age/price couple like this:
name | age | price
but that seems kinda clumsy and would create many thousands of rows. The other idea I had was to have columns for the cost at each possible age on the practice row like this:
name | 0 | 5 | 6 | 13 | 16 | 18 | 21 | 25 | 40 | 60
but that also seems incredibly clumsy and unwieldy.
Any ideas? Or maybe I'm better off just keeping it in JSON?
I would store the prices in a separate table that defines the "interval" in which the price is valid:
create table practice
(
id integer not null primary key,
name text
);
create table prices
(
practice_id integer not null references practice,
from_age integer not null,
to_age integer,
price integer not null,
constraint valid_interval check (from_age < to_age)
);
You can also add an exclusion constraint to prevent overlapping intervals:
CONSTRAINT no_overlap
EXCLUDE USING gist (practice_id WITH =, int4range(from_age, to_age) WITH &&)
Of course instead of using two columns from_age and to_age you could put that into a single int4range column which has the benefit of being indexable. As you typically will query per practice that only leaves a very small number of rows to go through so the index on the range column isn't necessary (or won't help)
To query the price, you just do:
select price
from prices
where practice_id = 42
and 25 between from_age and to_age
As the condition practice_id = 42 already narrows this down to just 5 or 6 rows, this is quite fast. Even if you have thousands of practices.
If I understand it correctly, the price depends on the combination of practice and age. How about make a table with 3 columns:
Practice_ID | Age | Price
Where the Practice_ID and Age are the primary keys. That way you can query for the price with the practice and age.
My table looks like
Fields Jack Mike Bruce ... Tony
Salary 150 300 125 ... 150
CTC 100 100 250 ... 500
Here Jack is the base user and I need to compare the salary and CTC of Mike , Bruce ,Tony upto n columnof users in the table and add comparison rating columns such as the output looks like,
Fields Jack Mike Mike_rating Bruce Bruce_rating ... Tony
Salary 150 300 high 125 low ... 150
CTC 100 100 equal 250 high ... 500
Output Explanation
The user list grows dynamically and corresponding rating column needed to be added.
Jack Salary is 150 and Mike is 300 . So Mike_Rating column should have value as high else low else if two values are equal then equal
Any Help would be appreciated . Thank you
Create a trigger on table user. Trigger is used exactly for things like this. So, for example, if u create a trigger on table Users to be activated every time after insert, then u can get the inserted data, and using it you can make changes to your table. You would also probably want to create triggers for delete and update operations on User table. You can find details on how to create a trigger here
My main table, from which I take all the data from is "RequestTable" (I reduced it down to make it easier) in which I have:
ID_student
ID_professor
Date (and the three altogether are primary keys)
changeprofessor-note - if student wants to change the professor
then he/she should write in that field a sentence
why he/she wants to do the change
professor-reject-note - if the professor is not happy about the work of
the student, then he can choose not to mentor that
student anymore, leaving him without a mentor and the
student should choose another mentor later.
ID-seminar- after choosing a mentor the students
can choose the seminar they want to work on
changeofSeminar-note - if the student wants to change the seminar
then they need to write the reason why in here
(then the ID of the new seminar should be written in
the ID seminar field also)
IDapprove-reject - all approving or rejecting is going through this field
My initial theory was that the students could choose the mentor and the seminar in one row, but it seems too complicated now because I have no idea how to make everything work after changing mentors, declined mentoring, changing seminars and so on.
I set a more comfortable theory that all the students need to choose the mentor first. So that I could get easier the data of mentoring when needed. And I set "is null" in the query under the "ID_seminar" and "changeofseminar-note" because any changes on just the seminar part can't affect the rows where the students chosen their mentors/professors and got approved.
I implemented your code and got this:
SELECT [requesttable].ID_Student, Max([requesttable].Datum) AS MaxOfDatum, First([requesttable].ID_Profesor) AS ID_Profesor, [requesttable].ID_status_odobrenja
FROM [requesttable]
WHERE ((([requesttable].ID_Student) Not In (SELECT [ID_Student]
FROM [requesttable]
WHERE [IDapprove-reject] IS NOT NULL )))
GROUP BY [requesttable].ID_Student, [requesttable].IDapprove-reject, [requesttable].changeseminar-note, [requesttable].ID_seminar
HAVING ((([requesttable].IDapprovereject)=1) AND (([requesttable].changeseminar-note) Is Null) AND (([requesttable].Id_seminar) Is Null))
ORDER BY [requesttable].ID_Student, Max([requesttable].Datum), First([requesttable].ID_Profesor), [requesttable].IDapproved-reject;
And i get:
3 12 1
15 11 1
55 5 1
And I need:
3 6 1
15 6 1
52 5 1 - after being rejected by mentor 10,
the student choose another mentor (id 5) and got approved.
55 5 1
Old info below:
I got my query to this point and two other data are set to show only rows with null values to get this:
ID student Id professor date professor-reject-note ID accept/reject
3 12 12.11.2012 null 1
3 6 13.11.2012 null 1
52 10 12.11.2012 null 1
52 10 15.11.2012 NOT null 1
55 5 12.11.2012 null 1
I want my results to be
3 6 12.10.2013 null 1
15 6 7.1.2013 null 1
55 5 12.11.2012 null 1
Totally exclude StudentID 52 because of the professor-reject-note meaning the professor doesn't want to mentor the student anymore. Also I have a doubt about the ID accept/reject number in that option , maybe I could set it to 2 instead of 1 to make it easier. 1 means accepted, 2 would mean rejected, but if I set it to 2 and exclude the entire row I still can't get rid of the other ID 52 row. I'm a bit confused about it and have no clue how make it work.
If I set date to maxdate and Id professor to group by FIRST I almost get what I want, all the data is right except the Student ID 52 is still there - both rows.
You could use:
SELECT t.[id student],
t.[id professor],
t.DATE,
t.[professor-reject-note],
t.[id accept/reject]
FROM atable t
WHERE t.[id student] NOT IN
(SELECT [id student]
FROM atable
WHERE [professor-reject-note] IS NOT NULL)
Your field / column names could do with some work.
I have a fact table that can store 2 types of transactions - TrxType1, TrxType2 having an attribute called Owner_Id mapped to Dim Owner. Problem is only one type of transaction TrxType1 has owner and the other does not have a relationship. Hence while querying the cube I am not getting the records for TrxType2.
Is there a way to manage it? I have already tried changing Null Processing to UnkownMember but still I am unable to see.
In my practice I always fill in dictionary tables with None value and map to this member all blank values.
But if you don't have any transactions with type TrxType2 how you can count them?
If you have next fact table:
Type_Id Owner_id ...
__________________________________________
1 13 (just for example)
1 8
0 11
0 4
Dictionary TrxType:
___________________________
id Code
0 None
1 TrxType1
2 TrxType2
your dimension can have the following hierarchy
Count of rows
All 4
-None 2
-TrxType1 2
-TrxType2 0
If you have different situation - please write an example.
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.