Dropping tables mySql - mysql

I am having terrible trouble figuring out the order these tables should be dropped in for my script I will post the script now, If anyone could help with the order they should be dropped in it would be a great help
drop table Registered_Student;
drop table EducateYou_VideoRating;
drop table EducateYou_Video;
drop table EducateYou_Violation;
drop table EducateYou_Member;
drop table Violation_Type;
drop table EducateYou_Comment;
drop table EducateYou_FollowList;
drop table EducateYou_Group;
drop table EducateYou_Admin;
commit;
create table EducateYou_Violation
(
violationId number (9),
violationStatus char(1) DEFAULT 'X',
videoId number(9),
groupId number (9),
commentId number(9),
CONSTRAINT pk_violationId PRIMARY KEY (violationId),
CONSTRAINT fk_x_videoId FOREIGN KEY (videoId) REFERENCES EducateYou_Video,
CONSTRAINT fk_y_groupId FOREIGN KEY (groupId) REFERENCES EducateYou_Group,
CONSTRAINT fk_z_commentId FOREIGN KEY (commentId) REFERENCES EducateYou_Comment
);
create table EducateYou_Video(
videoId number(9),
videoRef varchar2(50) NOT NULL,
videoTitle varchar2(15) NOT NULL,
uploadDate DATE NOT NULL,
videoStatus char(1) DEFAULT 'X',
videoCategory varchar2(15) NOT NULL,
regNo number(6) NOT NULL,
AdminId number(9),
commentId number(9),
CONSTRAINT pk_videoId PRIMARY KEY (videoId),
CONSTRAINT fk_r_regNo FOREIGN KEY (regNo) REFERENCES EducateYou_Member,
CONSTRAINT fk_p_AdminId FOREIGN KEY (AdminId) REFERENCES EducateYou_Admin,
CONSTRAINT fk_n_commentId FOREIGN KEY (commentId) REFERENCES EducateYou_Comment
);
create table EducateYou_VideoRating(
rating number(2) NOT NULL,
regNo number(9),
videoId number(9),
CONSTRAINT pk_video_rating PRIMARY KEY (videoId, regNo),
CONSTRAINT fk_x_videoId FOREIGN KEY (videoId) REFERENCES EducateYou_Video,
CONSTRAINT fk_z_regNo FOREIGN KEY (regNo) REFERENCES EducateYou_Member
);
create table EducateYou_Admin
(
AdminId number(9),
firstName varchar2(15),
surname varchar2(15),
adminActions varchar2(40),
CONSTRAINT pk_AdminId PRIMARY KEY (AdminId)
);
create table EducateYou_Group(
groupID number(5) NOT NULL,
groupname varchar2(15) NOT NULL,
regNo number(6) NOT NULL,
CONSTRAINT pk_groupID PRIMARY KEY (groupID),
CONSTRAINT fk_i_regNo FOREIGN KEY (regNo) REFERENCES EducateYou_Member
);
create table EducateYou_Comment(
commentId number(9),
commentStatus char(1) DEFAULT 'A',
commentTime timestamp,
counter number(5) DEFAULT 5,
educateYouComment varchar2(50),
regNo number(6) NOT NULL,
CONSTRAINT pk_commentId PRIMARY KEY (commentId),
CONSTRAINT fk_b_regNo FOREIGN KEY (regNo) REFERENCES EducateYou_Member
);
create table EducateYou_FollowList(
followListID number(9),
regNo number(9),
CONSTRAINT pk_followListID PRIMARY KEY (followListID),
CONSTRAINT fk_z_regNo FOREIGN KEY (regNo) REFERENCES EducateYou_Member
);
create table Violation_Type
(
violation_TypeId varchar2(9),
violationId number(9),
CONSTRAINT pk_violation_TypeId PRIMARY KEY (violation_TypeId, violationId ),
CONSTRAINT fk_m_violationId FOREIGN KEY (violationId) REFERENCES EducateYou_Violation
);
create table EducateYou_Member
(
regNo number(6),
status char(1) DEFAULT 'A',
collegeId varchar2(9) NOT NULL,
CONSTRAINT pk_regNo PRIMARY KEY (regNo),
CONSTRAINT fk_l_collegeId FOREIGN KEY (collegeId) REFERENCES Registered_Student
);
create table Registered_Student
(
collegeId varchar2(9),
password varchar2(9) NOT NULL,
firstname varchar2(20) NOT NULL,
surname varchar2(20) NOT NULL,
dob date NOT NULL,
street varchar2(25) NOT NULL,
town varchar2(10) NOT NULL,
county varchar2(15) NOT NULL,
college varchar2(30) NOT NULL,
course varchar2(10) NOT NULL,
CONSTRAINT pk_collegeId PRIMARY KEY (collegeId)
);

