MYSQL on delete cascade example -


i can't figure out how use on delete cascade in example.

create table `users` (   `login` varchar(16) not null,   `password` varchar(16) not null,   primary key (`login`),   unique key `login` (`login`) ) engine=innodb default charset=utf8  create table `employees` (   `id` int(11) not null auto_increment,   `name` varchar(20) not null,   `surname` varchar(25) not null,   `birthday` date not null,   `adres` varchar(50) not null,   `telephone` varchar(25) not null,   `login` varchar(16) default null,   primary key (`id`),   unique key `login` (`login`),   constraint `employees_ibfk_1` foreign key (`login`) references `users` (`login`) ) engine=innodb default charset=utf8 

one table stores informations employee , if has account(login) there table logins , passwords.

my goals:

  1. not every employee needs have user account(that means - unique record in users table) employees.login shouldn't restricted not null.
  2. i want possiblity add new user in users , assign employee.
  3. when delete employee, related unique user deleted(if had one).

first 2 done. don't know how make 3 happen now.

you've put constraint in wrong create table statement. goes on referencing table, "users", because that's table want automatically delete when row in other table deleted.

you want put nullable "id" in "users" table, unique , foreign key "employees".


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -