why liquibase run automatically the rollback sql statement - liquibase

I'm learning liquibase, I'm trying to write an sql changset that allows the possibility to rollback later on.
Here is my syntax:
--changeset me:1
create table DEPARTMENT (id SERIAL not null, name varchar(50) not null, person_id int, primary key (id), constraint person_foreign_key foreign key (person_id) references PERSON(id));
--rollback
drop table DEPARTMENT;
When running liquibase:update, the table department is created, but it is deleted by the second query.
what is the correct syntax to have the rollback statement without the removal of the department table?
I'm using liquibise version 4.7.1

It seems that the drop statement should be on the same line as --rollback.
Here is the changeset file:
-- liquibase formatted sql
-- changeset me:1
create table DEPARTMENT (
id SERIAL not null,
name varchar(50) not null,
person_id int,
primary key (id),
constraint person_foreign_key foreign key (person_id) references PERSON(id)
);
--rollback drop table DEPARTMENT;

Related

SQL The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Score_Gebruikersnaam"

I have a create script for my SQL database (see below). Everything works fine except when I run the INSERT INTO Scores() I get an error.
The error I get is:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Score_Gebruikersnaam". The conflict occurred in database "gamescores", table "dbo.Players", column 'gebruikersnaam'
I don't understand what I'm doing wrong so please help me :)
I already tried to drop the database first and then run the rest of the script but that didn't help. I think something went wrong with the foreign key...
Thanks!
USE master;
GO
DROP DATABASE IF EXISTS [gamescores];
CREATE DATABASE gamescores;
GO
USE gamescores;
GO
SET DATEFORMAT dmy;
CREATE TABLE [Players]
(
[gebruikersnaam] VARCHAR(75) NOT NULL,
[voornaam] VARCHAR(75) NOT NULL,
[achternaam] VARCHAR(75) NOT NULL,
[emailadres] VARCHAR(75) NOT NULL,
[geboortdatum] DATE NOT NULL
);
CREATE TABLE [Scores]
(
[scoreID] INT IDENTITY(1,1) NOT NULL,
[gebruikersnaam] VARCHAR(75) NOT NULL,
[aantalScore] INT NOT NULL,
[datum] DATE NOT NULL
);
ALTER TABLE [Players]
ADD CONSTRAINT [PK_Speler]
PRIMARY KEY (gebruikersnaam);
ALTER TABLE [Scores]
ADD CONSTRAINT [PK_Score]
PRIMARY KEY (scoreID);
ALTER TABLE [Players]
ADD CONSTRAINT [AK_Speler_Emailadres]
UNIQUE (emailadres)
ALTER TABLE [Scores]
ADD CONSTRAINT [FK_Score_Gebruikersnaam]
FOREIGN KEY (gebruikersnaam) REFERENCES Players(gebruikersnaam);
GO
INSERT INTO Players([gebruikersnaam], [voornaam], [achternaam], [emailadres], [geboortdatum])
VALUES ('apraundlin1', 'Angelle', 'Praundlin', 'apraundlin1#mapy.cz', '15-9-1997'),
('rnoore3', 'Rebekah', 'Noore', 'rnoore3#vk.com', '9-10-1987'),
('nplevinh', 'Nicolais', 'Plevin', 'nplevinh#mediafire.com', '18-3-2001');
-- SCORE table vullen
INSERT INTO Scores([gebruikersnaam], [aantalScore], [datum])
VALUES ('rsprasen0', 551, '15-5-2021'),
('fwhawell8', 309, '8-4-2021'),
('rgravett9', 1063, '16-11-2021');
You got an error because the database schema, though a foreign key, enforces that a player referenced in the scores table must first exist in the players table. Add the player to the players table, before trying to update their score in the scores table.

Mutating table trigger error upon deleting from a table

