Top > Ruby on Railsあれこれ > MySQLデータベースにテーブル作成

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
-
|
|
|
!
 
 
-
|
|
|
!
 
 
-
|
|
|
|
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
CREATE TABLE `rails`.`departments` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
 
CREATE TABLE `rails`.`addresses` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
 
CREATE TABLE `rails`.`employees` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  name varchar(255) not null,
  job_type varchar(30) not null,
  salary integer,
  department_id integer,
  address_id integer,
  PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
 
ALTER TABLE `rails`.`employees` ADD CONSTRAINT `fk_department` FOREIGN KEY `fk_department` (`department_id`)
    REFERENCES `departments` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;
 
ALTER TABLE `rails`.`employees` ADD CONSTRAINT `fk_address` FOREIGN KEY `fk_address` (`address_id`)
    REFERENCES `addresses` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;
 
INSERT INTO departments VALUES(1,'ACCOUNTING');
INSERT INTO departments VALUES(2,'RESEARCH');
INSERT INTO departments VALUES(3,'SALES');
INSERT INTO departments VALUES(4,'OPERATIONS');
 
INSERT INTO addresses VALUES(1,'STREET 1');
INSERT INTO addresses VALUES(2,'STREET 2');
INSERT INTO addresses VALUES(3,'STREET 3');
INSERT INTO addresses VALUES(4,'STREET 4');
INSERT INTO addresses VALUES(5,'STREET 5');
INSERT INTO addresses VALUES(6,'STREET 6');
INSERT INTO addresses VALUES(7,'STREET 7');
INSERT INTO addresses VALUES(8,'STREET 8');
INSERT INTO addresses VALUES(9,'STREET 9');
INSERT INTO addresses VALUES(10,'STREET 10');
INSERT INTO addresses VALUES(11,'STREET 11');
INSERT INTO addresses VALUES(12,'STREET 12');
INSERT INTO addresses VALUES(13,'STREET 13');
INSERT INTO addresses VALUES(14,'STREET 14');
 
INSERT INTO employees VALUES(1,'ALLEN','SALESMAN',1600,3,1);
INSERT INTO employees VALUES(2,'WARD','SALESMAN',1250,3,2);
INSERT INTO employees VALUES(3,'JONES','MANAGER',2975,2,3);
INSERT INTO employees VALUES(4,'MARTIN','SALESMAN',1250,3,4);
INSERT INTO employees VALUES(5,'BLAKE','MANAGER',2850,3,5);
INSERT INTO employees VALUES(6,'CLARK','MANAGER',2450,1,6);
INSERT INTO employees VALUES(7,'SCOTT','ANALYST',3000.0,2,7);
INSERT INTO employees VALUES(8,'KING','PRESIDENT',5000,1,8);
INSERT INTO employees VALUES(9,'TURNER','SALESMAN',1500,3,9);
INSERT INTO employees VALUES(10,'ADAMS','CLERK',1100,2,10);
INSERT INTO employees VALUES(11,'JAMES','CLERK',950,3,11);
INSERT INTO employees VALUES(12,'FORD','ANALYST',3000,2,12);
INSERT INTO employees VALUES(13,'MILLER','CLERK',1300,1,13);
INSERT INTO employees VALUES(14,'SMITH','CLERK',800,2,14);

添付ファイル: fileprepare_mysql_1.jpg 202件 [詳細]

リロード   新規 編集 凍結解除 差分 添付 複製 名前変更   ホーム 一覧 単語検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: 2012-08-08 (水) 23:25:36 (2923d)