Automatic user signature in SQL - 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)

Related

Userlogin time update

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;

SQL Server - How to insert a hashed password to a table?

Sorry for my ignorance, but I want to store hashed password in my database,
How can use the HASHBYTES method to store hashed password in Users table ?
CREATE TABLE [Users](
EmailAddress NVARCHAR(320) UNIQUE,
UserID INT IDENTITY(1,1) PRIMARY KEY,
UserPassword NVARCHAR(32), -- I Edited the length
FirstName VARCHAR(256) not null,
LastName VARCHAR(256) not null,
MobileNumber BIGINT,
)
--I checked and found this is how to hash a password
declare #afterhash varbinary(256) = HASHBYTES('SHA2_256', 'P#ssw0rd')
But how do I combine them both ?
As mentioned, I don't understand the problem here. Just use HASHBYTES in your parametrised INSERT:
INSERT INTO dbo.Users (EmailAddress, UserPassword, FirstName, LastName, MobileNumber)
VALUES(#EmailAddress, HASHBYTES('SHA2_256',#Password), #FirstName, #LastName, #MobileNumber);
Side Note: As I mentioned in my other answer, bigint isn't the right choice for a telephone number. Phone Numbers can start with a 0 and contain other characters from digits. A value like '01234567890' would be changed to 1234567890, a number like '+441234567890' would be changed to 441234567890, and a number like '(01234) 567890' would fail to INSERT completely
declare #Users table (passwordColumn NVARCHAR(32));
insert #Users values (HASHBYTES('SHA2_256','Password#1234.'));
select HASHBYTES('SHA2_256', passwordColumn) from #Users;

Oracle/Apex: adding an internal user to a table?

I am trying to add an internal APEX user to a user_Table that I created and I am unsure how to do this.
Using this video provided by our university: http://youtu.be/69rVjyepf5g
I was wondering how I would go about adding this new user to the following table:
CREATE TABLE user_table (
userId NUMBER NOT NULL,
userName VARCHAR2(50) NOT NULL,
emailAddress VARCHAR2(150) NOT NULL,
password VARCHAR(20) NOT NULL, PRIMARY KEY(userId)
);
I know the video shows lines such as:
DBMS_OUTPUT.PUT_LINE('Current user is ' || currentUser);
to display what user is currently in use in the system.
I am looking for a way to add a selected interal user to my user table for use in my application.
Any help would be much appreciated, thank you.
I managed to get this working by creating the user as an object:
CREATE OR REPLACE TYPE user_Object AS OBJECT(
userId NUMBER,
userName VARCHAR2(30),
emailAddress VARCHAR2(100),
MAP MEMBER FUNCTION get_userId RETURN NUMBER ) NOT FINAL;
Then creating a table referencing that object:
CREATE TABLE user_table OF user_Object;
Then I inserted the data into the table
INSERT INTO user_table VALUES('1','USER1','user#email.com');
And created a user with the name 'USER' as per the original linked video!
I then use the following query to select the friends that are saved under that specific user
ELECT DISTINCT B.userfriend.userName FROM friends_table B
WHERE B.friendId = (select C.userId FROM user_table C WHERE C.userName =(SELECT V('APP_USER') FROM DUAL));

Setting the default value to the current user in Oracle

I am trying to create a new table in Oracle 11g where the default value for a column is the currently logged in user. I need to do this is for logging purposes.
CREATE TABLE tracking (
pk NUMBER(19,0) PRIMARY KEY,
description VARCHAR2(50),
created_by VARCHAR2(128) DEFAULT CURRENT_USER
);
How can I write the DEFAULT CURRENT_USER section so it will take the current Oracle user as the default value? I know I could use a trigger, but I shouldn't have to...
You need to use USER not CURRENT_USER:
CREATE TABLE tracking (
pk NUMBER(19,0) PRIMARY KEY,
description VARCHAR2(50),
created_by VARCHAR2(128) DEFAULT USER
);
SQL Fiddle
The maximum length of a user is 30, so you could reduce this and I would increase the size of your DESCRIPTION column unless you're very sure that everything will come in at less than 51 characters.
Try user instead:
CREATE TABLE tracking (
pk NUMBER(19,0) PRIMARY KEY,
description VARCHAR2(50),
created_by VARCHAR2(128) DEFAULT USER
);
By the way, I also think created_at for the datetime is another useful default column.

Auto increment an ID with a string prefix in oracle sql?

I have created the following table and I wish to have the userID auto-increment whenever there is a row added to the database. However I would like the ID to be formatted as 000001 for example as below, since there are a few tables and it would be ideal to give each ID a string prefix:
userID
----------
user000001
user000002
user000003
CREATE TABLE UserTable (
userID VARCHAR(20),
username VARCHAR(250) NOT NULL,
firstName VARCHAR(250) NOT NULL,
lastName VARCHAR(250) NOT NULL,
CONSTRAINT pkUserID
PRIMARY KEY (userID),
CONSTRAINT uniUsername
UNIQUE (username)
);
You would have to use a combination of trigger and sequence as shown in the code below:
CREATE SEQUENCE CREATE SEQUENCE usertable_seq
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
/
CREATE OR REPLACE TRIGGER usertable_trigger
BEFORE INSERT ON UserTable
FOR EACH ROW
BEGIN
SELECT 'user' || to_char(usertable_seq.NEXTVAL, '000099')
INTO :new.userID
FROM dual;
END;
/
The prefix user is absolute pointless because it is attached to every ID. Drop it and use an ID NUMBER only. Plus, follow Jugal's advice.