Trigger on account, after insert account create a contact with lastname='red' using map
Related
I have a sql script to create a login and user as follows:
USE [master]
GO
CREATE LOGIN [MyLogin] WITH PASSWORD=N'MyPassword',
DEFAULT_DATABASE=[MyNewDatabase],
DEFAULT_LANGUAGE=[us_english],
CHECK_EXPIRATION=OFF,
CHECK_POLICY=OFF
USE [MyNewDatabase]
GO
CREATE USER [MyLogin] FOR LOGIN [MyLogin] WITH DEFAULT_SCHEMA=[MyLogin]
exec sp_addrolemember db_datareader, MyLogin;
exec sp_addrolemember db_datawriter, MyLogin;
GO
CREATE SCHEMA [MyLogin]
GO
However, I only want all of this to happen if the login doesn't exist. If I try and wrap it in a 'IF NOT EXISTS....BEGIN...END' it wouldn't work as the GO statements would override it. I need the 'GO' after the login so it can then be used for the creation of the user. So how can I make sure the login doesn't exist before trying to create both login and user?
SQL Server doesn't support syntax such as CREATE {object} IF NOT EXISTS. It did introduce CREATE OR ALTER syntax in SQL Server 2017, however, that is only for objects where the object CREATE statement must be the only one in the batch.
For objects that don't need that, such as a LOGIN and USER, you can check if the object exists using an IF and NOT EXISTS. For example:
USE master;
GO
IF NOT EXISTS (SELECT 1 FROM sys.server_principals WHERE [name] = N'MyLogin')
CREATE LOGIN [MyLogin]
WITH PASSWORD=N'MyPassword',
DEFAULT_DATABASE=[MyNewDatabase],
DEFAULT_LANGUAGE=[us_english],
CHECK_EXPIRATION=OFF,
CHECK_POLICY=OFF;
GO
USE [MyNewDatabase];
GO
IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE [name] = N'MyLogin')
BEGIN
CREATE USER [MyLogin] FOR LOGIN [MyLogin] WITH DEFAULT_SCHEMA=[MyLogin];
ALTER ROLE db_datareader ADD MEMBER MyLogin; --Don't use sp_addrolemember is has been deprecated for some time
ALTER ROLE db_datawriter ADD MEMBER MyLogin; --Don't use sp_addrolemember is has been deprecated for some time
END;
I am also creating the trigger but it aslo does not work
After creating this trigger i can not insert any borrow recorde.. But I want to make the trigger so that I can not insert the burrow record in December.
I am using Microsoft Azure Database for PostgreSQL with PostgreSQL 10 installed. As I'm trying to work on future tables together with other users I want to enable the other users to alter my tables as well.
I've created a role pgpublish and all users are members of the role. For a new table, which I created, I altered the table owner to the role pgpublish. Now everyone with the role pgpublish is able to alter the table:
ALTER TABLE "MYcoolSchema"."CoolNewTable" OWNER TO pgpublish;
To make this more automatic/generic, I created a trigger function and tried to create an event trigger as explained here.
Unfortunately I can't create the event trigger (The trigger function works fine), as it is stated:
ERROR: permission denied to create event trigger
"trg_create_set_owner" HINT: Must be superuser to create an event trigger.
SQL state: 42501
Is there a workaround for creating event triggers on Microsoft Azure Database for PostgreSQL? How can this look like?
I could run a cron job on another system to scan for new tables and alter the owner of these new tables to pgpublish but this is not cool at all.
Is it possible, to configure a Postgres database such, that a specific table may only be updated by a trigger. I have history table, updated by trigger, so I want to prevent this table from un unauthorised access. I want history table to be updated only from trigger.
Sure. Both the history table and the table with the trigger belong to a user that has no login rights. Then you grant privileges on the latter table to the application user.
To prevent unauthorized access to a table you can change the owner of the table to the user who should be accessing with the following query:
alter table yourschema.yourtable owner to youruser;
Now you can disable the trigger for all other users using the query:
alter table yourschema.yourtable disable trigger triggername all;
here all means that the trigger is disabled for all the users. Now only the owner will be able to use the trigger to update the table.
A trigger always fires on the event on it is defined. Thus, if an update trigger is defined for updates, no one can bypass the trigger during an update if the trigger is enabled.
If you have different user groups with different privileges accessing your database, then you should map this about users in the database. For instance you can disallow that a user can disable triggers on a table.
I want to call a certain procedure that logs everytime a database user is created or deleted in the db ( sql Anywhere 16).
For this i have written a Function that should be called via a trigger when a row is inserted or deleted from table SYS.SYSUSER.
However, I am not able to create a trigger on this table.
Am i allowed to create trigger on this or is there someother way to get notified whenever a user is created or deleted for db?
New to sybase please help.
heres is my create trigger code
CREATE TRIGGER myTrigger AFTER INSERT ON sys.sysuser
REFERENCING NEW AS newRecord
FOR EACH ROW
BEGIN
--
END;
You cannot create a trigger on a system table. You can create a handler for "system events", which are described in the online SQL Anywhere documentation. Unfortunately, creation or deletion of users is not a system event that can be handled. I don't believe there's a way short of polling that you can do what you want to do.
Full disclosure: I work for SAP in SQL Anywhere engineering.