How to get current user_id in Oracle Apex - sql

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.

Related

Is there a way to search one table for certain values, and if successfully found, then update a different table?

Good afternoon all,
This is my first time using SQL, so forgive me for being new. I have three different tables that I've created:
CREATE TABLE employee
(ID int,
fname text,
lname text,
age int,
hiredate date);
CREATE TABLE address
(ID int,
address1 text,
address2 text,
city text,
state text,
zip text);
CREATE TABLE contact
(ID int,
cellphone text,
homephone text,
email text);
I have instructions that I'm supposed to update a person's cellphone number in the contact table by "using fname and lname in where clause".
I know how to directly update the person's phone number in the table, but using an IN and WHERE in conjunction with UPDATE between two different tables is just really stumping me.. is there some way to do this that doesn't involve any kind of elaborate solution?
My line of thinking involved coming up with some kind of if-then solution. i.e. IF I can search table employee for a specific fname and lname and find it, THEN update the contact cellphone number with the new number I designate. But.. I have no idea if I can even do if-then statements like that. Anyone have any suggestions?
Thank you for your time!
This uses an IN CLAUSE, because there could be more than one ID with the same name combination.
UPDATE contact
SET cellphone = '1234567890'
WHERE ID IN ( SELECT ID FROM employee WHERE fname = 'A' AND lname = 'B');
update contact
set cellphone = 'xxx-xxx-xxxx', homephone = 'xxx-xxx-xxxx'
where ID = (
select ID from employee
where fname = 'Bob' and lname = 'Jones'
);
This does assume only one employee will match, otherwise it will cause an error (which is quite often what you want to happen.)

How do I populate a column based off two other columns upon insert

I just created my own PostgreSQL database.
I have a Person table with a first_name, last_name, and full_name column (also a person_id auto incrementing primary key)
If I insert as such:
INSERT INTO Person
(first_name, last_name)
VALUES('Michael', 'Jordan');
What's the best approach to automatically populate full_name column as first_name + last_name ('Michael Jordan')?
You can create a generated column:
alter table person
add full_name varchar(255) generated always as (concat(firstname, ' ', lastname));

SQL constraint E-Mail

i have an assignment that i have to do in SQL Server Manager.
I have a database, and a table named User. Under user there are a column named e-mail.
The assignment is to create a new constraint to that table that affect the column e-mail.
There has to be only one '#' and minimum one '.'
It is not allowed to be any special characters such as ( !, ", #, ¤, %, etc.) <- this do not include the '#' and '.'
I've tried some different things but cant seem to make it work. Also it should be noticed that I am a beginner.
Thanks for your help
Alternatively you can create your table with constraint, like this which will check if all the given conditions satisfies.
Please change table data with your's
If table not Exist
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Email varchar(255),
CHECK (len(Email) - len(replace(Email,'#',''))=1 AND len(Email) -
len(replace(Email,'.',''))=1 AND CHARINDEX('!',Email)!>0 AND
CHARINDEX('#',Email)!>0 AND CHARINDEX('%',Email)!>0 AND
CHARINDEX('¤',Email)!>0 AND CHARINDEX('"',Email)!>0)
);
Some Rejected Inputs
some#some.!%#¤com
some#some.!%#com
some#some.%#com
some#some.#com
some#some#com
!some#some#com
etc...
Some Excepted Inputs
some#some.com
some222#some.com
harry_porter#some.com
332#some.com
etc....
If you have already a table then
GO
ALTER TABLE [dbo].[Yourtablename] WITH CHECK ADD Constraint EmailConstraint CHECK (((len([Email])-
len(replace([Email],'#','')))=(1) AND (len([Email])-
len(replace([Email],'.','')))=(1) AND charindex('!',[Email])<=(0) AND
charindex('#',[Email])<=(0) AND charindex('%',[Email])<=(0) AND
charindex('¤',[Email])<=(0) AND charindex('"',[Email])<=(0)))
GO

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

Error connecting to SQL database I have created with a user I have created

I have some problems with connecting to a databases using the users i have created.Firstly i created the users like this:
create user util1 identified by user1;
create user util2 identified by user2;
create user util3 identified by user3;
create user util4 identified by user4;
create user util5 identified by user5;
create user util6 identified by user6;
create user util7 identified by user7;
create user util8 identified by user8;
grant dba to util1,util2,util3,util4,util5,util6,util7,util8;
select lpad(' ', 2*level) || granted_role "User, his roles and privileges"
from (select null grantee, username granted_role from dba_users where username like upper('%UTIL%')
union
select grantee, granted_role from dba_role_privs
union
select grantee, privilege from dba_sys_privs)
start with grantee is null
connect by grantee = prior granted_role;
This is working fine.But when i try to create a table using "util1"'s rights it doesn't work:
connect util1/user1 AS SYSDBA;
create table pr1(
cod_subansamblu number(4),
denumire varchar2(20) not null,
cantitate number(7,3),
UM varchar2(3),
pret_unitar number(9,2),
cod_ansamblu number(4),
cod_sectie number(4) not null,
constraint pr1_cod_subansamblu_pk primary key(cod_subansamblu),
constraint pr1_UM_chk check(UM in ('BUC','ML','MP','MC','SET'))
);
I really don't know what is going wrong. Help will be greatly appreciated.
The error message I receive is:
Last Execution Details
Results
Statistics Plan
ORA-00900: invalid SQL statement
There is nothing wrong with the CREATE TABLE statement:
SQL> create table pr1(
cod_subansamblu number(4),
denumire varchar2(20) not null,
cantitate number(7,3),
UM varchar2(3),
pret_unitar number(9,2),
cod_ansamblu number(4),
cod_sectie number(4) not null,
constraint pr1_cod_subansamblu_pk primary key(cod_subansamblu),
constraint pr1_UM_chk check(UM in ('BUC','ML','MP','MC','SET'))
); 2 3 4 5 6 7 8 9 10 11
Table created.
SQL>
So it's hard to understand why it throws ORA-00900 when you run it. What client are you using to run it? SQL*Plus? An IDE like TOAD? Can you provide a cut'n'paste of the complete session so we can see exactly what's going on?
The other thing to bear in mind is, if you connect using as sysdba you login to the database as SYS, regardless of whether you specify a user in the CONNECT string:
SQL> conn apc/password as sysdba
Connected.
SQL> sho user
USER is "SYS"
SQL>
(This only works if you are logged into the OS as the oracle account or other appropriately privileged user).
So you may think you are running your create table statement as UTIL1 but you would in fact be running it as SYS, which is not a good idea. In fact it is bad practice.