i created table with 4column with
create table conversation(
user_name varchar(200),
employer_name varchar(200),
message text,
date_sent timestamp
)
now i want alter date_sent column without remove it i want to set default value of current_timestamp for this column what i must do
You can use
ALTER TABLE conversation ALTER COLUMN
date_sent SET DEFAULT current_timestamp;
create table conversation(
user_name varchar(200),
employer_name varchar(200),
message text,
date_sent timestamp );
ALTER TABLE conversation ALTER COLUMN
date_sent SET DEFAULT current_timestamp;
insert into conversation
(user_name) values
('me');
select * from conversation;
user_name | employer_name | message | date_sent
:-------- | :------------ | :------ | :-------------------------
me | null | null | 2022-04-01 09:38:13.674547
db<>fiddle here
Related
I'm using Microsoft SQL Server Management Studio. I am trying to create this database, but I can not figure out what I'm doing wrong with the timestamp; it has to default now.
This is the Create Table query:
create table users(
id int identity primary key,
username varchar(255) unique,
created_at date, TIMESTAMP default)
For a column date use default getdate()
for a column datetime use default CURRENT_TIMESTAMP
create table users(
id int identity primary key,
username varchar(255) unique,
created_date date DEFAULT GETDATE(),
created_date_time datetime default CURRENT_TIMESTAMP)
GO
✓
insert into users ( username) values ('new user');
GO
1 rows affected
select * from users
GO
id | username | created_date | created_date_time
-: | :------- | :----------- | :----------------------
1 | new user | 2022-05-31 | 2022-05-31 13:19:41.830
db<>fiddle here
This is my code:
CREATE TABLE data
(
event_time DATE,
event_type1 TEXT,
event_type2 TEXT,
event_type3 TEXT,
event_type4 TEXT,
continent TEXT
);
COPY data
FROM '/home/data/data.csv' WITH CSV DELIMITER ';' ;
I get this error:
ERROR: missing data for column "event_type3"
Where: COPY dilans_data, line 11: "2018-01-01 00:07:41;subscribe;2458151268"
1 statement failed.
I do have missing values in some columns but first I would like to import the data after dealing with it.
I tried to add: NULL 'null_string' (NaN, N, 0) but it did not worked.
Can you help me?
You can create the table with the columns for which you have data and then add the other columns after the import.
CREATE TABLE data
(
event_time DATE,
event_type1 TEXT,
event_type2 TEXT
);
✓
insert into data values ('2018-01-01 00:07:41','subscribe','2458151268');
1 rows affected
ALTER TABLE data ADD COLUMN event_type3 TEXT;
ALTER TABLE data ADD COLUMN event_type4 TEXT ;
ALTER TABLE data ADD COLUMN continent TEXT ;
✓
✓
✓
SELECT * FROM data;
event_time | event_type1 | event_type2 | event_type3 | event_type4 | continent
:--------- | :---------- | :---------- | :---------- | :---------- | :--------
2018-01-01 | subscribe | 2458151268 | null | null | null
db<>fiddle here
i have this table salle that has 2 attributes
this is the table that inherits from salle, it's called salleCours and has 3 additional attributes.
when i run the second command in oracle 11g express in sql command line it says under 'under' : missing or invalid option.
i dont know if it's a syntax problem or something else
Create table salle(
Numero varchar(20) primary key,
Videoprojecteur char(1) ) ;
Create table salleCours UNDER salle(
Capacite number(3),
Retroprojecteur char(1),
micro char(1)) ;
You want to define an OBJECT type and then use an object-defined table:
CREATE TYPE salle_type AS OBJECT(
Numero varchar(20),
Videoprojecteur char(1)
) NOT FINAL;
CREATE TYPE salleCours_type UNDER salle_type(
Capacite number(3),
Retroprojecteur char(1),
micro char(1)
);
CREATE TABLE salle OF salle_type (
Numero PRIMARY KEY
);
Then you can insert rows of either type:
INSERT INTO salle VALUES( salle_type( 'abc', 'Y' ) );
INSERT INTO salle VALUES( salleCours_type( 'def', 'Y', 42, 'N', 'X' ) );
And, if you want the values:
SELECT s.*,
TREAT( VALUE(s) AS salleCours_type ).Capacite AS capacite,
TREAT( VALUE(s) AS salleCours_type ).Retroprojecteur AS Retroprojecteur,
TREAT( VALUE(s) AS salleCours_type ).micro AS micro
FROM salle s
Which outputs:
NUMERO | VIDEOPROJECTEUR | CAPACITE | RETROPROJECTEUR | MICRO
:----- | :-------------- | -------: | :-------------- | :----
abc | Y | null | null | null
def | Y | 42 | N | X
db<>fiddle here
I'm practising triggers on Oracle and I'm having troubles with this trigger:
CREATE OR REPLACE TRIGGER MODIFICACIONES_SALARIOS AFTER
UPDATE OF salario ON empleados_pac
FOR EACH ROW
DECLARE
v_username VARCHAR(10);
v_hora VARCHAR2(10);
BEGIN
SELECT user INTO v_username FROM dual;
SELECT to_char(sysdate, 'HH24:MI:ss') INTO v_hora FROM dual;
INSERT INTO audita_salarios (id_emp, salario_antiguo, salario_nuevo, fecha, hora, username) VALUES (id_empleado, :old.salario, :new.salario, sysdate, v_hora, v_username);
END;
I'm doing an historic table where when the salary of the employeer changes the trigger inserts in the historic his ID, the old salary, the new one, the date and the user who changed the salary.
All works perfectly except for the ID insert. Oracle says that the column is not allowed.
I think the problem is because I want to insert an unique ID into a new column where the ID is going to be repeated if the salary changes two or more times. How could I fix this?
I think the problem is because I want to insert an unique ID into a new column where the ID is going to be repeated if the salary changes two or more times. How could I fix this?
Do not have a UNIQUE constraint on audita_salarios.id_emp; instead have a composite UNIQUE constraint on id_emp and fecha (if you need a UNIQUE constraint at all).
Also:
use a virtual column for HORA (or remove that column as it is just duplicating the time component of FECHA);
use :NEW.id_empleado rather than id_empleado in the trigger.
CREATE TABLE empleados_pac (
id_empleado NUMBER(2),
salario NUMBER(10,2)
);
CREATE TABLE audita_salarios (
id_emp NUMBER(2),
salario_antiguo NUMBER(10,2),
salario_nuevo NUMBER(10,2),
fecha DATE,
hora VARCHAR2(8)
GENERATED ALWAYS AS ( CAST( TO_CHAR( fecha, 'HH24:MI:SS' ) AS VARCHAR2(8) ) ),
username VARCHAR2(60),
CONSTRAINT audita_salarios__pk PRIMARY KEY ( id_emp, fecha )
);
CREATE TRIGGER MODIFICACIONES_SALARIOS
AFTER UPDATE OF salario ON empleados_pac
FOR EACH ROW
DECLARE
BEGIN
INSERT INTO audita_salarios (
id_emp, salario_antiguo, salario_nuevo, fecha, username
) VALUES (
:new.id_empleado, :old.salario, :new.salario, sysdate, USER
);
END;
/
INSERT INTO empleados_pac ( id_empleado, salario ) VALUES ( 1, 10 );
UPDATE empleados_pac
SET salario = 20
WHERE id_empleado = 1;
BEGIN
DBMS_SESSION.SLEEP(1);
END;
/
UPDATE empleados_pac
SET salario = 30
WHERE id_empleado = 1;
-- You do not actually need to constraint on the audit table unless you
-- want to enforce that salary updates must be on different times.
ALTER TABLE audita_salarios DROP CONSTRAINT audita_salarios__pk;
UPDATE empleados_pac
SET salario = 40
WHERE id_empleado = 1;
Then:
SELECT * FROM audita_salarios;
Outputs:
ID_EMP | SALARIO_ANTIGUO | SALARIO_NUEVO | FECHA | HORA | USERNAME
-----: | --------------: | ------------: | :------------------ | :------- | :--------------------------
1 | 10 | 20 | 2021-04-01 13:02:42 | 13:02:42 | FIDDLE_XRWUFWRAYDJNIGNCOQFP
1 | 20 | 30 | 2021-04-01 13:02:43 | 13:02:43 | FIDDLE_XRWUFWRAYDJNIGNCOQFP
1 | 30 | 40 | 2021-04-01 13:02:43 | 13:02:43 | FIDDLE_XRWUFWRAYDJNIGNCOQFP
db<>fiddle here
there is table called event that act as parent the child inherit tables of event are special event and hotel event i have created the types as bellow but I'm contuse about how to create tables to these tables in oracle.I have referred most of the currently available solutions within Stack overflow, git hub etc. However, none of these solutions have worked out successfully.
Table types :
Event_t (
EventID:char(5),
EventType:varchar(20),
VenueName:varchar(50),
NoOfGuest:number(10)
) NOT FINAL
HotelEvent_t (
Date:date,
Price:numbr(8,2)
) UNDER Event_t
SpecialEvent_t (
BookingDate:date,
EndDate:date,
MenuNumber:number(2),
Reservation ref Reservation_t
) UNDER event_t
Thank you very much and any suggestion will be greatly appreciated.
Create your types using the correct syntax:
CREATE TYPE Event_t AS OBJECT(
EventID char(5),
EventType varchar(20),
VenueName varchar(50),
NoOfGuest number(10)
) NOT FINAL;
CREATE TYPE HotelEvent_t UNDER Event_t (
datetime date, -- Date is a keyword, try to use a different name.
Price number(8,2)
);
CREATE TYPE SpecialEvent_t UNDER event_t (
BookingDate date,
EndDate date,
MenuNumbers NUMBER(2),
Reservation ref Reservation_t
);
Then you can create an object table:
CREATE TABLE Events OF Event_T(
eventid CONSTRAINT Events__EventID__PK PRIMARY KEY
);
Then you can insert the different types into it:
INSERT INTO EVENTS VALUES(
HotelEvent_T(
'H1',
'HOTEL',
'Venue1',
42,
DATE '0001-02-03' + INTERVAL '04:05:06' HOUR TO SECOND,
123456.78
)
);
INSERT INTO EVENTS VALUES(
SpecialEvent_T(
'SE1',
'SPECIAL',
'Time Travel Convention',
-1,
SYSDATE,
TRUNC(SYSDATE),
0,
NULL
)
);
and get the data out of it:
SELECT e.*,
TREAT( VALUE(e) AS HotelEvent_T ).datetime AS datetime,
TREAT( VALUE(e) AS HotelEvent_T ).price AS price,
TREAT( VALUE(e) AS SpecialEvent_T ).bookingdate AS bookingdate,
TREAT( VALUE(e) AS SpecialEvent_T ).enddate AS enddate,
TREAT( VALUE(e) AS SpecialEvent_T ).menunumbers AS menunumbers
FROM Events e;
Which outputs:
EVENTID | EVENTTYPE | VENUENAME | NOOFGUEST | DATETIME | PRICE | BOOKINGDATE | ENDDATE | MENUNUMBERS
:------ | :-------- | :--------------------- | --------: | :------------------ | --------: | :------------------ | :------------------ | ----------:
H1 | HOTEL | Venue1 | 42 | 0001-02-03 04:05:06 | 123456.78 | null | null | null
SE1 | SPECIAL | Time Travel Convention | -1 | null | null | 2020-03-30 21:11:22 | 2020-03-30 00:00:00 | 0
db<>fiddle here
The typical way to create these tables in Oracle would be:
create table event_t (
event_id char(5) primary key not null,
event_type varchar2(20),
venue_mame varchar2(50),
no_of_guest number(10)
);
create table hotel_event_t (
event_date date,
price number(8,2),
event_id char(5),
constraint fk1 foreign key (event_id) references event_t (event_id)
);
create table special_event_t (
booking_date date,
end_date date,
menu_number number(2),
reservation_id char(5),
constraint fk2 foreign key (reservation_id)
references reservation_t (reservation_id),
event_id char(5),
constraint fk3 foreign key (event_id) references event_t (event_id)
);