· 9 years ago · Oct 18, 2016, 11:44 AM
1DROP TABLE IF EXISTS measurements, c_matrices;
2CREATE TABLE measurements (
3 measurement_id SERIAL PRIMARY KEY NOT NULL,
4 db_id INTEGER NOT NULL,
5 patient_id INTEGER NOT NULL,
6 channel_id INTEGER NOT NULL,
7 c_id INTEGER NOT NULL,
8 time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
9 CONSTRAINT no_duplicate_measurements UNIQUE (time),
10 CONSTRAINT no_same_measurements UNIQUE (db_id, patient_id, channel_id, c_id)
11);
12
13CREATE TABLE c_matrices (
14 c_id SERIAL PRIMARY KEY NOT NULL,
15 measurement_id INTEGER NOT NULL,
16 event_index_start INTEGER NOT NULL,
17 event_index_end INTEGER NOT NULL,
18 c_matrix_data BYTEA NOT NULL,
19 c_size_in_bytes INTEGER NOT NULL,
20 UNIQUE (measurement_id, event_index_start, event_index_end)
21);
22
23-- http://dba.stackexchange.com/a/107062/69807
24WITH ins1 AS (
25 INSERT INTO measurements (measurement_id, db_id, patient_id, channel_id) -- TODO not sure since this autogenerated
26 VALUES (1, 2, 3, 4) -- if data comes from client app
27 -- SELECT ... if data from other db TODO how to do this here SELECT c_id FROM c_matrices WHERE measurement_id==777
28 RETURNING measurement_id -- generating a serial ID
29 )
30INSERT INTO c_matrices (measurement_id, event_index_start, event_index_end, c_matrix_data, c_size_in_bytes)
31 VALUES (1, 2, 3, 'testBinaryData', 1234)
32 RETURNING c_id -- generating a serial id -- TODO right?
33 FROM ins1;
34
35DROP TABLE
36CREATE TABLE
37CREATE TABLE
38psql:creates.sql:38: ERROR: syntax error at or near "FROM"
39LINE 10: FROM ins1;