Kohana orm inheritance - php

I want to auto creating parent record on creating children.
Example.
CREATE TABLE IF NOT EXISTS `front_end_users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`userid` int(11) unsigned NOT NULL,
`tel` varchar(20) NOT NULL,
`addr1` varchar(255) NOT NULL,
`addr2` varchar(255) NOT NULL,
`city` int(11) NOT NULL,
`zip` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_userid` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(127) NOT NULL,
`username` varchar(32) NOT NULL DEFAULT '',
`password` char(64) NOT NULL,
`logins` int(10) unsigned NOT NULL DEFAULT '0',
`last_login` int(10) unsigned DEFAULT NULL,
`reset_token` char(64) NOT NULL DEFAULT '',
`status` varchar(20) NOT NULL DEFAULT '',
`last_failed_login` datetime NOT NULL,
`failed_login_count` int(11) NOT NULL DEFAULT '0',
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_username` (`username`),
UNIQUE KEY `uniq_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `promouter` (
`userid` int(10) unsigned NOT NULL,
`balance` float NOT NULL,
`commision` float NOT NULL,
`mountly_fee` float NOT NULL,
`verified` tinyint(1) NOT NULL DEFAULT '0',
KEY `promouter_ibfk_1` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `front_end_users`
ADD CONSTRAINT `front_end_users_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `users` (`id`) ON DELETE CASCADE;
ALTER TABLE `promouter`
ADD CONSTRAINT `promouter_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `front_end_users` (`userid`) ON DELETE CASCADE;
users->front_end_users->promouter
I want if I create promouter with login, password and address than automatically inserts appropriate fields in users (login, password), front_end_users (addr1, addr2, userid) and promouter (userid).
And I have several roles. So I can not add have_one to user model. But I added have_one to front_end_users and promouter models.

If you want to trigger parent creation on child creation then I think you should use trigger(s) on database level.
EDIT:
Create a table with the following scheme:
Rules(ID, TableName, ColumnName, Event, Action, Parameters)
You will have a set of Rules then and you will be able to make a general implementation for this. In your business layer you will call your rule parser (which also has to be implemented by you) and this way you can make any logical triggers in your business layer. I hope this helps.

Related

how to create Duplicatable entry at foreign key column in mysql

I created a site that gives you info about universities, every university has registration and exams, etc
so in database I have two columns
THE FIRST TABLE IS THE UNIVERSITIES:
CREATE TABLE `universities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL,
`views` int(11) NOT NULL DEFAULT 0,
`image` varchar(255) NOT NULL,
`banner` varchar(255) NOT NULL,
`aboutUni` text NOT NULL,
`aboutCity` text NOT NULL,
`localRank` int(5) DEFAULT NULL,
`globalRank` int(3) DEFAULT NULL,
`foundedY` int(4) NOT NULL,
`lang` varchar(10) DEFAULT NULL,
`published` tinyint(1) NOT NULL DEFAULT 1,
`created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`user_id` int(11) DEFAULT NULL,
`video` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `slug` (`slug`),
UNIQUE KEY `title` (`title`),
UNIQUE KEY `slug_2` (`slug`),
KEY `universities_ibfk_1` (`user_id`),
FULLTEXT KEY `title_2` (`title`),
CONSTRAINT `universities_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8
THE SECOND TABLE IS THE EXAMS
CREATE TABLE `bsc` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`university_id` int(11) DEFAULT NULL,
`dates` text NOT NULL,
`date_end` date DEFAULT NULL,
`date` date DEFAULT NULL,
`papers` text NOT NULL,
`info` text NOT NULL,
`method` text NOT NULL,
`link` varchar(255) DEFAULT NULL,
`register_link` varchar(255) NOT NULL,
`links` text NOT NULL,
`sort` int(2) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `university_id` (`university_id`),
UNIQUE KEY `university_id_2` (`university_id`),
CONSTRAINT `bsc_ibfk_1` FOREIGN KEY (`university_id`) REFERENCES `universities` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4
NOTICE!: the tables have other columns but I think it's not important
university_id has a foreign key relation with universities.id
the problem is when I want to add tow exam to a university it appears me this error:
Duplicate entry '39' for key 'university_id'
The problem you faced Duplicate entry '39' for key 'university_id' results from trying to store in university_id a value that already existed somewhere, which is not allowed according to some constraints.
as shown in the DDL part
UNIQUE KEY `university_id` (`university_id`)
This constrain is the main cause for that error, so you need to remove that constrain by
ALTER TABLE `bsc` DROP INDEX `university_id`;
Thanks for #Tangentially, who was the first one who expected that error in his comment.
It seems that these keys are unnecesary:
UNIQUE KEY university_id (university_id),
UNIQUE KEY university_id_2 (university_id),
given that keys, you should only have distinct values in that fields.

Not able to assign On Delete Set null and on updated set Null in mysql

bank_api_uat_user Table
CREATE TABLE IF NOT EXISTS `bank_api_uat_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bank_name` varchar(255) DEFAULT NULL,
`role` varchar(10) NOT NULL,
`bank_code` char(10) NOT NULL,
`user_name` varchar(255) NOT NULL,
`password` varchar(20) NOT NULL,
`api_key` varchar(255) NOT NULL,
`Client_Secret` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_name` (`user_name`),
KEY `user_name_2` (`user_name`),
KEY `id` (`id`,`user_name`),
KEY `role` (`role`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=36 ;
create_role Table
CREATE TABLE IF NOT EXISTS `create_role` (
`date` datetime NOT NULL,
`Role_name` varchar(50) NOT NULL,
`Role_code` varchar(5) NOT NULL,
PRIMARY KEY (`Role_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to assign referential integrity to bank_api_uat_user table each time i add constraint it gives below error
MySQL said: Documentation
1215 - Cannot add foreign key constraint
Below query used to create foreign key.
ALTER TABLE `bank_api_uat_user` ADD CONSTRAINT `const_file_role` FOREIGN KEY (`role`) REFERENCES `test`.`create_role`(`Role_code`) ON DELETE SET NULL ON UPDATE SET NULL;
Its important to make a child column Null for setting null reference option.
the above queries work if role is declared as NULL
CREATE TABLE IF NOT EXISTS `bank_api_uat_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bank_name` varchar(255) DEFAULT NULL,
`role` varchar(10) NULL,
`bank_code` char(10) NOT NULL,
`user_name` varchar(255) NOT NULL,
`password` varchar(20) NOT NULL,
`api_key` varchar(255) NOT NULL,
`Client_Secret` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_name` (`user_name`),
KEY `user_name_2` (`user_name`),
KEY `id` (`id`,`user_name`),
KEY `role` (`role`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=36 ;
Full explanation can be find here : https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html

Database schema - Creating a private messaging based on an 'item'

users table:
CREATE TABLE `users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`unique_id` varchar(23) NOT NULL,
`full_name` varchar(100) DEFAULT NULL,
`name` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `unique_id` (`unique_id`) USING BTREE,
UNIQUE KEY `email` (`email`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8;
items table:
CREATE TABLE `items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`item_photo` text,
`item_name` varchar(100) DEFAULT NULL,
`item_description` varchar(500) DEFAULT NULL,
`item_price` varchar(10) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`category_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_user_id` (`user_id`) USING BTREE,
KEY `fk_category_id` (`category_id`) USING BTREE,
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`),
CONSTRAINT `items_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8;
What I would wanna do is to create a private messaging system for my project. Before this I have implemented a Comment system and it works well (pull out comments which has the same item_id). You can see the DDL and query here. But when it come to this, I find it hard to think about private messaging model.
Basically, the private message is to bid the item price between TWO users only (the seller and the bidder). Other registered user cannot see others bidding.
Here's my try at creating Bids table:
CREATE TABLE `bids` (
`id` int(11) NOT NULL,
`bid` float DEFAULT NULL,
`message` varchar(255) DEFAULT NULL,
`from_uid` varchar(255) DEFAULT NULL,
`to_uid` varchar(255) DEFAULT NULL,
`to_iid` int(11) DEFAULT NULL,
`time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I have also tried to make Foreign Keys into bids table, but it seems like it's already too complicated for me. So an error 1215 - Cannot add foreign key constraint came out :(
If anything just let me know.
EDIT: charset to utf8. Failed to create Foreign key constraints
MESSAGES
message_id
conversation_id
user_id
message
CONVERSATIONS
conversation_id
item_id
user_id
BIDS
bid_id
item_id
user_id
amount

trying to make a foreign key but MySql won't allow me, is there any way?

i have these 3 tables
CREATE TABLE IF NOT EXISTS `enrollment` (
`STUDENT_NUM` varchar(10) NOT NULL,
`SUBJECT_NUM` varchar(10) NOT NULL,
`UNITS` int(10) NOT NULL,
`DAYS` varchar(50) NOT NULL,
`TIME_START` time NOT NULL,
`TIME_END` time NOT NULL,
`ROOM_ID` int(11) DEFAULT NULL,
`PRELIM` float(10,2) DEFAULT NULL,
`MIDTERM` float(10,2) DEFAULT NULL,
`FINALS` float(10,2) DEFAULT NULL,
`FINAL_GRADE` float(10,2) DEFAULT NULL,
`SEMESTER` varchar(50) NOT NULL,
`SCHOOL_YEAR` varchar(50) NOT NULL,
`DATE_ADDED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`STUDENT_NUM`,`SUBJECT_NUM`),
KEY `SUBJECT_NUM` (`SUBJECT_NUM`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `subjects` (
`SUBJECT_NUM` varchar(10) NOT NULL,
`EMPLOYEE_NUM` varchar(10) NOT NULL,
`SUBJECT_TITLE` varchar(100) DEFAULT NULL,
`DEPARTMENT` varchar(100) DEFAULT NULL,
`UNITS` int(10) NOT NULL,
`DAYS` varchar(50) NOT NULL,
`TIME_START` time NOT NULL,
`TIME_END` time NOT NULL,
`room_id` int(11) DEFAULT NULL,
`SEMESTER` varchar(50) NOT NULL,
`SCHOOL_YEAR` varchar(50) NOT NULL,
`COUNT` int(10) DEFAULT NULL,
`STATUS` varchar(50) DEFAULT NULL,
`FLAG` varchar(50) NOT NULL,
`DATE_ADDED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`SUBJECT_NUM`),
UNIQUE KEY `SUBJECT_NUM` (`SUBJECT_NUM`),
KEY `EMPLOYEE_NUM` (`EMPLOYEE_NUM`),
KEY `EMPLOYEE_NUM_2` (`EMPLOYEE_NUM`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `room` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`room` varchar(255) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `room` (`room`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
what i am trying to do is make the field ROOM_ID from enrollment and subjects foreign key and the reference be the ID from room.. the ROOM_ID must not be unique...
i am getting this error
#1452 - Cannot add or update a child row: a foreign key constraint fails (`enrollmentdb`.`#sql-277_164`, CONSTRAINT `#sql-277_164_ibfk_3` FOREIGN KEY (`ROOM_ID`) REFERENCES `room` (`ID`))
when i am using this SQL command:
ALTER TABLE enrollment
ADD FOREIGN KEY (room_id)
REFERENCES room(ID)
Try below code if its working :
ALTER TABLE enrollment
ADD CONSTRAINT ROOM_ID_fk
FOREIGN KEY(ROOM_ID)
REFERENCES room(ID);
Thanks!
room_id is capitalised in your table. Try:
ALTER TABLE enrollment
ADD FOREIGN KEY (ROOM_ID)
REFERENCES room(ID)
replace 'room_id' with 'ROOM_ID'

How to accurately set up a foreign key scenario in MySQL to accomplish my addressbook

I have been making a database and learning along the way. I recently got into using InnoDB and using foreign keys to connect tables together.
But in all honestly I'm probably making my foreign keys blindly. What is the correct set and check list that I need to use when making a foreign key.
My understanding with foreign keys is that I have a Master Table, and any changes in my Master Table are reflected to any tables that hold a foreign key to a specific column in it.
So my current log-in system has a set up like this
users
=====
id PK
username
password
and my other tables look like this
contacts
========
id PK
user_id references `users`.`id`
group
name
address
groups
======
id PK
user_id
group_name
group_contacts
==============
id PK
group_id references `group`.`id`
contact_id references `contacts`.`id`
To my understanding these tables can be deleted when the Master Table is deleted using the ON DELETE CASCADE option correct?
My problem now is that I can't seem to make group_id and contact_id a Foreign key to groups.id and contacts.id with this setup. I get an error when running the SQL statements.
I'm trying to make my address book so that when a user places a contact into a group becomes all automated and I don't have to change much information. The group_contact table is what I THINK I will querying when I want to see where each contact belongs to. If I change the name of a group it will reflect across all tables right? This is where foreign keys come in and I'm confusing myself with how these keys should behave for me.
But like I said I can't seem to make a foreign key without getting an error.
I know I can Google my Foreign Key question, which I have but I can't seem to learn this way without getting feedback and input to my exact scenario ;(
Not to ask too much but because of my confusion I'm also having a hard time trying to see how I can make a PHP script to handle the group name change and query the database to pull down contacts that belong to a specific group.
This would really help me a lot guys, and I hope to learn something!
My query is this:
ALTER TABLE `list_`.`groups_contacts`
ADD CONSTRAINT `group_id` FOREIGN KEY (`group_id`) REFERENCES `list_`.`groups` (`id`)
ADD CONSTRAINT `contact_id` FOREIGN KEY (`contact_id`) REFERENCES `list_`.`contacts` (`id`);
My database looks like this:
CREATE TABLE `list_`.`buyer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`isClosed` tinyint(1) NOT NULL,
`display_limit` int(1),
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`prop_address` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`state` varchar(11) NOT NULL,
`zip` varchar(5) NOT NULL,
`cell_phone` varchar(16) NOT NULL,
`home_phone` varchar(16) NOT NULL,
`other1` varchar(16) NOT NULL,
`other2` varchar(16) NOT NULL,
`comments` text NOT NULL,
`comment_exist` tinyint(1) NOT NULL,
`comment_date` text NOT NULL,
`date_added` date NOT NULL,
`date_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 AUTO_INCREMENT=23 ;
CREATE TABLE `list_`.`company` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL DEFAULT '0',
`company_name` varchar(128) NOT NULL,
PRIMARY KEY (`id`, `user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
CREATE TABLE `list_`.`contacts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`group` varchar(128) NOT NULL,
`first_name` varchar(128) NOT NULL,
`last_name` varchar(128) NOT NULL,
`address` varchar(128) NOT NULL,
`city` varchar(128) NOT NULL,
`state` varchar(2) NOT NULL,
`zip` int(5) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`cell_number` varchar(16) NOT NULL,
`work_number` varchar(16) NOT NULL,
`fax_number` varchar(16) NOT NULL,
`email` varchar(128) NOT NULL,
`company` varchar(55) NOT NULL,
`title` varchar(56) NOT NULL,
`notes` text NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`),
KEY `group` (`group`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
CREATE TABLE `list_`.`groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`position` int(8) unsigned NOT NULL DEFAULT '0',
`name` varchar(128) NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`),
KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8 AUTO_INCREMENT=32 ;
CREATE TABLE `list_`.`prospect` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`isClosed` tinyint(1) DEFAULT '0',
`display_limit` int(1) DEFAULT '0',
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`prop_address` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`state` varchar(11) NOT NULL,
`zip` varchar(5) NOT NULL,
`cell_phone` varchar(16) NOT NULL,
`home_phone` varchar(16) NOT NULL,
`other1` varchar(16) NOT NULL,
`other2` varchar(16) NOT NULL,
`comments` text NOT NULL,
`date_added` date NOT NULL,
`date_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
CREATE TABLE `list_`.`seller` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`file` int(11) DEFAULT NULL,
`isClosed` tinyint(1) NOT NULL,
`display_limit` int(1) NOT NULL DEFAULT '0',
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`prop_address` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`state` varchar(22) NOT NULL,
`zip` varchar(5) NOT NULL,
`cell_phone` varchar(16) NOT NULL,
`home_phone` varchar(16) NOT NULL,
`other1` varchar(16) NOT NULL,
`other2` varchar(16) NOT NULL,
`comments` text NOT NULL,
`comment_exist` tinyint(1) NOT NULL,
`comment_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_added` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
CREATE TABLE `list_`.`settings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`seller_display_limit` int(4) DEFAULT '0',
`buyer_display_limit` int(4) DEFAULT '0',
`prospect_display_limit` int(4) DEFAULT '0',
`property_display_limit` int(4) DEFAULT '0',
`date_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
CREATE TABLE `list_`.`users` (
`id` tinyint(11) NOT NULL AUTO_INCREMENT,
`md5_id` varchar(200) NOT NULL,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`user_level` tinyint(1) DEFAULT '1',
`first_name` varchar(200) NOT NULL,
`last_name` varchar(200) NOT NULL,
`email` varchar(200) DEFAULT NULL,
`approved` int(1) NOT NULL,
`banned` int(1) NOT NULL,
`date_joined` date NOT NULL,
`ip` varchar(15) DEFAULT NULL,
`activation_code` int(9) DEFAULT NULL,
`ckey` varchar(220) NOT NULL,
`ctime` varchar(220) NOT NULL,
`last_logged_in` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`account_number` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
ALTER TABLE `list_`.`buyer`
ADD CONSTRAINT `buyer_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE;
--
-- Constraints for table `company`
--
ALTER TABLE `list_`.`company`
ADD CONSTRAINT `company_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE;
--
-- Constraints for table `contacts`
--
ALTER TABLE `list_`.`contacts`
ADD CONSTRAINT `contacts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
ADD CONSTRAINT `group_ibfk_2` FOREIGN KEY (`group`) REFERENCES `groups` (`name`) ON UPDATE CASCADE;
--
-- Constraints for table `groups`
--
ALTER TABLE `list_`.`groups`
ADD CONSTRAINT `group_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
--
-- Constraints for table `prospect`
--
ALTER TABLE `list_`.`prospect`
ADD CONSTRAINT `prospect_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
--
-- Constraints for table `seller`
--
ALTER TABLE `list_`.`seller`
ADD CONSTRAINT `seller_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
--
-- Constraints for table `settings`
--
ALTER TABLE `list_`.`settings`
ADD CONSTRAINT `settings_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
I think you slightly misunderstood the concept of Foreign Keys. Changing the name of a group is not supposed to reflect on any other table, you just change your group-table.
Assuming you have this simple scenario, where one Contact can belong only to one Group:
Groups
id
group_name
Contacts
id
group_id -> Groups.id
first_name
...
Your Contacts do not have the information about the group_name. You just store the reference to your Groups.id.
If you want to query your contacts and the name of their group, you join those two tables:
Select c.first_name, g.group_name
From contacts c
Join groups g On ( g.id = c.group_id )
If you want to change the name of a group, you do a simple update:
Update groups
Set group_name = 'Your new group name'
Where id = 99 --# The id of the group to rename
This only changes your Groups table, without changing your Contacts.
Your Foreign Key on Contacts.group_id is there to ensure the referential integrity. This means, that you are not allowed to have a contact with group_id=88 if there is no record in Groups with id=88.
Using ON DELETE CASCADE would delete all Contacts that are members of a certain group, once you delete that group.

Categories