Userlogin time update - sql

i have build a small website with a user login system.
I want it so that when user logs in the 'UserThisLogin' value is being updated automatically but prior to that i want the value of the 'UserThisLogin' to be coppied into the 'UserLastLogin' field in the same row so that each user can keep track of when they were last logged in.
i have tried with a trigger but i cant seem to find the solution.
any help would be appreciated.
below are the sql used for the user table and the little i have of a trigger
CREATE Table User (
UserID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
UserName VARCHAR(20) UNIQUE NOT NULL,
UserEmail varchar(35) DEFAULT NULL,
UserPassword varchar(50) NOT NULL,
UserApproved INT DEFAULT 0,
UserCreated DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
UserThisLogin TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UserLastLogin TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
UserLoginCount INT DEFAULT '0',
UserLevelID INT REFERENCES UserLevel(UserLevelID)
);
CREATE TRIGGER nywebdb.login_trigger
BEFORE UPDATE
ON nywebdb.User
AS
BEGIN
UPDATE User SET UserLastLogin = SELECT UserThisLogin FROM User where ;
END;

Related

Upsert (merge) for updating record if it exists and inserting otherwise

I am trying to write a DB2 query that allows me to either update a record if it already exists but if it does not exist it should be inserted. I wrote the following query that should accomplish this:
MERGE INTO OA1P.TLZ712A1 AS PC
USING (
SELECT * FROM OA1P.TLZ712A1
WHERE CALENDAR_ID=13 AND
"PACKAGE"='M2108'
) PC2
ON (PC.ID_PACKAGE_CALENDAR=PC2.ID_PACKAGE_CALENDAR)
WHEN MATCHED THEN
UPDATE SET ACT_DATE = '31.12.2021'
WHEN NOT MATCHED THEN
INSERT ("PACKAGE", ACT_DATE, CALENDAR_ID, PREPTA, MIXED) VALUES ('M2108', '31.12.2021', 13, 0, 0)
This query should attempt to check if a record already exists for the selection criteria. Updating a record seems to be working fine but I am not able to get the "WHEN NOT MATCHED" part to work and inserting a new record. Anyone able to provide some assistance?
The table is used to save the activation date of a certain software package. PACKAGE is the reference to the package table containing the name of the package (eg. "M2108"). CALENDAR_ID refers to a system where the software package will be activated. The actual date is stored in ACT_DATE.
Did not manage to get the DDL into SQLFiddle so I have to provide it here:
CREATE TABLE OA1P.TLZ712A1 (
ID_PACKAGE_CALENDAR INTEGER DEFAULT IDENTITY GENERATED BY DEFAULT NOT NULL,
CALENDAR_ID INTEGER,
"PACKAGE" VARCHAR(10) NOT NULL,
ACT_DATE DATE NOT NULL,
PREPTA SMALLINT DEFAULT 0 NOT NULL,
MIXED SMALLINT DEFAULT 0 NOT NULL,
"COMMENT" VARCHAR(60) NOT NULL,
LAST_MODIFIED_PID CHAR(7) NOT NULL,
ST_STARTID TIMESTAMP NOT NULL,
ST_FROM TIMESTAMP NOT NULL,
ST_TO TIMESTAMP NOT NULL,
CONSTRAINT TLZ712A1_PK PRIMARY KEY (ID_PACKAGE_CALENDAR),
CONSTRAINT CALENDAR FOREIGN KEY (CALENDAR_ID) REFERENCES OA1P.TLZ711A1(ID_CALENDAR) ON DELETE RESTRICT,
CONSTRAINT "PACKAGE" FOREIGN KEY ("PACKAGE") REFERENCES OA1P.TLZ716A1(NAME) ON DELETE RESTRICT
);
CREATE UNIQUE INDEX ILZ712A0 ON OA1P.TLZ712A1 (ID_PACKAGE_CALENDAR);
If your goal is to set ACT_DATE to 31.12.2021 if a row is found with PACKAGE = M2108 and CALENDAR_ID = 13 and if no row is found with these values then insert it, then this could be the answer
MERGE INTO OA1P.TLZ712A1 AS PC
USING (
VALUES ('M2108', 13, date '31.12.2021')
) PC2 ("PACKAGE", CALENDAR_ID, ACT_DATE)
ON (PC."PACKAGE", PC.CALENDAR_ID) = (PC2."PACKAGE", PC2.CALENDAR_ID)
WHEN MATCHED THEN
UPDATE SET ACT_DATE = PC2.ACT_DATE
WHEN NOT MATCHED THEN
INSERT ("PACKAGE", ACT_DATE, CALENDAR_ID, PREPTA, MIXED) VALUES (PC2."PACKAGE", PC2.ACT_DATE, PC2.CALENDAR_ID, 0, 0)

Automatic user signature in SQL

