root/trunk/data/sql/snippets-schema.sql

Revision 2, 3.4 kB (checked in by fabien, 6 years ago)

initial import

Line 
1
2 # This is a fix for InnoDB in MySQL >= 4.1.x
3 # It "suspends judgement" for fkey relationships until are tables are set.
4 SET FOREIGN_KEY_CHECKS = 0;
5
6 #-----------------------------------------------------------------------------
7 #-- sn_user
8 #-----------------------------------------------------------------------------
9
10 DROP TABLE IF EXISTS `sn_user`;
11
12
13 CREATE TABLE `sn_user`
14 (
15     `id` INTEGER  NOT NULL AUTO_INCREMENT,
16     `login` VARCHAR(10),
17     `password` VARCHAR(10),
18     `email` VARCHAR(50),
19     `first_name` VARCHAR(50),
20     `last_name` VARCHAR(50),
21     `is_admin` INTEGER default 0,
22     `created_at` DATETIME,
23     PRIMARY KEY (`id`)
24 )Type=InnoDB;
25
26 #-----------------------------------------------------------------------------
27 #-- sn_snippet
28 #-----------------------------------------------------------------------------
29
30 DROP TABLE IF EXISTS `sn_snippet`;
31
32
33 CREATE TABLE `sn_snippet`
34 (
35     `id` INTEGER  NOT NULL AUTO_INCREMENT,
36     `user_id` INTEGER,
37     `title` TEXT,
38     `body` TEXT,
39     `html_body` TEXT,
40     `created_at` DATETIME,
41     `average_vote` DOUBLE,
42     `nb_comments` INTEGER,
43     `all_tags` VARCHAR(255),
44     PRIMARY KEY (`id`),
45     INDEX `sn_snippet_FI_1` (`user_id`),
46     CONSTRAINT `sn_snippet_FK_1`
47         FOREIGN KEY (`user_id`)
48         REFERENCES `sn_user` (`id`)
49 )Type=InnoDB;
50
51 #-----------------------------------------------------------------------------
52 #-- sn_tag
53 #-----------------------------------------------------------------------------
54
55 DROP TABLE IF EXISTS `sn_tag`;
56
57
58 CREATE TABLE `sn_tag`
59 (
60     `id` INTEGER  NOT NULL AUTO_INCREMENT,
61     `user_id` INTEGER,
62     `snippet_id` INTEGER,
63     `name` VARCHAR(50),
64     `created_at` DATETIME,
65     PRIMARY KEY (`id`),
66     INDEX `sn_tag_FI_1` (`user_id`),
67     CONSTRAINT `sn_tag_FK_1`
68         FOREIGN KEY (`user_id`)
69         REFERENCES `sn_user` (`id`),
70     INDEX `sn_tag_FI_2` (`snippet_id`),
71     CONSTRAINT `sn_tag_FK_2`
72         FOREIGN KEY (`snippet_id`)
73         REFERENCES `sn_snippet` (`id`)
74         ON DELETE CASCADE
75 )Type=InnoDB;
76
77 #-----------------------------------------------------------------------------
78 #-- sn_comment
79 #-----------------------------------------------------------------------------
80
81 DROP TABLE IF EXISTS `sn_comment`;
82
83
84 CREATE TABLE `sn_comment`
85 (
86     `id` INTEGER  NOT NULL AUTO_INCREMENT,
87     `user_id` INTEGER,
88     `snippet_id` INTEGER,
89     `body` TEXT,
90     `html_body` TEXT,
91     `created_at` DATETIME,
92     PRIMARY KEY (`id`),
93     INDEX `sn_comment_FI_1` (`user_id`),
94     CONSTRAINT `sn_comment_FK_1`
95         FOREIGN KEY (`user_id`)
96         REFERENCES `sn_user` (`id`),
97     INDEX `sn_comment_FI_2` (`snippet_id`),
98     CONSTRAINT `sn_comment_FK_2`
99         FOREIGN KEY (`snippet_id`)
100         REFERENCES `sn_snippet` (`id`)
101         ON DELETE CASCADE
102 )Type=InnoDB;
103
104 #-----------------------------------------------------------------------------
105 #-- sn_vote
106 #-----------------------------------------------------------------------------
107
108 DROP TABLE IF EXISTS `sn_vote`;
109
110
111 CREATE TABLE `sn_vote`
112 (
113     `id` INTEGER  NOT NULL AUTO_INCREMENT,
114     `user_id` INTEGER,
115     `snippet_id` INTEGER,
116     `vote` INTEGER,
117     `created_at` DATETIME,
118     PRIMARY KEY (`id`),
119     INDEX `sn_vote_FI_1` (`user_id`),
120     CONSTRAINT `sn_vote_FK_1`
121         FOREIGN KEY (`user_id`)
122         REFERENCES `sn_user` (`id`),
123     INDEX `sn_vote_FI_2` (`snippet_id`),
124     CONSTRAINT `sn_vote_FK_2`
125         FOREIGN KEY (`snippet_id`)
126         REFERENCES `sn_snippet` (`id`)
127         ON DELETE CASCADE
128 )Type=InnoDB;
129
130 # This restores the fkey checks, after having unset them earlier
131 SET FOREIGN_KEY_CHECKS = 1;
Note: See TracBrowser for help on using the browser.