I have these table definitions
CREATE TABLE EMPLOYEE(
EmployeeID NUMBER(4),
Name VARCHAR2(20),
Hiredate DATE NOT NULL,
Gender VARCHAR(1),
DateOfBirth DATE NOT NULL,
Salary NUMBER(8,2),
Commission NUMBER(8, 2),
DName VARCHAR(20),
PName VARCHAR(20),
Phone NUMBER(8) NOT NULL,
GLetter VARCHAR(1),
CONSTRAINT EMPLOYEE_EMPLOYEEID_PK PRIMARY KEY(EMPLOYEEID),
CONSTRAINT EMPLOYEE_DNAME_FK FOREIGN KEY(DName) REFERENCES DEPARTMENT(DName) ON DELETE CASCADE,
CONSTRAINT EMPLOYEE_PNAME_FK FOREIGN KEY(PName) REFERENCES POSITION(PName) ON DELETE CASCADE,
CONSTRAINT EMPLOYEE_GLETTER_FK FOREIGN KEY(GLetter) REFERENCES GRADE(GLetter) ON DELETE CASCADE,
CONSTRAINT GENDER_CK CHECK (Gender='M' or Gender='F')
);
CREATE TABLE LOGIN(
Username VARCHAR(20),
Password VARCHAR(20),
EmployeeID NUMBER(4),
CONSTRAINT LOGIN_USERNAME_PK PRIMARY KEY(Username),
CONSTRAINT LOGIN_EMPLOYEEID_FK FOREIGN KEY(EmployeeID) REFERENCES EMPLOYEE(EmployeeID) ON DELETE CASCADE
);
CREATE SEQUENCE TRANSACTION_SEQ START WITH 6;
CREATE TABLE TRANSACTION(
TransactionID NUMBER(4) DEFAULT TRANSACTION_SEQ.NEXTVAL,
TransactionDate DATE,
Username VARCHAR(20),
EmployeeID NUMBER(4),
CONSTRAINT TRANSACTION_TRANSACTIONID_PK PRIMARY KEY(TransactionID),
CONSTRAINT TRANSACTION_USERNAME_FK FOREIGN KEY(Username) REFERENCES LOGIN(Username) ON DELETE CASCADE,
CONSTRAINT TRANSACTION_EMPLOYEEID_FK FOREIGN KEY(EmployeeID) REFERENCES EMPLOYEE(EmployeeID) ON DELETE CASCADE
);
and this trigger
CREATE OR REPLACE TRIGGER EMPLOYEE_TRANSACTION
BEFORE INSERT OR UPDATE OR DELETE ON EMPLOYEE
FOR EACH ROW
DECLARE
ID NUMBER(4);
USR VARCHAR(20);
BEGIN
SELECT LOWER(USER)
INTO USR
FROM DUAL;
SELECT EMPLOYEEID
INTO ID
FROM LOGIN
WHERE USERNAME = USR;
INSERT INTO TRANSACTION VALUES(DEFAULT, SYSDATE, USR, ID);
END;
/
my problem arises when trying to delete an employee.
basically, the trigger finds out the employeeid of the user who is making the changes and inserts it and other values to the transaction table. i get this error:
Error report -
ORA-04091: table ---.LOGIN is mutating, trigger/function may not see it
ORA-06512: at "---.EMPLOYEE_TRANSACTION", line 9
ORA-04088: error during execution of trigger '---.EMPLOYEE_TRANSACTION'
is oracle taking into account the possibility that i am deleteing an employee whose id (which is going to be deleted from table LOGIN) is that of the oracle USER?
any solutions? thank you very much!
If we assume that this fiddle is a reproducible test case that demonstrates your problem (note that I had to make several changes to your code in order to be able to do things like inserting the test data), the issue is that the foreign key constraint on the login table is defined to do a cascade delete (on delete cascade). That means that when you are deleting a row from employee, login will be mutating (Oracle is in the middle of deleting the child row). Thus you can't query it from within a trigger on employee.
Depending on exactly what you are hoping to accomplish, you have a few options
Don't query the login table and don't store the employeeID in the transaction table. If you have the username that performed the operation in the transaction table, you can always look up their employeeID.
Don't use triggers to populate the transaction table. If you have a stored procedure that deletes an employee, it makes more sense for that procedure to do the work of logging the transaction.
Don't define your constraints as on delete cascade. That would mean, though, that you'd need to delete the login row prior to deleting the employee row. Here is an example where we remove the on delete cascade from the LOGIN_EMPLOYEEID_FK constraint and add a statement to delete the associated login row.

