Setting the default value to the current user in Oracle - sql

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.

Related

No row selected

SQL> create table artwork
2 (
artwork_id number(7) NOT NULL,
barcode char(20),
title char(20),
description char(50),
PRIMARY KEY (artwork_id)
);
Table created.
SQL> select * from artwork;
no rows selected
I created the table but it showing me this error dont know. Why table it not showing?
I would expect the create to look like this:
create table artwork (
artwork_id number primary key,
barcode char(20),
title varchar2(20),
description varchar2(50)
);
Notes:
There is no need to have number(7). You can specify the length, but it is not necessary.
For title and description you definitely want varchar2(). There is no reason to store trailing spaces at the end of a name.
That may be the same for barcode, but because it might always be exactly 20 characters or trailing spaces might be significant, you might leave it as char().
The primary key constraint can be expressed in-line. There is no need for a separate declaration.
You probably simply want something like
create table artwork
(
artwork_id number(7) NOT NULL,
barcode varchar2(20),
title varchar2(20),
description varchar2(50),
PRIMARY KEY (artwork_id)
);
insert into artwork values (0, 'barcode', 'fancytitle', 'somedescription');
insert into artwork values (1, 'barcode1', 'fancytitle1', 'somedescription1');
select * from artwork;
This creates a table "ARTWORK", inserts 2 rows in it and then selects all rows currently in the table.
An empty table contains no data, with the create table-statement you only define the bucket of data, you have to fill the bucket as well with items.
I'd also recommend a auto increment column (oracle 12c) or a trigger/sequence to increment the id automatically. But that's something to read later :)

Creating a table in SQL using Oracle 11G

I am new to learning SQL and have been struggling to create a table for an assignment. These are the requirements:
Create a new table to track the Library location.
LIBRARY (lib_id, lib_name, lib_address, lib_city, lib_state, lib_zip)
LIB_ID is the library id – it is an auto generated number. (you should create a sequence number called lib_id_seq, start with 1001 and increment by 1.)
LIB_ID is the primary key.
LIB_NAME, LIB_ADDRESS, and LIB_CITY is between 1 and 35 characters.
LIB_STATE is 2 characters – default to TX.
LIB_ZIP is 5 numbers. Check for one of the following zip codes – 75081, 75080, 75082, 75079, 75078
And this is what I have written out so far:
CREATE TABLE LIBRARY
(
LIB_ID INT(4),
LIB_ADDRESS VARCHAR(35),
LIB_CITY VARCHAR(35),
LIB_STATE VARCHAR(2) DEFAULT ‘TX’,
LIB_ZIP INT(5) CHECK (Frequency IN ('75078', ‘75079', '75080', '75081', ‘75082’))
PRIMARY KEY(LIB_ID)
);
CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;
I keep getting errors, but am not sure what I need to fix.
For oracle (Kid Tested unsure if SO approved)...
use varchar2 instead of varchar
use Number instead of int
added constraint syntax (named them)
adjusted apostrophe's (Removed) instead of whatever the heck you had in some of them :P (It's a numeric field shouldn't be using text apostrophes!)
personally I wouldn't name a table library as that's a reserved word
I woudln't use a numeric Zip code as we will never do math on a zipcode.
.
.
CREATE TABLE LIBRARY (
LIB_ID Number(4),
LIB_ADDRESS VARCHAR2(35),
LIB_CITY VARCHAR2(35),
LIB_STATE VARCHAR2(2) DEFAULT 'TX',
LIB_ZIP NUMBER(5),
CONSTRAINT Lib_ZIP_CON CHECK (LIB_ZIP IN (75078, 75079, 75080, 75081, 75082)),
CONSTRAINT LIB_ID_PK PRIMARY KEY(LIB_ID)
);
CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;
This works for SQL Server. You need to modify the syntax accordingly for the concerned db.
CREATE TABLE LIBRARY
(
LIB_ID INTEGER PRIMARY KEY,
LIB_ADDRESS VARCHAR(35),
LIB_CITY VARCHAR(35),
LIB_STATE VARCHAR(2) DEFAULT 'TX',
LIB_ZIP INTEGER,
CHECK( LIB_ZIP IN ('75078', '75079', '75080', '75081', '75082') )
);
CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;
For learning how to create tables and constraints check this link on w3schools as you seem to be a beginner.
http://www.w3schools.com/sql/sql_primarykey.asp

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.

Sql create table check date

On creating table i needed to add a check statement for current date should be created date cell. So for example
create table own_departments
(
id number(4) primary key,
name varchar2(30),
num_of_emps number(4) default '0',
est_date date,
check(est_date < sysdate)
);
This check gives errors.
Is anyone know how to check DATE format???
If you are using SQL Server then you will need to create a table constraint...
ALTER TABLE _departments WITH CHECK ADD CONSTRAINT CK_DateGreaterThan CHECK (([est_date]>YOUR_DATE))
GO
ALTER TABLE _departments CHECK CONSTRAINT CK_DateGreaterThan
GO
If you're using SQL Server / Oracle / MS Access, then it should be something like this:
create table own_departments
(
id number(4) primary key,
name varchar2(30),
num_of_emps number(4) default '0',
**est_date date check(est_date < sysdate)**
);

Autoincrement Primary key in Oracle database

I would like to achieve an identity or auto-incrementing value in a column ala SQL Server:
CREATE TABLE RollingStock
(
Id NUMBER IDENTITY(1,1),
Name Varchar2(80) NOT NULL
);
How can this be done?
As Orbman says, the standard way to do it is with a sequence. What most people also do is couple this with an insert trigger. So, when a row is inserted without an ID, the trigger fires to fill out the ID for you from the sequence.
CREATE SEQUENCE SEQ_ROLLINGSTOCK_ID START WITH 1 INCREMENT BY 1 NOCYCLE;
CREATE OR REPLACE TRIGGER BI_ROLLINGSTOCK
BEFORE INSERT ON ROLLINGSTOCK
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
WHEN (NEW.ID IS NULL)
BEGIN
select SEQ_ROLLINGSTOCK_ID.NEXTVAL
INTO :NEW.ID from dual;
END;
This is one of the few cases where it makes sense to use a trigger in Oracle.
If you really don't care what the primary key holds, you can use a RAW type for the primary key column which holds a system-generated guid in binary form.
CREATE TABLE RollingStock
(
ID RAW(16) DEFAULT SYS_GUID() PRIMARY KEY,
NAME VARCHAR2(80 CHAR) NOT NULL
);