sql - Creating MySQL relationships with foreign keys and alter statements -
i try specific because feel understanding of subject isn't quite precise well. problem understanding how create relations between tables foreign keys add alter statements. have these tables.
create table article ( content text not null, published_on date not null, created_on date not null, ); create table category( name varchar(30) not null, date_created date not null, ); create table user( income float(30, 30) not null, password varchar(30) not null, picture_url text not null, ); create table tag( description varchar(30) not null, second_priority float(30, 30) not null, );
to have make there relationships:
tag has 1 one connection category category has many 1 connection user user has 1 many connection article
and use there statements:
alter table tag add constraint foreign key (tag_id) references category (category_id); alter table category add constraint foreign key (user_id) references user (user_id); alter table user add constraint foreign key (user_id) references article (user_id);
my problem third 1 fails. when switch places of article , user constraint passes. after bit of digging , experimenting found out can't constraint 2 keys 1 either unique or primary key , other 1 not null. far don't know how continue, can please enlighten me how create 1 one, many one, 1 many , many many relationship between these tables because kinda lost , in head mess.
full stuff:
create database exam_database; use exam_database; create table article ( content text not null, published_on date not null, created_on date not null, user_id int(30) not null ); create table category( name varchar(30) not null, date_created date not null, category_id int(30) not null primary key auto_increment unique, user_id int(30) not null ); create table user( income float(30, 30) not null, password varchar(30) not null, picture_url text not null, user_id int(30) not null primary key auto_increment unique ); create table tag( description varchar(30) not null, second_priority float(30, 30) not null, tag_id int(30) not null primary key auto_increment unique ); alter table tag add constraint foreign key (tag_id) references category (category_id); alter table category add constraint foreign key (user_id) references user (user_id); alter table user add constraint foreign key (user_id) references article (user_id);
first structure has errors: missing pk, why using int(30)? etc... db should looks like:
create database exam_database; use exam_database; create table article ( id int not null primary key auto_increment unique, user_id int not null, content text not null, published_on date not null, created_on date not null ); create table category( id int not null primary key auto_increment unique, user_id int not null, name varchar(30) not null, date_created date not null ); create table user ( id int not null primary key auto_increment unique , income float(30, 30) not null, password varchar(30) not null, picture_url text not null ); create table tag ( id int not null primary key auto_increment unique, category_id int not null, description varchar(30) not null, second_priority float(30, 30) not null );
second fks should like:
alter table tag add constraint foreign key (category_id) references category (id); alter table category add constraint foreign key (user_id) references user (id); alter table article add constraint foreign key (user_id) references user (id);
Comments
Post a Comment