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

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));

Related

How to get current user_id in Oracle Apex

I have table users; there are columns (id, first_name, last_name).
I want to display last_name||','||first_name and return id for user who is logged in.
How can I achieve that?
Thanks in advance
That depends on what you call "ID" and what you store in there.
Basically, you have to have a column which contains username of user that connects to your Apex application. Let's presume that it is a new column in your table:
create table users
(id number,
first_name varchar2(20),
last_name varchar2(20),
--
username varchar2(20) --> this
);
Then you can reference via :APP_USER as
select last_name ||', '|| first_name
from users
where username = :APP_USER;
:APP_USER value is what you see in Apex upper right corner, your username. If you know that, you can fetch anything you want from the users table.

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)

SQL Server : create a foreign key with a condition

I'm designing a new database for a company, trying to keep strict constraints with foreign keys etc for integrity. I have a table [Member] which holds companies on the system. This table has a column of [internalContact] for the user in our company who deals with this member which has a foreign linked to the users table by user id.
What I would like to know is if it is possible to assign a condition to the foreign key, since the users table contains internal and external users. ie. for the field to only accept a user id where the user type is 5. Can this be done, or can I only control this in my application code?
Thanks
You can use a check constraint for this.
(The code is untested some syntax errors will be in there)
CREATE TABLE Member
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
InternalContactId
CONSTRAINT chk_Person CHECK (isInternalUser(internalContactId) > 0)
)
ALTER TABLE Member
ADD FOREIGN KEY (InternalContacId)
REFERENCES Persons(P_Id)
Then just create a function isInternalUser that returns 1 if user in ok to be an internal contact
CREATE FUNCTION isInternalUser ( #userId int(10) )
RETURNS int
AS
BEGIN
DECLARE #tmp int
SELECT #tmp = count(*)
FROM users
WHERE userId = #UserId and <check to see if user is internal>
RETURN(#CtrPrice)
END
GO

SQL Server 2008 stored procedure for a table having two foreign keys and both of them is a composite key

I have the following tables :
create table ApartmentInfo(
ApartmentId int primary key identity,
ApName nvarchar(50))
create table [User](
UserId int primary key identity,
FirstName nvarchar(50),
LastName nvarchar(50),
Username nvarchar(50),
[Password] nvarchar(50),
[Description] nvarchar(200))
create table ApUser(
ApartmentId int foreign key references ApartmentInfo(ApartmentId),
UserId int foreign key references [User](UserId),
primary key(ApartmentId,UserId))
Summary of the usage is: suppose I have 10 apartments in my Apartmentinfo table and 3 users in the [User] table.
Now I want to write a stored procedure such that:
every UserId has all 10 ApartmentId's, and whenever a new apartment is created in ApartmentInfo table it will also be added in ApUser table again having all 3 userId's.
And if a new user is created in User table then it will also have all the 10 ApartmentId's related with it in ApUser table.
Thanks in advance, I am new to SQL Server and I don't know how it can be done or not but, if it is possible then please let me know, I will be grateful to you, thanks.
If you're not dead set on using a stored procedure this use case seems like an excellent fit for server side triggers:
IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[trigger_insert_apartment]'))
DROP TRIGGER [dbo].[trigger_insert_apartment]
GO
CREATE TRIGGER trigger_insert_apartment
ON ApartmentInfo FOR INSERT
AS INSERT ApUser(ApartmentId, UserId ) SELECT i.ApartmentId, u.UserId FROM [User] AS u, inserted AS i
GO
IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[trigger_insert_user]'))
DROP TRIGGER [dbo].[trigger_insert_user]
GO
CREATE TRIGGER trigger_insert_user
ON [User] FOR INSERT
AS INSERT ApUser(ApartmentId,UserId ) SELECT a.ApartmentId, i.UserId FROM ApartmentInfo AS a, inserted AS i
GO
These two triggers will insert all apartments or users into the apinfo table when you add a new user or apartment, and if I understood your question that was what you wanted?
To address your question about what you'd do to associate apartments with users IF YOU WANTED TO LATER...
If you're restricting which apartments a user has access to view, I would NOT associate them individually to every user. BUT, if they're manually adding a "watch" on an apartment in your application, then this is ok to do. If you want to only show them a certain set of apartments, try to group them in a logical way if you can. So maybe have an ApartmentGroup lookup table:
ApartmentGroup(ApartmentGroupId (Int), ApartmentGroupName varchar(50))
and assign an ApartmentGroupId to each apartment. Then you can have a join table for ApUser and ApartmentGroup. That way you're not associating every apartment with every user.
However, if you want to add the apartment to each user still, then in your InsertApartment stored procedure, just insert it into the ApUser table as well.

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.