Trying to add the user name to a table, but I get dbo as user name
CREATE TABLE username
(
ID int,
Date datetime default CURRENT_TIMESTAMP,
Signatur varchar(200) default CURRENT_USER,
Comment varchar(255),
);
AND THEN
INSERT INTO username
(Comment)
VALUES
('Just some text');
SELECT * FROM username
ORDER BY Date desc;
result
When I replace CURRENT_USER with SYSTEM_USER or SUSER_SNAME() I get the user name DESKTOP-0DM0L90\jokr1
How do I get only jokr1 as user name?
The full username is domain\user; so that's what all of the user/suser etc. commands give you. You can use standard string manipulation techniques to get just one part of the string.
CREATE TABLE username
(
ID int,
Date datetime default CURRENT_TIMESTAMP,
Signatur varchar(200) default right(SYSTEM_USER,len(SYSTEM_USER)-charindex('\',SYSTEM_USER)),
Comment varchar(255),
);
If you ever want to identify the 'user' associated with this, I would recommend storing the SID via:
SELECT SUSER_SID(system_user)

Constraint violation when merging into table

I'm having a staging table and a datawarehouse table, which keep giving me constraint violation. i can't seem to figure out why since DRIVERID and RACEID a combination of those should be unique? How come i get contraint violation - primary key
table
CREATE TABLE QUALIFYING (
QUALIFYID DECIMAL(18,0) IDENTITY NOT NULL,
RACEID DECIMAL(18,0) DEFAULT '0' NOT NULL,
DRIVERID DECIMAL(18,0) DEFAULT '0' NOT NULL,
CONSTRUCTORID DECIMAL(18,0) DEFAULT '0' NOT NULL,
DRIVERNUMBER DECIMAL(18,0) DEFAULT '0' NOT NULL,
DRIVERPOSITION DECIMAL(18,0) DEFAULT NULL,
Q1 VARCHAR(255) UTF8 DEFAULT NULL,
Q2 VARCHAR(255) UTF8 DEFAULT NULL,
Q3 VARCHAR(255) UTF8 DEFAULT NULL,
PRIMARY KEY(QUALIFYID)
);
Staging
CREATE OR REPLACE TABLE STGQUALIFYING(
raceId int DEFAULT '0' NOT NULL,
driverId int DEFAULT '0' NOT NULL,
constructorId int DEFAULT '0' NOT NULL,
driverNumber int DEFAULT '0' NOT NULL,
driverPosition int DEFAULT NULL,
q1 varchar(255) DEFAULT NULL,
q2 varchar(255) DEFAULT NULL,
q3 varchar(255) DEFAULT NULL,
PRIMARY KEY(RACEID, DRIVERID)
);
SQL
MERGE INTO QUALIFYING c
USING STGQUALIFYING n
ON
(n.RACEID = c.RACEID AND n.DRIVERID = c.DRIVERID)
WHEN MATCHED THEN
UPDATE SET
CONSTRUCTORID = n.CONSTRUCTORID, DRIVERNUMBER = n.DRIVERNUMBER, DRIVERPOSITION = n.DRIVERPOSITION, Q1 = n.Q1, Q2 = n.Q2, Q3 = n.Q3
WHEN NOT MATCHED THEN
INSERT (RACEID, DRIVERID, CONSTRUCTORID, DRIVERNUMBER, DRIVERPOSITION, Q1, Q2, Q3) VALUES
(RACEID, DRIVERID, CONSTRUCTORID, DRIVERNUMBER, DRIVERPOSITION, Q1, Q2, Q3);
The EXASolution user manual says:
The content of an identity column applies to the following rules:
If you specify an explicit value for the identity column while inserting a row, then this value is inserted.
In all other cases monotonically increasing numbers are generated by the system, but gaps can occur between the numbers.
and
You should not mistake an identity column with a constraint, i.e. identity columns do not guarantee unique values. But the values are unique as long as values are inserted only implicitly and are not changed manually.
You've put a primary key constraint on your identity column, so it must be unique. Since you are getting duplicates from your merge, either (a) you have, at some point, provided explicit values as in the first bullet above or updated a value manually, and the monotonically increasing sequence has reached a point where it is clashing with those existing values; or (b) there's a bug in their merge. The former seems more likely.
You can look at recently inserted value if you have one, or do a temporary insert of a new row (with merge) to see if it will create a row successfully, and if so whether you already have ID values higher than the one it allocates for that new row. If there are no higher values already, and insert works and merge continues to fail consistently, then it sounds like something you'd need to raise with EXASolution.

ORA-001722 Invalid Number when insert in Oracle SQL

CREATE TABLE the_user( Name VARCHAR(40) not null,
Address VARCHAR(255) not null,
Delivery_address VARCHAR(255),
Email VARCHAR(25) not null,
Phone INTEGER not null,
Status INTEGER not null,
Password VARCHAR(25) not null,
DOB DATE not null,
PRIMARY KEY (Email),
FOREIGN KEY (Status) REFERENCES User_Status (Status_Id),
CONSTRAINT check_Password CHECK (Password > 4)
);
INSERT INTO the_user VALUES (
'Pergrin Took',
'12 Bag end, hobbiton, The Shire, Eriador',
'The address, Dublin',
'ptook#lotr.com',
'8679046',
'001',
'treebeard',
TO_DATE('2013/11/04 14:11:34', 'yyyy/mm/dd hh24:mi:ss')
);
I have the above database in Oracle but when I try to run the insert command I get an ORA-1722 error, Invalid Number. There is a entry in the user_status table which corresponds to the 1 in the insert.
I have been stuck on this for days.
Quotes are not a problem - it will be converted implicitly to numbers as far as they are valid.
Check your constraints:
CONSTRAINT check_Password CHECK (Password > 4)
Here you try to compare string and number -> in this comparison Oracle always tries to cast both as numbers -> password fails and you see an error.
Try to use instead of password e.g. '55' and you will see the row is inserted.
Perhaps you wanted to do this?
CONSTRAINT check_Password CHECK (length(Password) > 4)

JavaDB throws error when boolean is used. why?

This is the table I try to create in JavaDB.
CREATE TABLE USER(
userid INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
displayName VARCHAR(20) not null,
username VARCHAR(15) not null unique,
password VARCHAR(15) not null,
adminrole boolean default false,
lastlogin timestamp default CURRENT TIMESTAMP
);
I get Error code -1, SQL state 42X01: Syntax error: BOOLEAN.
Line 3, column 1
You have to use JavaDB Version 10.7 or later and also upgrade your database to new version too.