Drop Table Syntax
Add SET FOREIGN_KEY_CHECKS = 0; in beginning to ignore the foreign key constraints while deleting the tables and use a single Drop command for all tables as once with CASCADE Option as below:
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS Registered_Student,
IF EXISTS EducateYou_VideoRating,
IF EXISTS EducateYou_Video,
IF EXISTSEducateYou_Violation,
IF EXISTS EducateYou_Member,
IF EXISTS Violation_Type,
IF EXISTS EducateYou_Comment,
IF EXISTS EducateYou_FollowList,
IF EXISTS EducateYou_Group,
IF EXISTS EducateYou_Admin
CASCADE;
Alternatively, drop all the constraints first and then drop the tables.

Related

Getting error while altering table in MySQL

I wrote a sql file to create a Database and add tables in that database.
Here is the code:
CREATE SCHEMA `employee_details`;
CREATE TABLE `employee_details`.`employee` (
`emp_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45),
`dob` DATE,
`joiningdate` DATE,
`designation_id` INT,
`gender` VARCHAR(45),
`address` VARCHAR(90),
`manager_id` INT,
PRIMARY KEY (`emp_id`)
);
CREATE TABLE `employee_details`.`designation` (
`designation_id` INT NOT NULL AUTO_INCREMENT,
`designation` VARCHAR(45),
`dept_id` INT,
`salary` FLOAT,
PRIMARY KEY (`designation_id`)
);
CREATE TABLE `employee_details`.`department` (
`dept_id` integer NOT NULL AUTO_INCREMENT,
`dept_name` VARCHAR(45),
PRIMARY KEY (`dept_id`)
);
CREATE TABLE `employee_details`.`rating` (
`emp_id` INT,
`emp_rating` INT,
`add_perks` FLOAT,
`experience` INT
);
CREATE TABLE `employee_details`.`project_assign` (
`emp_id` INT,
`project_id` INT
);
CREATE TABLE `employee_details`.`project` (
`project_id` INT NOT NULL AUTO_INCREMENT,
`project_name` VARCHAR(45),
`project_dept` INT,
PRIMARY KEY (`project_id`)
);
ALTER TABLE employee
ADD FOREIGN KEY (emp_id) REFERENCES rating(emp_id);
ALTER TABLE employee
ADD FOREIGN KEY (emp_id) REFERENCES project_assign(emp_id);
ALTER TABLE employee
ADD FOREIGN KEY (emp_id) REFERENCES employee(manager_id);
ALTER TABLE designation
ADD FOREIGN KEY (designation_id) REFERENCES employee(designation_id);
ALTER TABLE designation
ADD FOREIGN KEY (dept_id) REFERENCES department(dept_id);
ALTER TABLE department
ADD FOREIGN KEY (dept_id) REFERENCES project(project_dept);
ALTER TABLE project
ADD FOREIGN KEY (project_id) REFERENCES project_assign(project_id);
When I execute that sql file, I get below error:
15:16:50 ALTER TABLE employee ADD FOREIGN KEY (emp_id) REFERENCES rating(emp_id) Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'employee_ibfk_1' in the referenced table 'rating' 0.000 sec
I checked data types of emp_id in employee and rating. They are same. How can I fix this error?
All your foreign key relationships are backwards. It should be:
ALTER TABLE rating
ADD FOREIGN KEY (emp_id) REFERENCES employee(emp_id);
and similar for all the other foreign keys.

MySQL foreign key constraint error code 1215

Can anyone see the problem with my create statement for table contactgroup. I keep getting error code 1215 can't create foreign key constraint.
create table contact(
ContactID int(5) not null auto_increment,
ContactName varchar(255) Not null,
ContactNumber int(5),
ContactEmail varchar(255),
primary key(ContactID))ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table contactgroup(
ContactGroupID int(5) Not Null,
ContactID int(5) Not Null,
primary key(ContactGroupID),
key fk_contactgroup_ContactID (ContactID),
constraint fk_contactgroup_ContactID
foreign key(ContactID)
references contact) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You need to mention both the table and the primary key column of that table when defining a foreign key constraint:
FOREIGN KEY (ContactID) REFERENCES contact(ContactID)

can't add foreign key constraint in mysql

create table buys (
user_id INT(11) not null,
software_id INT(11) not null,
associated_id INT(11) not null,
primary key (associated_id),
foreign key (software_id) references software,
foreign key (user_id) references users,
foreign key (associated_id) references associated);
I have a ternary relationship associated table having total participation and a key constraint but cannot add foreign key constraints
Check the following query:
CREATE TABLE products(
prd_id int not null auto_increment primary key,
cat_id int not null,
FOREIGN KEY fk_cat(cat_id)
REFERENCES categories(cat_id)
)ENGINE=InnoDB;
In your case:
foreign key (software_id) references software -> here column name is missing in software table

Error 150 : table couldn't be created

Where am I going wrong? I get the error 150 debit_card table couldn't be created. Something to do the foreign keys i know, just don't know what exactly. Help?
create table customer(
cust_id int auto_increment,
first_name varchar(25),
last_name varchar(25),
street varchar(25),
city varchar(25),
state char(2),
zip varchar(10),
CONSTRAINT customer_pk primary key(cust_id)
);
create table card_account(
acct_no char(16),
exp_date date,
card_type ENUM("Debit","Credit") not null,
CONSTRAINT card_account_pk primary key(acct_no,exp_date)
);
create table debit_card(
bank_no char(9) not null,
acct_no char(16),
exp_date date,
CONSTRAINT debit_card_pk primary key(acct_no,exp_date),
CONSTRAINT debit_card_acct_no_fk foreign key(acct_no) references card_account(acct_no) on delete cascade,
CONSTRAINT debit_card_exp_date_fk foreign key(exp_date) references card_account(exp_date) on delete cascade
);
Your card_account table has a composite primary key. You cannot use those columns separately in a foreign key constraint.
But you can add a foreign key referencing to the composite primary key:
create table debit_card(
bank_no char(9) not null,
acct_no char(16),
exp_date date,
CONSTRAINT debit_card_pk primary key(acct_no,exp_date),
CONSTRAINT debit_card_acct_no_fk foreign key(acct_no,exp_date) references card_account(acct_no,exp_date) on delete cascade
);

Error Code : 1005 Can't create table 'custom_db2.durableprofile' (errno: 150)

can't create database
--------------------------------------------------------
create table DurableArticle(
durable_code varchar(30) not null,
durable_name varchar(100),
durable_number varchar(20),
durable_brandname varchar(125),
durable_modelordetail varchar(125),
durable_price varchar(20),
durable_entrance varchar(20),
constraint durable_PK primary key (durable_code)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
---------------------------------------------
create table DurableProfile(
profile_year integer not null,
profile_status varchar(100),
profile_note varchar(100),
staff_id integer not null,
owner_id integer not null,
room_id integer not null,
durable_code varchar(30) not null,
constraint profile_PK primary key (profile_year,durable_code),
constraint profile_FK1 foreign key (staff_id) references staff(staff_id),
constraint profile_FK2 foreign key (owner_id) references owner(owner_id),
constraint profile_FK3 foreign key (room_id) references room(room_id),
constraint profile_Fk4 foreign key (durable_code) references durablearticle (durable_code)
)ENGINE=InnoDB;
you reference 3 tables in the second statement, which obviously do not exist (yet).
Remove the foreign keys or create those tables, then it works.
create table DurableProfile(
profile_year integer not null,
profile_status varchar(100),
profile_note varchar(100),
staff_id integer not null,
owner_id integer not null,
room_id integer not null,
durable_code varchar(30) not null,
constraint profile_PK primary key (profile_year,durable_code),
constraint profile_Fk4 foreign key (durable_code) references durablearticle (durable_code)
)ENGINE=InnoDB;