2011-02-04 1 views
1

이 참여하지 않을 것이다 나의 데이터베이스입니다 :InnoDB의 테이블에 대한 관계를 생성 - 하나

-- Host: localhost 
-- Generation Time: Feb 04, 2011 at 01:49 PM 
-- Server version: 5.0.45 
-- PHP Version: 5.2.5 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; 

-- 
-- Database: `myepguide` 
-- 

-- -------------------------------------------------------- 
-- 
-- Table structure for table `channel1` 
-- 

CREATE TABLE IF NOT EXISTS `channel1` (
    `id` mediumint(255) NOT NULL auto_increment, 
    `channel` varchar(255) default NULL, 
    PRIMARY KEY (`id`), 
    KEY `channel` USING BTREE (`channel`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; 

-- 
-- Dumping data for table `channel1` 
-- 

INSERT INTO `channel1` (`id`, `channel`) VALUES 
(1, '<a href="channel/BBCOne.php">BBC One</a>'), 
(3, '<a href="channel/ITV1.php"><i>ITV1</i></a>'), 
(2, '<a href="channel/ITV2.php"><i>ITV2 </i></a>'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `myepguide` 
-- 

CREATE TABLE IF NOT EXISTS `myepguide` (
    `id` mediumint(9) NOT NULL auto_increment, 
    `programme` varchar(255) NOT NULL, 
    `channel` varchar(255) default NULL, 
    `airdate` datetime NOT NULL, 
    `displayair` datetime NOT NULL, 
    `expiration` datetime NOT NULL, 
    `episode` varchar(255) default '', 
    `setreminder` varchar(255) NOT NULL default '<img src="alert.gif" height="16" width="22"> <a href="setreminder.php" alt="Set a reminder" target="_top">Set Reminder</a>', 
    PRIMARY KEY (`id`), 
    KEY `programme1` USING BTREE (`programme`), 
    KEY `channel2` USING BTREE (`channel`), 
    KEY `episode` (`episode`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; 

-- 
-- Dumping data for table `myepguide` 
-- 

INSERT INTO `myepguide` (`id`, `programme`, `channel`, `airdate`, `displayair`, `expiration`, `episode`, `setreminder`) VALUES 
(1, '<a href="programmes/casualty.php">Casualty</a>', '<a href="lib/channel/ITV1"><i>BBC One </i></a>', '2011-05-18 14:30:00', '2011-05-18 14:30:00', '2011-05-18 15:00:00', 'No Fjords in Finland', '<img src="alert.gif" height="16" width="22"> <a href="setreminder.php" alt="Set a reminder" target="_top">Set Reminder</a>'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `episode` 
-- 

CREATE TABLE IF NOT EXISTS `episode` (
    `id` mediumint(9) NOT NULL auto_increment, 
    `episode` varchar(255) default NULL, 
    PRIMARY KEY (`id`), 
    KEY `episode` USING BTREE (`episode`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; 

-- 
-- Dumping data for table `episode` 
-- 

INSERT INTO `episode` (`id`, `episode`) VALUES 
(1, 'No Fjords in Finland'), 
(2, 'Casualty 25th Special'), 
(3, '<a href="Holby1.php">Palimpsest</a>'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `programme` 
-- 

CREATE TABLE IF NOT EXISTS `programme` (
    `id` mediumint(255) NOT NULL auto_increment, 
    `programme` varchar(255) default NULL, 
    PRIMARY KEY (`id`), 
    KEY `programme1` USING BTREE (`programme`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; 

-- 
-- Dumping data for table `programme` 
-- 

INSERT INTO `programme` (`id`, `programme`) VALUES 
(1, '<a href="programmes/casualty.php">Casualty</a>'); 

-- 
-- Constraints for dumped tables 
-- 

-- 
-- Constraints for table `myepguide` 
-- 
ALTER TABLE `myepguide` 
    ADD CONSTRAINT `myepguide_ibfk_1` FOREIGN KEY (`programme`) REFERENCES `myepguide` (`programme`) ON UPDATE CASCADE, 
    ADD CONSTRAINT `myepguide_ibfk_2` FOREIGN KEY (`channel`) REFERENCES `channel1` (`channel`) ON DELETE SET NULL ON UPDATE CASCADE; 

난에, 어떤 이유로 표에 그에게 myepguide을 연결하는 'episode` 표를 얻을 수 없다 phpMyAdmin을 그것은 항상 "관계가 추가되지 않았다"라고 말한다.

삭제 및 다시 생성해도 작동하지 않으므로 어떻게 해결할 수 있습니까?

모두 InnoDB 형식에 저장되어 있는데 왜 이런 일이 일어 났는지 이해할 수 없습니다.

도움을 주시면 감사하겠습니다.

+0

외래 키는 기본 키, 아니면 고유 한 컬럼에 연결해야합니다. 'myepguide'를 링크하고 있습니까? 어떤 관계를 성취하려고하는지 설명해 주시겠습니까? 프로그램 목록은 무엇입니까? 에피소드 가이드는 무엇을 제시해야합니까? –

+0

@Damir Sudarevic - 몇 가지 기본 키, 즉 에피소드 및 프로그램 표에있는 ID 필드가 있습니다. – whitstone86

답변

2

의견에서 언급했듯이, 나는 당신이 여기서 무엇을 연결하려고하는지 잘 모르므로, 처음에는 가능한 관계의 아이디어를 얻기 위해 이것을 사용할 수 있습니다.

enter image description here

편집

create table TvSeries (
     TvSeriesID int not null auto_increment 
-- , other fields here 
) ENGINE=InnoDB ; 
alter table TvSeries 
     add constraint pk_TvSeries primary key (TvSeriesID) ; 

create table Episode (
     TvSeriesID int not null 
    , EpisodeNo int not null 
-- , other fields here 
) ENGINE=InnoDB ; 
alter table Episode 
     add constraint pk_Episode primary key (TvSeriesID, EpisodeNo) 
    , add constraint fk1_Episode foreign key (TvSeriesID) references TvSeries (TvSeriesID) ; 


create table Channel (
     ChannelID int not null 
-- , other fields here 
) ENGINE=InnoDB ; 
alter table Channel 
     add constraint pk_Channel primary key (ChannelID); 


create table Programme (
     ChannelID int  not null 
    , StartTime datetime not null 
    , TvSeriesID int  not null 
    , EpisodeNo int  not null 
-- , other fields here 
) ENGINE=InnoDB ; 
alter table Programme 
     add constraint pk_Programme primary key (ChannelID, StartTime) 
    , add constraint fk1_Programme foreign key (ChannelID) references Channel (ChannelID) 
    , add constraint fk2_Programme foreign key (TvSeriesID, EpisodeNo) references Episode (TvSeriesID, EpisodeNo) ; 


create table myEpisodeGuide (
     TvSeriesID int  not null 
    , EpisodeNo int  not null 
    , ChannelID int  not null 
    , StartTime datetime not null 
    , SetReminder int  not null 
-- , other fields here 
) ENGINE=InnoDB ; 
alter table myEpisodeGuide 
     add constraint pk_myEpisodeGuide primary key (TvSeriesID, EpisodeNo, ChannelID, StartTime) 
    , add constraint fk1_myEpisodeGuide foreign key (TvSeriesID, EpisodeNo) references Episode (TvSeriesID, EpisodeNo) 
    , add constraint fk2_myEpisodeGuide foreign key (ChannelID, StartTime) references Programme (ChannelID, StartTime) ; 
+0

phpmyadmin에서 어떻게합니까? – whitstone86

+0

@ whitstone86 - 편집을 참조하십시오. –