cycles or multiple cascade paths. SQL Error?

I'm trying to create Employee database for practice. I'm getting this error:
Introducing FOREIGN KEY constraint 'fk_dno' on table 'Employee' may
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or
ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints ?
What is causing this error?
create table Department(
Dno int not null,
Name_d varchar(30),
primary key(Dno)
)
create table Employee(
E_id int not null,
F_name varchar(30),
L_name varchar(30),
B_date date,
address_e varchar(30),
salary int,
Sex varchar(8),
Cnic varchar(15),
Email varchar(50),
start_date_e date,
primary key (E_id)
)
alter table Department add Mgr_id int
alter table Department add constraint fk_mgr Foreign key(Mgr_id) references Employee(E_id) on update cascade on delete set null
alter table Employee add Dno int
alter table Employee add constraint fk_dno Foreign key(Dno) references Department(Dno) on update cascade on delete set null
Having mutual foreign keys with ON UPDATE CASCADE creates a cycle because, if you modify a row from one, it searches for rows to delete / modify on the other table, which triggers modification in the other table (the one that originated the change) and so on.
Change the ON UPDATE and ON DELETE options for your FK. Evaluate if you need them mutually referencing fields (you might be designing with the wrong attributes)

ORA-01748: only simple column names allowed here in Oracle

What I am trying to do ?
I am trying to create two tables and at the same time i am trying to link them together using foreign and primary keys. However I successfully create my parent table ( Student with primary key ) but failed to create child table ( Attendence with foreign key ).
What is the problem ?
I get the following error while creating Attendence table:
ERROR at line 5: ORA-01748: only simple column names allowed here
My code:
Student table:
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Attendence table:
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk Attendence.ST_ROLLNO foreign key references Student(ST_ROLLNO)
);
Your foreign key constraint syntax is wrong; it should be:
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
You are preceding the FK column name with the table name, which is wrong in itself, but also have it in the wrong place.
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Table STUDENT created.
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
);
Table ATTENDENCE created.
According to oracle documentation,
ORA ERR
ORA-01748 only simple column names allowed here
The following is the cause of this error:
This SQL statement does not allow a qualified column name, such as
username.table.column or table.column.
Action you can take to resolve this issue: Remove the qualifications
from the column and retry the operation.
In your case, you are trying to refer to the table name while defining a constraint -
Attendence.ST_ROLLNO - WRONG.
It must contain a simple name without the table name or schema name.

SQL Management Studio 2012 Foreign Key Is A Mess

I am trying to add a foreign key to a table. I have created two tables.
CREATE TABLE madeupbusiness.staff
(
staffnum int NOT NULL,
forename varchar(30) NOT NULL,
surname varchar(30) NOT NULL,
meeting int NOT NULL,
PRIMARY KEY (staffnum),
)
GO
meeting should use the PK from from the meeting table to create a FK :
CREATE TABLE madeupbusiness.meeting
(
meetingnum int NOT NULL,
room varchar(30) NOT NULL,
PRIMARY KEY (meetingnum),
)
GO
To create the foreign key I run this query
ALTER TABLE madeupbusiness.staff
WITH CHECK
ADD CONSTRAINT FK_staff_meetingnum FOREIGN KEY (meetingnum)
REFERENCES madeupbusiness.meeting(meetingnum)
ON DELETE CASCADE
ON UPDATE CASCADE
;
GO
The query runs but when I create a database diagram there is a square loop the staff table from the staffnum key back onto it. Sorry but I don't really know how to describe it. There is no relationship between the two tables. What am I doing wrong?
I have tried to add the relationship from design view but the foreign key table is greyed out.
If you can rebuild the Table do this:
CREATE TABLE madeupbusiness.meeting
(meetingnum int NOT NULL PRIMARY KEY REFERENCES madeupbusiness.meeting(YourColumnYouWantItShouldBeReferenced),
room varchar(30) NOT NULL);
GO