Update already existing columns in table in database in postgresql? - sql

Hi I have a task of assigning nick_name to new users, I added a new column and when a new user is getting created, a nick_name is assigned to it, but what should I do for already existing users (default is not an option here). Every time I do this manually using update statement, Is there a better way to do it?
Thanks

The basic sql-command is
update my_table set nick = username where nick is null;
So you can adjust it to fit the nickname to your needs.

Related

How do I insert data from parameters into a table with a trigger after Update

I am new to triggers, I am trying one that would let me insert the reasons for the update into a log table after another table was updated updated. The problem is that I am trying to make the user type an input as it would do in a stored procedure but that doesn't work with triggers apparently.
Let's say we have the table, Users:
User ID | User
--------+-------
1 John
The result I want is the following:
Log_ID | Reason_Change
-------+------------------------------------------
1 John wanted to change his name to John25
Is that possible?
I am trying:
#Reason_Change VARCHAR(500)
In SQL Server, you can do this with an instead of trigger on a view. The idea is the following:
Create a view that contains the columns from users along with the change reason.
Define an instead of trigger on the view. This will allow an insert to include the change reason.
Only use the view for inserts.
Then, you can include the reason in the insert. The trigger would put all the other columns in users and update the logs table appropriately.
Such triggers are explained in the documentation.

Specifying the ID during UPDATE on Derby TRIGGER

I just started learning and reading about TRIGGERs in Derby which I use as Database for a project in Java. I used to have MSSQL as database in the past.
There's just one problem I have with triggers. I can't find a syntax and I don't know how to specify the ID of which row to update during an UPDATE trigger.
This is what I have.
CREATE TRIGGER COPY_UPDATED_USERNAME
AFTER UPDATE ON ALLUSERS
REFERENCING NEW AS NEWUSERNAME_ROW
FOR EACH ROW MODE DB2SQL
UPDATE ALLUSERS_MAINTABSPERMISSION SET USERNAME = NEWUSERNAME_ROW.USERNAME
It's able to update the USERNAME column but it affects/updates all rows instead of just one row. That's where I'm having problems with.
In MSSQL I can simply put SET #ID = (SELECT ID FROM INSERTED) but I don't know how to do it in Derby.
How to do this on Derby?
Thanks.
I was able to solve my problem. Just had to add the WHERE clause and put the Id.
I hope this helps new derby users.
CREATE TRIGGER COPY_UPDATED_USERNAME
AFTER UPDATE OF USERNAME ON ALLUSERS
REFERENCING NEW AS NEWUSERNAME_ROW
FOR EACH ROW MODE DB2SQL
UPDATE ALLUSERS_MAINTABSPERMISSION SET USERNAME = NEWUSERNAME_ROW.USERNAME
WHERE USERID = NEWUSERNAME_ROW.USERID;

I want to make an sql query has timeout

In my asp.net mvc application , I use Sql Data Adapter to update a record.
For example
UPDATE sample SET status = 1 WHERE id = #id
I need to test a scenerio where this sql code cannot be run and not any records are updated. How can i make this?
Should i make a lock for sample table and how can i do that ?
How can i make this UPDATE query somehow not works and not updates any record ?
Forgot to note that: I cannot change this code or application code and cannot give random #id parameter.
I need to do that in database level.
You can make the specific table in database read only by using one of the below techniques.
Insert, Update, Delete Trigger
Check Constraint and Delete Trigger
Make the Database Read Only
Put the Table in a Read Only File Group
DENY Object Level Permission
Create a View
You could set id to a value that does not exist for sure (e.g. -1).
Or you could remove the user rights of the user accessing the database (in the connection string) for updating this table. Hoping it's not sa :)
Add an [Authorize] attribute to the controller in question and attempt to perform the operation as a guest user.
Edit. To do it server side, enter the following to a query window on the database
Begin Transaction
That should lock up the database while you do your tests

Trigger on specific cell in SQL Server

I have a SQL Server database with users. I want to keep one specific user from changing his picture. I don't believe there is a way to make a single cell immutable but is there a way to create a trigger that is called so that whenever this person changes his picture (newpicture.jpg), the UPDATE gets either rolls back or the data value is changed by to the desired value (justin.jpg)?
I'm not a SQL expert but this is what I have so far
CREATE TRIGGER justin ON Users
AFTER UPDATE
AS
IF EXISTS (SELECT ImagePath from Users WHERE Id = '[justin's id]')
BEGIN
ROLLBACK TRANSACTION;
RETURN
END;
GO
The problem with this is that it just checks if the specific user already has a picture, and then keeps the entire table (and not just that specific user) from updating.
In short, I just want to keep my friend, Justin, from being able to change his current picture, which is of him drinking out of a juice box. Thank you in advance for your suggestions.
If you just want to restrict this specific user from having any ImagePath value other than a predefined one, then you can simply use a CHECK CONSTRAINT:
ALTER TABLE Users
ADD CONSTRAINT CHK_Justin
CHECK ( ([UserId] <> 'Justin') OR
([UserId] = 'Justin') AND (ImagePath = '/images/fixedPic.jpg'))
Instead of Rolling back the whole transaction just update after his update:
CREATE TRIGGER justin
ON Users
AFTER UPDATE
AS
BEGIN
UPDATE Users
SET [ImagePath] = '/images/fixedPic.jpg'
WHERE [UserId] = '[justins id]'
END;
GO

How do I create a trigger to insert a value into an ID field that is Max([ID Field])+1 on insert

When I add a new record I want SQL Server to automatically add a fresh ID.
There are already some records which have been migrated over (from Access) and until I finish preparing the server for other required functionality I will be manually migrating further records over (if this affects any possible answers).
What are the simplest ways to implement this.
The simplest way would be to make the column an IDENTITY column. Here is an example of how to do this (it's not as simple as ALTER TABLE).
Make use of the Identity field type. This will automatically create a value for you using the next available number in the sequence.
Here is an example of how to create an Identity column (add a new column) on an existing table
ALTER TABLE MyTable ADD IdColumn INT IDENTITY(1,1)