Automatically create record when another is created - sql

I have two tables in my database: Users, Roles and Membership. The Membership table assigns users to specific Roles.
How could I automatically create the Membership record for anytime a new record is inserted in Users.
Example: When a user is created and assigned an ID number (# 562), The database would automatically add them to the Membership table with a specific role ID.
How could I do this?

Write an AFTER INSERT TRIGGER on Users TABLE, that will INSERT the new Row in the Membership table.
http://msdn.microsoft.com/en-us/magazine/cc164047.aspx
Assuming you have a Default RoleID for your new Membership row, when a new User is inserted in Users table, something like this should work.
CREATE TRIGGER TRI_USERS_INSERT on Users
AFTER INSERT
AS
SET NOCOUNT ON
-- If you have a Default RoleID, select that into a variable and use it in the INSERT below.
-- For this example, I am using just the number 1
-- Also assumes that the ID for Memberships table is AUTO GENERATED, so it's not in INSERT list.
INSERT INTO Memberships (UserID, RoleID)
SELECT ID, 1 FROM INSERTED
GO

Related

Using Sequence for inserting

I want to insert a new row in my table.
There I want to put the ID with the help of seq_name.nextval.
So how to know the sequence name for that particular table?
To use a sequence to generate IDs, you create it normally in the schema where the table is.
As user application user GEM_APP:
CREATE TABLE my_table (id NUMBER, col1 ...);
CREATE SEQUENCE my_seq;
The application user itself (and f.i. it's stored procedures) can use the sequence directly:
INSERT INTO my_table (id, col1) VALUES (my_seq.nextval, 'bla');
However, other users need the correct privileges. Usually, you grant select rights on the sequence to the same users or roles you grant insert rights on the table:
GRANT SELECT, INSERT ON my_table TO user_xy;
GRANT SELECT ON my_seq TO user_xy;
Then the other user can insert data into the table, but must qualify the schema:
INSERT INTO gem_app.my_table(id, col1) VALUES (gem_app.my_seq.nextval, 'bla');
You can create aliases to hide the schemas, some people like them, some not, but I would definitely not recommend to use PUBLIC synonyms as they are hard to control and create all kind of namespace clashes.

Create after insert trigger that updates record in parent table after inserting record into child table

I have two tables
License(licenseId, licenseNumber, lastUsed)
User(userId, userName, dateAdded, licenseId)
After adding a user to a respective license I would like to update the lastUsed field with the most recent date found within the user table using the license.
I am using MS Access
Since you are using Access 2010 you can create an After Insert Data Macro on the [User] table, something like this:

Entering many Values into a sql database at one

Question:
I have a table with 100 rows called user
I am after creating a new table called village and I would like to have it automatically fill in 100 rows in the new table
You can use a select to full records of your new table like this
insert into village (user_id, owner)
select id, username
from `user`

Sql - create entry in table A for every entry in table B

I have table Users model with id. Now i create table(model) Settings which should store settings for every User form Users table. How can i create entry in table Settings for every exist User from Users table?
Column Users:
id ;some value ; someothervalue;
Column Settings:
id ; user_id ; message_1 ; message_2
I want to create entry in the table Settings for every User. Right now every new User have before_create filter but old users havent got entry in Settings table.
Insert TableSettings(list of ColNames ...)
Select [list of colnames ...]
From TableUsers
Just make sure that the list of colNames in tableSettings match (in the number of columns and datatype) the list of columns in tableUsers.
to start with just the UserId and message_1
Insert tableSettings(userID, message_1)
Select id, 'A message with someotherValue:' + someothervalue
from tableUsers

How to Create Trigger to Keep Track of Last Changed Data

CREATE TABLE Member
(
memberID - PK
memberName
dateRegistered - one time process
);
CREATE TABLE MemberLastChanged
(
memberID
memberName
dateEntered
);
If by any chance a user changes his member name, i need to keep track of the currently changed memberName in a history table.
For example, current info is:
memberID: 5534 memberName: james
User changes it to:
memberID: 5534 memberName:
mark
By now, "Member" will hold current values:
5534 and mark
AND
"MemberLastChanged" will hold:
5534 and james
How can i achieve this in t-sql using trigger?
CREATE TRIGGER TRG_Member_U ON Member FOR UPDATE
AS
SET NOCOUNT ON
INSERT MemberLastChanged (memberID, memberName)
SELECT
D.memberID, D.memberName
FROM
DELETED D JOIN INSERTED I ON D.memberID = I.memberID
WHERE
D.memberName <> I.memberName
GO
Also, add a default of GETDATE to dateRegistered so it's recorded automatically.
This also filters out dummy updates by comparing new and old values (INSERTED vs DELETED).
INSERTED and DELETED are special tables available only in trigger.
You create an UPDATE trigger - triggers have access to two logical tables that have an identical structure to the table they are defined on:
INSERTED, which is the new data to go into the table
DELETED, which is the old data the is in the table
See this MDSN article on using these logical tables.
With this data you can populate your history table.
CREATE TRIGGER trg_Member_MemberUpdate
ON dbo.Member AFTER UPDATE
AS
INSERT INTO dbo.MemberLastChanged(memberID, memberName)
SELECT d.MemberID, d.MemberName
FROM DELETED d
You want to have an AFTER UPDATE trigger on your users table - something like:
CREATE TRIGGER trg_MemberUpdated
ON dbo.Member AFTER UPDATE
AS BEGIN
IF UPDATE(memberName)
INSERT INTO
dbo.MemberLastChanged(memberID, memberName, dateEntered)
SELECT
d.MemberID, d.MemberName, GETDATE()
FROM
Deleted d
END
Basically, this trigger checks to see whether the memberName property was updated; if so, a row with the old values (which are available in the Deleted pseudo table inside the UPDATE trigger) is inserted into MemberLastChanged