Related
I am working on a trigger that is supposed to block an insert when #verkoperstatus is 0; this does function, but for some reason it also stops the insert when #verkoperstatus is 1. What could be the root cause behind this?
CREATE TRIGGER [dbo].[verkoper_check] ON [dbo].[Verkoper]
FOR INSERT,UPDATE
AS
BEGIN
DECLARE #verkoperstatus bit
DECLARE #gebruikersnaam varchar(25)
SELECT #gebruikersnaam = gebruikersnaam FROM inserted
SELECT #verkoperstatus = verkoper FROM Gebruiker WHERE gebruikersnaam = #gebruikersnaam
IF #verkoperstatus = 0
BEGIN
RAISERROR('Geen verkoper!',18,1);
ROLLBACK;
END
ELSE
BEGIN
COMMIT;
END
END
It should insert when #verkoperstatus is 1, and raise an error when #verkopstatus is 0.
The table Gebruiker is references, which includes a 'gebruikersnaam' and a 'verkoper' column. The value of the 'gebruikersnaam' column is the identifying column which (in this specific case is 'Lars'). Verkoper is a bit column, which indicated if one is a seller or not, so this has the value of a 0 or a 1.
The goal I am trying to achieve is to have an insert on the Verkoper tabel if a 'gebruikersnaam' has the 'verkoper' value of one.
This means if there is a row in Gebruiker with the 'gebruikersnaam' of Lars and the verkoper has a value of 1. This will be an allowed insert into the Verkoper tabel.
As the Verkoper has the following columns: 'gebruikersnaam', 'banknaam', 'rekeningnummer', 'controleoptienaam' and 'creditcardnummer'. When 'gebruikersnaam' corresponds with a 'gebruikersnaam' from the Gebruikers table AND has a value of 1 in the 'verkoper' column this record will be allowed to be inserted into the Verkoper table.
As of now there is a row in the Gebruikers column which includes the gebruikersnaam 'Lars' and a verkoper value of '1'. Meaning any SQL Insert with the gebruikersnaam of 'Lars' should be allowed into the Verkoper table.
This however does not function the way I believe it should.
These are the tables mentioned above:
CREATE TABLE Verkoper (
gebruikersnaam varchar(25) NOT NULL,
banknaam varchar(255) NULL,
rekeningnummer varchar(32) NULL,
controleoptienaam char(10) NOT NULL,
creditcardnummer integer NULL,
CONSTRAINT pk_Verkoper PRIMARY KEY (gebruikersnaam),
CONSTRAINT fk_Verkoper_Gebruikersnaam FOREIGN KEY (gebruikersnaam) REFERENCES Gebruiker(gebruikersnaam),
CONSTRAINT ck_rekening CHECK (rekeningnummer is NOT NULL OR creditcardnummer is NOT NULL),
CONSTRAINT ck_controleoptie CHECK (controleoptienaam IN('Post', 'Creditcard'))
)
CREATE TABLE Gebruiker(
gebruikersnaam varchar(25) NOT NULL,
voornaam varchar(25) NOT NULL,
achternaam varchar(25) NOT NULL,
adresregel_1 varchar(255) NULL,
adresregel_2 varchar(255) NULL,
postcode char(7) NULL,
plaatsnaam varchar(255) NULL,
land varchar(255) NULL,
geboortedag char(10) NOT NULL,
mailbox varchar(255) NOT NULL,
wachtwoord varchar(255) NOT NULL,
verkoper bit NOT NULL,
CONSTRAINT pk_gebruiker PRIMARY KEY (gebruikersnaam),
)
For the inserts I am using the following data:
INSERT INTO Gebruiker VALUES ('Lars', 'Lars', 'Last_name', null, null, null, null, null, '04/04/2019', 'lars#mymailbox.cloud', 'MyPassword', 1)
INSERT INTO Verkoper VALUES ('Lars', 'ING', 'NL32ABN32492809', 'Post', null)
This is untested, however, I suspect this is the logic you really need:
CREATE TRIGGER [dbo].[verkoper_check] ON [dbo].[Verkoper]
FOR INSERT,UPDATE
AS BEGIN
IF EXISTS(SELECT 1
FROM inserted i
JOIN Gebruiker G ON i.gebruikersnaam = G.gebruikersnaam
WHERE G.verkoper = 0) BEGIN
RAISERROR('Geen verkoper!',18,1);
ROLLBACK;
END;
END;
I'm using sqlite3 for this database.
For an assignment I have made a database for your typical food ordering website. A table in this database called customer_order contains all orders placed by customers. This table also contains the columns for the delivery address of the person who ordered the food. The database also contains a table called customer which contains the home address of each customer (the home address of a customer is characterized by the prefix preferred_).
Since its possible you want food delivered to your person when you are not home, when inserting into customer_order, the columns that correspond to the delivery address can be some other address than the customer's home address.
What I want to do is create a trigger that automatically fills the the columns for the delivery address with the customer's home address when no delivery address is specified in the insert into the customer_order table.
The code I have so far is this:
CREATE TRIGGER update_delivery_address BEFORE INSERT ON customer_order
WHEN address_street IS NULL
BEGIN
UPDATE customer_order SET address_street = (SELECT customer.preferred_address_street FROM customer WHERE customer.user_id = customer_order.user_id);
UPDATE customer_order SET address_number = (SELECT customer.preferred_address_number FROM customer WHERE customer.user_id = customer_order.user_id);
UPDATE customer_order SET address_zipcode = (SELECT customer.preferred_address_zipcode FROM customer WHERE customer.user_id = customer_order.user_id);
UPDATE customer_order SET address_city = (SELECT customer.preferred_address_city FROM customer WHERE customer.user_id = customer_order.user_id);
END;
When I declare the database I don't get any errors, but when I try to insert into the database I get the following error:
Error: near line 108: no such column: address_street
If anyone knows how I can fix this or a better approach, any help is much appreciated.
CREATE TABLE `customer_order` (
`customer_order_id` int(11) NOT NULL PRIMARY KEY,
`user_id` int(11) DEFAULT NULL,
`restaurant_id` int(11) NOT NULL,
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`preferred_delivery_time` datetime NOT NULL,
`full_name` varchar(300) NOT NULL,
`phone_number` varchar(14) NOT NULL,
`email_address` varchar(300) NOT NULL,
`address_street` varchar(300) DEFAULT NULL,
`address_number` varchar(8) DEFAULT NULL,
`address_zipcode` varchar(6) DEFAULT NULL,
`address_city` varchar(300) DEFAULT NULL,
`geolocation` VARCHAR(30) NOT NULL,
`is_paid` tinyint(4) NOT NULL,
`notes` text DEFAULT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
FOREIGN KEY(user_id) REFERENCES user(user_id),
FOREIGN KEY(restaurant_id) REFERENCES restaurant(restaurant_id)
);
AND
CREATE TABLE `customer` (
`user_id` int(11) NOT NULL PRIMARY KEY,
`preferred_address_street` varchar(300) NOT NULL,
`preferred_address_number` varchar(8) NOT NULL,
`preferred_address_zipcode` varchar(6) NOT NULL,
`preferred_address_city` varchar(300) NOT NULL,
`geolocation` VARCHAR(30) NOT NULL,
`discount_points_collected` int(11) NOT NULL,
FOREIGN KEY(user_id) REFERENCES user(user_id)
);
I believe that your issue is with the WHEN clause, it should be using the new/old table rather than the actual table (as it's an INSERT trigger then only new is avialble).
As per :-
Both the WHEN clause and the trigger actions may access elements of
the row being inserted, deleted or updated using references of the
form "NEW.column-name" and "OLD.column-name", where column-name is the
name of a column from the table that the trigger is associated with.
OLD and NEW references may only be used in triggers on events for
which they are relevant, as follows:
INSERT NEW references are valid
UPDATE NEW and OLD references are
valid
DELETE OLD references are valid
As per CREATE TRIGGER
I also believe that you'd need a WHERE clause to restrict the UPDATE's. I'd also suggest a single UPDATE rather.
As such I'd suggest using :-
CREATE TRIGGER IF NOT EXISTS update_delivery_address AFTER INSERT ON customer_order
WHEN new.address_street IS NULL
BEGIN
UPDATE customer_order SET address_street = (SELECT customer.preferred_address_street FROM customer WHERE customer.user_id = customer_order.user_id),
address_number = (SELECT customer.preferred_address_number FROM customer WHERE customer.user_id = customer_order.user_id),
address_zipcode = (SELECT customer.preferred_address_zipcode FROM customer WHERE customer.user_id = customer_order.user_id),
address_city = (SELECT customer.preferred_address_city FROM customer WHERE customer.user_id = customer_order.user_id)
WHERE customer_order_id = new.customer_order_id;
END;
The above has been tested, but with only with a single new order and limited customers/users, also foreign key reference to restaurants removed, The following was used for testing:-
INSERT INTO customer_order VALUES(1,1,10,'2018-10-10','20:30','Fred Bloggs','1234567890','fred#fred.com',null,null,null,null,'????','0','',1);
The resultant row being :-
Alternative (simpler) Trigger using Row values :-
CREATE TRIGGER IF NOT EXISTS update_delivery_address
AFTER INSERT ON customer_order
WHEN new.address_street IS NULL
BEGIN
UPDATE customer_order
SET (address_street, address_number, address_zipcode, address_city) =
(SELECT preferred_address_street,
preferred_address_number,
preferred_address_zipcode,
preferred_address_city
FROM customer
WHERE customer.user_id = customer_order.user_id
)
WHERE customer_order_id = new.customer_order_id;
END
;
NOTE I believe this requires at least SQLite release 3.16.1 as per (the above was tested on 3.21.0).
Fix a bug concerning the use of row values within triggers (see ticket
8c9458e7) that was in version 3.15.0 but was not reported until
moments after the 3.16.0 release was published.
SQL used for testing
DROP TABLE IF EXISTS customer_order;
CREATE TABLE IF NOT EXISTS `customer_order` (
`customer_order_id` int(11) NOT NULL PRIMARY KEY,
`user_id` int(11) DEFAULT NULL,
`restaurant_id` int(11) NOT NULL,
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`preferred_delivery_time` datetime NOT NULL,
`full_name` varchar(300) NOT NULL,
`phone_number` varchar(14) NOT NULL,
`email_address` varchar(300) NOT NULL,
`address_street` varchar(300) DEFAULT NULL,
`address_number` varchar(8) DEFAULT NULL,
`address_zipcode` varchar(6) DEFAULT NULL,
`address_city` varchar(300) DEFAULT NULL,
`geolocation` VARCHAR(30) NOT NULL,
`is_paid` tinyint(4) NOT NULL,
`notes` text DEFAULT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
FOREIGN KEY(user_id) REFERENCES user(user_id)
--FOREIGN KEY(restaurant_id) REFERENCES restaurant(restaurant_id)
);
DROP TABLE IF EXISTS customer;
CREATE TABLE `customer` (
`user_id` int(11) NOT NULL PRIMARY KEY,
`preferred_address_street` varchar(300) NOT NULL,
`preferred_address_number` varchar(8) NOT NULL,
`preferred_address_zipcode` varchar(6) NOT NULL,
`preferred_address_city` varchar(300) NOT NULL,
`geolocation` VARCHAR(30) NOT NULL,
`discount_points_collected` int(11) NOT NULL,
FOREIGN KEY(user_id) REFERENCES user(user_id)
);
DROP TABLE IF EXISTS user;
CREATE TABLE IF NOT EXISTS user (
user_id INTEGER PRIMARY KEY,
user_name TEXT
);
DROP TRIGGER IF EXISTS update_delivery_address;
CREATE TRIGGER IF NOT EXISTS update_delivery_address
AFTER INSERT ON customer_order
WHEN new.address_street IS NULL
BEGIN
UPDATE customer_order
SET (address_street, address_number, address_zipcode, address_city) =
(SELECT preferred_address_street,
preferred_address_number,
preferred_address_zipcode,
preferred_address_city
FROM customer
WHERE customer.user_id = customer_order.user_id
)
WHERE customer_order_id = new.customer_order_id;
END
;
INSERT INTO user (user_name) VALUES ('Fred'),('Bert'),('Harry'),('Tom');
INSERT INTO customer (
user_id,
preferred_address_street,
preferred_address_number,
preferred_address_zipcode,
preferred_address_city,
geolocation,discount_points_collected)
VALUES (1,'Somerset Blvd','1','12345','Syndey','?????',100);
INSERT INTO customer_order (
customer_order_id,
user_id,
restaurant_id,
preferred_delivery_time,
full_name,
phone_number,
email_address,
geolocation,
is_paid,
notes
)
VALUES(1,1,20,'21:30','Fred Bloggs','0000 000 000','Fred#Bloggs.com','x',0,'not to note');
Update every column separately using insert join for trigger. You will need after insert trigger
Try this
CREATE TRIGGER update_delivery_address ON customer_order
AFTER INSERT
AS
BEGIN
UPDATE c
SET address_street = cu.preferred_address_street
FROM customer_order c
INNER JOIN INSERTED i ON c.user_id = i.user_id
INNER JOIN Customer cu ON c.user_id = cu.user_id
WHERE c.Address_street IS NULL
UPDATE c
SET address_number = cu.preferred_address_number
FROM customer_order c
INNER JOIN INSERTED i ON c.user_id = i.user_id
INNER JOIN Customer cu ON c.user_id = cu.user_id
WHERE c.Address_street IS NULL
UPDATE c
SET address_zipcode = cu.preferred_address_zipcode
FROM customer_order c
INNER JOIN INSERTED i ON c.user_id = i.user_id
INNER JOIN Customer cu ON c.user_id = cu.user_id
WHERE c.Address_street IS NULL
UPDATE c
SET address_city = cu.preferred_address_city
FROM customer_order c
INNER JOIN INSERTED i ON c.user_id = i.user_id
INNER JOIN Customer cu ON c.user_id = cu.user_id
WHERE c.Address_street IS NULL
END;
I have three tables:
CREATE TABLE Rents
(
RentID INT IDENTITY NOT NULL PRIMARY KEY,
StartDate SMALLDATETIME,
EndDate SMALLDATETIME,
Price MONEY,
RealEstateID INT UNIQUE NOT NULL,
DealMadeByEmployeeID INT NOT NULL,
CONSTRAINT CHK_Rents CHECK (Price > 0 AND EndDate > StartDate),
CONSTRAINT FK_Rents_EstatesBasicInfo
FOREIGN KEY (RealEstateID) REFERENCES EstatesBasicInfo(RealEstateID),
CONSTRAINT FK_Rents_Employees
FOREIGN KEY (DealMadeByEmployeeID) REFERENCES Employees(EmployeeID)
);
CREATE TABLE Purchases
(
PurchaseID INT IDENTITY NOT NULL PRIMARY KEY,
DateBought SMALLDATETIME,
Price MONEY CHECK (Price>0),
RealEstateID INT UNIQUE NOT NULL,
DealMadeByEmployeeID INT NOT NULL,
CONSTRAINT FK_Purchases_EstatesBasicInfo
FOREIGN KEY (RealEstateID) REFERENCES EstatesBasicInfo(RealEstateID),
CONSTRAINT FK_Purchases_Employees
FOREIGN KEY (DealMadeByEmployeeID) REFERENCES Employees(EmployeeID)
);
CREATE TABLE EmployeesSalary
(
EmployeeID INT NOT NULL PRIMARY KEY,
CurrentSalary MONEY DEFAULT 0,-- на процент
MonthlySalesMade INT DEFAULT 0,
MonthlyRentsMade INT DEFAULT 0,
CONSTRAINT FK_EmployeesSalary_Employees
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID),
CONSTRAINT CHK_EmployeesSalary
CHECK (CurrentSalary >= 0 AND MonthlySalesMade >= 0 AND MonthlyRentsMade >= 0)
);
Each of them has a trigger
CREATE TRIGGER tr_EmployeesSalaryPurchasesUpdate --при INSERT в Purchases таблицата
ON Purchases
AFTER INSERT
AS
BEGIN
UPDATE EmployeesSalary
SET EmployeesSalary.MonthlySalesMade = EmployeesSalary.MonthlySalesMade + 1
WHERE EmployeesSalary.EmployeeID IN (SELECT inserted.DealMadeByEmployeeID
FROM inserted
WHERE DateBought IS NOT NULL)
END
--Update на MonthlyRentsMade
GO
CREATE TRIGGER tr_EmployeesSalaryRentsUpdate --при INSERT в Rents таблицата
ON Rents
AFTER INSERT
AS
BEGIN
UPDATE EmployeesSalary
SET MonthlyRentsMade = MonthlyRentsMade + 1
WHERE EmployeesSalary.EmployeeID IN (SELECT inserted.DealMadeByEmployeeID
FROM inserted
WHERE StartDate IS NOT NULL)
END
The problem comes when I want to add a trigger to EmployeesSalary:
CREATE TRIGGER tr_EmployeesSalaryCurrentSalary
ON EmployeesSalary
AFTER INSERT
AS
BEGIN
UPDATE EmployeesSalary
SET CurrentSalary = CurrentSalary + ((MonthlySalesMade + MonthlyRentsMade) * 200)
WHERE EmployeeID IN (SELECT i.EmployeeID
FROM inserted AS i);
END
I want when I get an insert in the EmployeesSalary (by the other two triggers), the CurrentSalary to be updated (depending on the EmployeeID). The trigger cause any errors but it doesn't work. (I think this is called nested triggers not sure...) Where is my mistake?
I would prefer to use a computed column instead of a trigger for something like this. Here is an example. I also added ISNULL around the two columns in case you have a NULL. Otherwise the computation will always be NULL.
alter table EmployeesSalary
add ComputedSalary as CurrentSalary + ((isnull(MonthlySalesMade, 0) + isnull(MonthlyRentsMade, 0)) * 200) persisted
Since this is an after-insert trigger (recursion won't occur on update table). I think where you are making a mistake is using NULL values in calculations so I would try this:
CREATE TRIGGER tr_EmployeesSalaryCurrentSalary
ON EmployeesSalary
AFTER INSERT
AS
BEGIN
UPDATE es
SET CurrentSalary = coalesce(i.CurrentSalary, 0) + ((coalesce(i.MonthlySalesMade, 0) + coalesce(i.MonthlyRentsMade, 0)) * 200)
from EmployeesSalary as es
inner join inserted AS i
on i.EmployeeID = es.EmployeeID
END
Also, I don't like IN statements so I'm joining instead.
I create 2 tables employees and customers.The employees table code is here:
create table employees(
employeeNumber number not null,
lastName varchar2(30) not null,
firstName varchar2(30) not null,
email varchar2(50) not null,
officeCode varchar2(10) not null,
assignTo number default null,
jobTitle varchar2(100) not null,
primary key (employeeNumber),
foreign key (officeCode) references offices(officeCode),
foreign key (assignTo) references employees(employeeNumber)
);
here assignTo is a foreign key of his own table employees and employeeNumber is auto increment.Some insertion sample is here:
insert into employees (lastName,firstName,email,officeCode,jobTitle)
values ('hasan','rumy','md.rejaulhasanrumy#gmail.com','123','manager');
insert into employees (lastName,firstName,email,officeCode,assignTo,jobTitle)
values ('hasan','rakib','kalorakib#gmail.com','123', 1 ,'assistant manager');
The customer table code is here:
create table customers (
customerNumber number not null,
customerName varchar2(50) not null,
phone varchar2(20) not null,
address varchar2(70) not null,
city varchar2(50) not null,
postalCode varchar2(15) not null,
country varchar2(40) not null,
salesRepEmployeeNumber number default null,
primary key(customerNumber),
foreign key (salesRepEmployeeNumber) references employees (employeeNumber)
);
customerNumber is auto increment.some sample insertion is here:
insert into customers
(customerName,phone,address,city,postalCode,country,salesRepEmployeeNumber)
values ('roxy','017456','holy park','kolia','Z143','something',1);
Now I create a trigger which execute before update employeeNumber column of employees table for on update cascade and the code is here:
create or replace trigger employees_update
before update of employeeNumber on employees
for each row
begin
update employees
set
assignTo = :new.employeeNumber
where assignTo = :old.employeeNumber;
update customers set
salesRepEmployeeNumber = :new.employeeNumber
where salesRepEmployeeNumber = :old.employeeNumber;
end;
/
above all is right in oracle but the problem is when I update employees table.The update code is here:
update employees set employeeNumber = 134 where employeeNumber = 1;
the problem is here:
ORA-04091: table RUMY.EMPLOYEES is mutating, trigger/function may not see it
ORA-06512: at "RUMY.EMPLOYEES_UPDATE", line 2
ORA-04088: error during execution of trigger 'RUMY.EMPLOYEES_UPDATE'
1. update employees set employeeNumber = 134 where employeeNumber = 1;
As far I know it's a system problem so where I make mistake?Can not I make foreign key assignTo of employees table?Also notice that same thing work properly in mysql.Advance thanks for answering this long question.
As you have discovered, you cannot select from the same table that a row-level trigger is defined against; it causes a table mutating exception.
Then - assuming at least Oracle 11, this will need to be split into individual triggers in earlier versions
CREATE OR REPLACE TRIGGER employees_update
FOR UPDATE ON employees
COMPOUND TRIGGER
TYPE employeeNumberRec IS RECORD
(oldEmployeeNumber employees.employeeNumber%TYPE
,newEmployeeNumber employees.employeeNumber%TYPE);
TYPE employeeNumbersTbl IS TABLE OF employeeNumberRec;
g_employeeNumbers employeeNumbersTbl;
BEFORE STATEMENT
IS
BEGIN
-- Reset the internal employees table
g_employeeNumbers := employeeNumbersTbl();
END BEFORE STATEMENT;
AFTER EACH ROW
IS
BEGIN
-- Store the updated employees
IF :new.employeeNumber <> :old.employeeNumber THEN
g_employeeNumbers.EXTEND;
g_employeeNumbers(g_employeeNumbers.LAST).oldEmployeeNumber := :old.employeeNumber;
g_employeeNumbers(g_employeeNumbers.LAST).newEmployeeNumber := :new.employeeNumber;
END IF;
END AFTER EACH ROW;
AFTER STATEMENT
IS
BEGIN
-- Now update the child tables
FORALL l_index IN 1..g_employeeNumbers.COUNT
UPDATE employees
SET assignTo = g_employeeNumbers(l_index).newEmployeeNumber
WHERE assignTo = g_employeeNumbers(l_index).oldEmployeeNumber;
FORALL l_index IN 1..g_employeeNumbers.COUNT
UPDATE customers
SET salesRepEmployeeNumber = g_employeeNumbers(l_index).newEmployeeNumber
WHERE salesRepEmployeeNumber = g_employeeNumbers(l_index).oldEmployeeNumber;
END AFTER STATEMENT;
END;
EDIT
In addition you will need to make the foreign key constraints that reference this table deferred e.g.
CREATE TABLE employees
(employeeNumber NUMBER NOT NULL
,lastName VARCHAR2(30) NOT NULL
,firstName VARCHAR2(30) NOT NULL
,email VARCHAR2(50) NOT NULL
,officeCode VARCHAR2(10) NOT NULL
,assignTo NUMBER DEFAULT NULL
,jobTitle VARCHAR2(100) NOT NULL
,PRIMARY KEY (employeeNumber)
,FOREIGN KEY (officeCode)
REFERENCES offices (officeCode)
,FOREIGN KEY (assignTo)
REFERENCES employees (employeeNumber)
DEFERRABLE
INITIALLY DEFERRED
)
and
CREATE TABLE customers
(customerNumber NUMBER NOT NULL
,customerName VARCHAR2(50) NOT NULL
,phone VARCHAR2(20) NOT NULL
,address VARCHAR2(70) NOT NULL
,city VARCHAR2(50) NOT NULL
,postalCode VARCHAR2(15) NOT NULL
,country VARCHAR2(40) NOT NULL
,salesRepEmployeeNumber NUMBER DEFAULT NULL
,PRIMARY KEY (customerNumber)
,FOREIGN KEY (salesRepEmployeeNumber)
REFERENCES employees (employeeNumber)
DEFERRABLE
INITIALLY DEFERRED
)
Note: if the constraint is violated this will cause an error at COMMIT not after an individual DML statement.
You can re-write the above trigger in following ways to avoid the issues:
create or replace trigger employees_update
before update of employeeNumber on employees
for each row
begin
emp_upd_trg_proc(:new.employeeNumber,:old.employeeNumber);
update customers set
salesRepEmployeeNumber = :new.employeeNumber
where salesRepEmployeeNumber = :old.employeeNumber;
end;
/
create or replace procedure emp_upd_trg_proc
(new number, old number)
is
pragma autonomous_transaction;
begin
update employees
set
assignTo = new
where assignTo = old;
commit;
end;
/
maybe I am over thinking this these are the instructions
create four new tables: my_interests, my_professions, my_seeking and my_status. These tables should contain two columns: one for an id and another for the data value. Populate these from the appropriate columns in the my_contacts table. Once your new tables are ready, you can replace the original data columns with id numbers matching the applicable id values in your new my_professions and my_status tables.
SO THE WAY I READ THE FIRST IS I HAVE THE FIRST COL AS AN AUTO INCREMENT PRIMARY KEY, THE NEXT VALUE WOULD BE THE DATA TYPE, I AM STARTING WITH MY_STATUS FIRST AND THE TYPES WOULD BE
COMMITED_RELATIONSHIP
DIVORCED
MARRIED
SINGLE
WIDOWED
AND MY AUTO_INC NUMBER WOULD CORRESPOND TO THE ID OF DATA, IN OTHER WORDS
01**COMMITED
02**DIVORCED
03** AND SO ON
AM I CORRECT IN THE WAY I AM READING THE INSTRUCTIONS?
AND IF SO NOT QUITE SURE HOW TO DO THIS, I KNOW HOW TO CREATE THE TABLE, AND I UNDERSTAND INSERT INTO AND VALUES, BUT HOW DO I INSERT THE VALUES AND AUTO INCREMENT? I REALLY DISLIKE PROGRAMMING :(
thanks in advance
Create table my_status (status_id int NOT NULL AUTO_INCREMENT,status_type varchar(20), PRIMARY KEY (status_id)); To Create
insert into my_status (status_type) values("Single"),("Divorced"),("Married"); to insert
Create table my_status (status_id int NOT NULL AUTO_INCREMENT,status_type varchar(20), PRIMARY KEY (status_id)); To Create
insert into my_status (status_type) values("Single"),("Divorced"),("Married"); to insert
Possible Answer to your 2nd question
CREATE TABLE `my_status` (
`status_id` int(11) NOT NULL auto_increment,
`status_type` varchar(30),PRIMARY KEY(`status_id`) );
CREATE TABLE `my_profession` (
`profession_id` int(11) NOT NULL auto_increment,
`profession_type` varchar(30),PRIMARY KEY(`profession_id`) );
CREATE TABLE `my_contacts` (
`id` int(11) NOT NULL auto_increment,
`last_name` varchar(30) ,
`first_name` varchar(20) ,
`email` varchar(50) ,
`gender` char(1),
`birthday` date ,
`profession_id` int(11),
`location` varchar(50),
`status_id` int(11),
`interests` varchar(200),
`seeking` varchar(200),
PRIMARY KEY (`id`),
FOREIGN KEY (status_id)
REFERENCES my_status(status_id),
FOREIGN KEY (profession_id)
REFERENCES my_profession(profession_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
insert into my_status (status_type) values("Single"),("Divorced"),("Married");
insert into my_profession (profession_type) values("Writer"),("CA"),("Developer");
INSERT INTO `my_contacts` (`last_name`,`first_name`,`email`,`gender`,`birthday`,`profession_id`,`location`,`status_id`,`interests`,`seeking`) VALUES ('Anderson','Jillian','jill_anderson# \nbreakneckpizza.com','F','1980-09-05',1,'Palo Alto, CA',1,'kayaking, reptiles','relationship, friends');
INSERT INTO `my_contacts` (`last_name`,`first_name`,`email`,`gender`,`birthday`,`profession_id`,`location`,`status_id`,`interests`,`seeking`) VALUES ('Kenton','Leo','lkenton#starbuzzcoffee.com','M','1974-01-10',2,'San Francisco, CA',2,'women','women to date');
INSERT INTO `my_contacts` (`last_name`,`first_name`,`email`,`gender`,`birthday`,`profession_id`,`location`,`status_id`,`interests`,`seeking`) VALUES ('McGavin','Darrin',' captainlove#headfirsttheater.com','M','1966-01-23',3,'San Diego, CA',3,'sailing, fishing, yachting','women for casual relationships');
INSERT INTO `my_contacts` (`last_name`,`first_name`,`email`,`gender`,`birthday`,`profession_id`,`location`,`status_id`,`interests`,`seeking`) VALUES ('xyz','abc',' xyz#abc.com','F','1966-01-24',1,'San Diego, CA',3,'sailing, fishing, yachting, golfing','women for casual relationships');
select * from my_contacts;
select * from my_status;
select * from my_profession;
SELECT * FROM my_contacts WHERE status_id IN
(SELECT status_id FROM my_status WHERE status_type = 'Married');
Hope this is what you want!!!
UPDATE my_contacts
SET status='1'
WHERE status='committed relationship';
UPDATE my_contacts
set status= '2'
where status='divorced';
UPDATE my_contacts
set status= '3'
where status='married';
UPDATE my_contacts
set status= '4'
where status='single';
UPDATE my_contacts
set status= '5'
where status='widowed';
THANKS EVERYONE!! (yeah that time I shouted, lol!)