MySQL: "CREATE DATABASE" results in MySQL Error Code: 1064 - mysql

I recently bought this book called "SQL Queries for Mere Mortals (3rd Edition" to study SQL. It came with MySQL scripts that they said I could run and have example databases to work with and follow along with the book. However, some of the scripts are resulting in an error message. Here is one example script that will not work:
CREATE DATABASE EntertainmentAgencyModify;
USE EntertainmentAgencyModify;
CREATE TABLE Agents (
AgentID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
AgtFirstName nvarchar (25) NULL ,
AgtLastName nvarchar (25) NULL ,
AgtStreetAddress nvarchar (50) NULL ,
AgtCity nvarchar (30) NULL ,
AgtState nvarchar (2) NULL ,
AgtZipCode nvarchar (10) NULL ,
AgtPhoneNumber nvarchar (15) NULL ,
DateHired date NULL ,
Salary decimal(15, 2) NULL DEFAULT 0 ,
CommissionRate float(24) NULL
);
CREATE TABLE Customers (
CustomerID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
CustFirstName nvarchar (25) NULL ,
CustLastName nvarchar (25) NULL ,
CustStreetAddress nvarchar (50) NULL ,
CustCity nvarchar (30) NULL ,
CustState nvarchar (2) NULL ,
CustZipCode nvarchar (10) NULL ,
CustPhoneNumber nvarchar (15) NULL
);
CREATE TABLE Engagements (
EngagementNumber int NOT NULL AUTO_INCREMENT PRIMARY KEY,
StartDate date NULL ,
EndDate date NULL ,
StartTime time NULL ,
StopTime time NULL ,
ContractPrice decimal(15,2) NULL DEFAULT 0 ,
CustomerID int NULL DEFAULT 0 ,
AgentID int NULL DEFAULT 0 ,
EntertainerID int NULL DEFAULT 0
);
CREATE TABLE Engagements_Archive (
EngagementNumber int NOT NULL ,
StartDate date NULL ,
EndDate date NULL ,
StartTime time NULL ,
StopTime time NULL ,
ContractPrice decimal(15,2) NULL ,
CustomerID int NULL ,
AgentID int NULL ,
EntertainerID int NULL
);
CREATE TABLE Entertainer_Members (
EntertainerID int NOT NULL ,
MemberID int NOT NULL DEFAULT 0 ,
Status smallint NULL DEFAULT 0
);
CREATE TABLE Entertainer_Styles (
EntertainerID int NOT NULL ,
StyleID int NOT NULL DEFAULT 0
);
CREATE TABLE Entertainers (
EntertainerID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
EntStageName nvarchar (50) NULL ,
EntSSN nvarchar (12) NULL ,
EntStreetAddress nvarchar (50) NULL ,
EntCity nvarchar (30) NULL ,
EntState nvarchar (2) NULL ,
EntZipCode nvarchar (10) NULL ,
EntPhoneNumber nvarchar (15) NULL ,
EntWebPage nvarchar (50) NULL ,
EntEMailAddress nvarchar (50) NULL ,
DateEntered date NULL ,
EntPricePerDay decimal(15,2) NULL
);
CREATE TABLE Members (
MemberID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
MbrFirstName nvarchar (25) NULL ,
MbrLastName nvarchar (25) NULL ,
MbrPhoneNumber nvarchar (15) NULL ,
Gender nvarchar (2) NULL
);
CREATE TABLE Musical_Preferences (
CustomerID int NOT NULL DEFAULT 0 ,
StyleID int NOT NULL DEFAULT 0
);
CREATE TABLE Musical_Styles (
StyleID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
StyleName nvarchar (75) NULL
);
CREATE INDEX AgtZipCode ON Agents(AgtZipCode);
CREATE INDEX CustZipCode ON Customers(CustZipCode);
CREATE INDEX AgentsEngagements ON Engagements(AgentID);
CREATE INDEX CustomerID ON Engagements(CustomerID);
CREATE INDEX EmployeeID ON Engagements(AgentID);
CREATE INDEX EntertainerID ON Engagements(EntertainerID);
ALTER TABLE Engagements_Archive
ADD CONSTRAINT Engagements_Archive_PK PRIMARY KEY
(
EngagementNumber
);
CREATE INDEX CustomerID ON Engagements_Archive(CustomerID);
CREATE INDEX EmployeeID ON Engagements_Archive(AgentID);
CREATE INDEX EntertainerID ON Engagements_Archive(EntertainerID);
ALTER TABLE Entertainer_Members
ADD CONSTRAINT Entertainer_Members_PK PRIMARY KEY
(
EntertainerID,
MemberID
);
CREATE INDEX EntertainersEntertainer_Members ON Entertainer_Members(EntertainerID);
CREATE INDEX MembersEntertainer_Members ON Entertainer_Members(MemberID);
ALTER TABLE Entertainer_Styles
ADD CONSTRAINT Entertainer_Styles_PK PRIMARY KEY
(
EntertainerID,
StyleID
);
CREATE INDEX EntertainersEntertainer_Styles ON Entertainer_Styles(EntertainerID);
CREATE INDEX Musical_StylesEntertainer_Styles ON Entertainer_Styles(StyleID);
CREATE INDEX EntZipCode ON Entertainers(EntZipCode);
ALTER TABLE Musical_Preferences
ADD CONSTRAINT Musical_Preferences_PK PRIMARY KEY
(
CustomerID,
StyleID
);
CREATE INDEX CustomerID ON Musical_Preferences(CustomerID);
CREATE INDEX StyleID ON Musical_Preferences(StyleID);
ALTER TABLE Engagements
ADD CONSTRAINT Engagements_FK00 FOREIGN KEY
(
AgentID
) REFERENCES Agents (
AgentID
),
ADD CONSTRAINT Engagements_FK01 FOREIGN KEY
(
CustomerID
) REFERENCES Customers (
CustomerID
),
ADD CONSTRAINT Engagements_FK02 FOREIGN KEY
(
EntertainerID
) REFERENCES Entertainers (
EntertainerID
);
ALTER TABLE Entertainer_Members
ADD CONSTRAINT Entertainer_Members_FK00 FOREIGN KEY
(
EntertainerID
) REFERENCES Entertainers (
EntertainerID
),
ADD CONSTRAINT Entertainer_Members_FK01 FOREIGN KEY
(
MemberID
) REFERENCES Members (
MemberID
);
ALTER TABLE Entertainer_Styles
ADD CONSTRAINT Entertainer_Styles_FK00 FOREIGN KEY
(
EntertainerID
) REFERENCES Entertainers (
EntertainerID
) ON DELETE CASCADE,
ADD CONSTRAINT Entertainer_Styles_FK01 FOREIGN KEY
(
StyleID
) REFERENCES Musical_Styles
(
StyleID
);
ALTER TABLE Musical_Preferences
ADD CONSTRAINT Musical_Preferences_FK00 FOREIGN KEY
(
CustomerID
) REFERENCES Customers (
CustomerID
) ON DELETE CASCADE,
ADD CONSTRAINT Musical_Preferences_FK01 FOREIGN KEY
(
StyleID
) REFERENCES Musical_Styles (
StyleID
);
Running this script results in the following error message:
16:33:35 CREATE DATABASE EntertainmentAgencyModify Error Code: 1064.
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'CREATE DATABASE EntertainmentAgencyModify' at line 1 0.00041
sec
Screenshot
I have tried running other scripts that came with the book that also starts with "CREATE DATABASE" and some of them ran smoothly without any errors, so I'm confused as to why I'm getting this error message. Any help would be greatly appreciated! Thanks all!

Note that since this script has a "CREATE DATABASE" at the top it will fail if that database already exists. That also means that if it works partially and you run it again, it;ll fail completely the next time. "CREATE DATABASE" is a pretty big hammer, so you probably don't want to get into the habit of doing it a lot. But, having said that, here's an even bigger hammer; add
DROP DATABASE IF EXISTS EntertainmentAgencyModify;
in front of the CREATE and similarly for the others. When you have real data, you, of course, never want to use either of these.

Can you try running the following just by typing it in your mysql console:
CREATE DATABASE EntertainmentAgencyModify;
It should simply work, or at least tell you something more then a syntax error.

The code executes fine. May be its a problem with your MySql.
Try to install a new MySql. :)
https://www.mysql.com/downloads/

Thanks everyone! I have no idea why this works, but I found a solution. I had to simply delete "CREATE " from the first line of the script and re-type it. Then it worked. If anyone has any idea why this seemingly irrelevant solution worked, I'd love to know. Thanks everyone for your help!

Related

How to write mysql query, where i need to put order by before group by

//I know that there are questions like this before, but I can't get this to work
So, I have 3 tables, seats(id, type_id, hall_id), seat_types(id(PK), type_name) and taken_seats(screening_id(PK), seat_id(PK), type_id).
I need to write a query where I pass a screening ID and hall ID, and get back seat_id,type_name, and isTaken (if seat is taken or not).
The problem I have is that I have a query that return a duplicate if I do ORDER BY, but if I do GROUP BY I don't get correct data back because I didn't ordered it.
My query:
USE cinema_app;
SELECT
seats.id,
seat_types.type_name,
taken_seats.screening_id,
CASE
WHEN taken_seats.screening_id = 3 THEN 'ATaken'
ELSE 'Not taken'
END AS isTaken
FROM seats
JOIN seat_types ON seats.type_id = seat_types.id
LEFT JOIN taken_seats ON taken_seats.seat_id = seats.id
WHERE seats.hall_id = 2
ORDER BY seats.id, isTaken;
Table I'm getting back:
https://imgur.com/tnmCIHi
(id 181)
Database create script:
DROP DATABASE cinema_app;
CREATE DATABASE cinema_app CHARACTER SET UTF8MB4;
USE cinema_app;
CREATE TABLE roles (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
role_name VARCHAR(20) NOT NULL
);
CREATE TABLE ranks (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
rank_name VARCHAR(20) NOT NULL
);
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
birthdate DATE NOT NULL,
email VARCHAR(100) NOT NULL,
pass VARCHAR(20) NOT NULL,
phone_number VARCHAR(20) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
role_id INT NOT NULL,
FOREIGN KEY (role_id) REFERENCES roles(id)
);
CREATE TABLE user_ranks (
user_id INT NOT NULL,
rank_id INT NOT NULL,
PRIMARY KEY (user_id, rank_id),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (rank_id) REFERENCES ranks(id)
);
CREATE TABLE movies (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
TMDB_id INT NOT NULL
);
CREATE TABLE spectacles (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
poster_img MEDIUMBLOB,
time_length TIME NOT NULL
);
CREATE TABLE hall_types (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
type_name VARCHAR(10) NOT NULL
);
CREATE TABLE halls (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
type_id INT NOT NULL,
FOREIGN KEY (type_id) REFERENCES hall_types(id)
);
CREATE TABLE screening_types (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
type_name VARCHAR(20) NOT NULL
);
CREATE TABLE screening_visions (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
vision_name VARCHAR(10) NOT NULL
);
CREATE TABLE screenings (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
starting_date_time TIMESTAMP NOT NULL,
ending_date_time TIMESTAMP NOT NULL,
first_pause BOOL NOT NULL,
second_pause BOOL NOT NULL,
ticket_price DECIMAL(4,2) NOT NULL,
private_screening BOOL NOT NULL,
hall_id INT NOT NULL,
movie_id INT,
spectacle_id INT,
type_id INT NOT NULL,
vision_id INT NOT NULL,
FOREIGN KEY (hall_id) REFERENCES halls(id),
FOREIGN KEY (movie_id) REFERENCES movies(id),
FOREIGN KEY (spectacle_id) REFERENCES spectacles(id),
FOREIGN KEY (type_id) REFERENCES screening_types(id),
FOREIGN KEY (vision_id) REFERENCES screening_visions(id)
);
CREATE TABLE private_screening_reqs (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
starting_date_time TIMESTAMP NOT NULL,
ending_date_time TIMESTAMP NOT NULL,
first_pause BOOL NOT NULL,
second_pause BOOL NOT NULL,
people_number INT NOT NULL,
price DECIMAL(5,2) NOT NULL,
TMDB_id INT NOT NULL,
hall_id INT NOT NULL,
user_id INT NOT NULL,
FOREIGN KEY (hall_id) REFERENCES halls(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE seat_types (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
type_name VARCHAR(15) NOT NULL
);
CREATE TABLE seats (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
type_id INT NOT NULL,
hall_id INT NOT NULL,
FOREIGN KEY (hall_id) REFERENCES halls(id),
FOREIGN KEY (type_id) REFERENCES seat_types(id)
);
CREATE TABLE taken_seats (
screening_id INT NOT NULL,
seat_id INT NOT NULL,
user_id INT,
PRIMARY KEY (screening_id, seat_id),
FOREIGN KEY (screening_id) REFERENCES screenings(id),
FOREIGN KEY (seat_id) REFERENCES seats(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
SAMPLE data (CSV):
hall_types:
id,type_name
1,2D
2,"2D Extreme"
3,3D
4,"3D Extreme"
5,"Real 3D"
6,4DX
halls:
id,type_id
1,1
2,2
3,2
4,5
5,6
screening_types:
id,type_name
1,Normal
2,Premiere
3,Preprempere
4,"Late night"
5,Kids
screening_visions:
id,vision_name
1,2D
2,"2D Extreme"
3,3D
4,"3D Extreme"
5,"Real 3D"
6,4DX
movies:
id,TMDB_id
1,255
2,314
3,142
screenings:
id,starting_date_time,ending_date_time,first_pause,second_pause,ticket_price,private_screening,hall_id,movie_id,spectacle_id,type_id,vision_id
1,"2022-03-24 19:00:00","2022-03-24 20:00:00",0,0,8.00,0,2,1,NULL,1,2
2,"2022-03-24 21:30:00","2022-03-24 22:45:00",1,0,7.50,0,1,1,NULL,1,1
3,"2022-03-24 20:10:00","2022-03-24 22:10:00",1,0,9.00,0,2,2,NULL,2,2
4,"2022-03-25 20:10:00","2022-03-25 22:10:00",0,0,7.00,0,3,3,NULL,2,3
seat_types:
id,type_name
1,Normal
2,VIP
3,LoveBox
seats:
id,type_id,hall_id
1,1,1
2,1,1
3,1,1
4,1,1
5,1,1
6,1,1
7,1,1
8,1,1
9,1,1
10,1,1
11,1,1
12,1,1
13,1,1
14,1,1
15,1,1
16,1,1
17,1,1
18,1,1
19,1,1
20,1,1
21,1,1
22,1,1
23,1,1
24,1,1
25,1,1
26,1,1
27,1,1
28,1,1
29,1,1
30,1,1
31,1,1
32,1,1
33,1,1
34,1,1
35,1,1
36,1,1
37,1,1
38,1,1
39,1,1
40,1,1
41,1,1
42,1,1
43,1,1
44,1,1
45,1,1
46,1,1
47,1,1
48,1,1
49,1,1
50,1,1
51,1,1
52,1,1
53,1,1
54,1,1
55,1,1
56,1,1
57,1,1
58,1,1
59,1,1
60,1,1
61,1,1
62,1,1
63,1,1
64,1,1
65,1,1
66,1,1
67,1,1
68,1,1
69,1,1
70,1,1
71,1,1
72,1,1
73,1,1
74,1,1
75,1,1
76,1,1
77,1,1
78,1,1
79,1,1
80,1,1
81,1,1
82,1,1
83,1,1
84,1,1
85,1,1
86,1,1
87,1,1
88,1,1
89,1,1
90,1,1
91,1,1
92,1,1
93,1,1
94,1,1
95,1,1
96,1,1
97,1,1
98,1,1
99,1,1
100,1,1
101,1,1
102,1,1
103,1,1
104,1,1
105,1,1
106,1,1
107,1,1
108,1,1
109,1,1
110,1,1
111,1,1
112,1,1
113,1,1
114,1,1
115,1,1
116,1,1
117,1,1
118,1,1
119,1,1
120,2,1
121,2,1
122,2,1
123,2,1
124,2,1
125,2,1
126,2,1
127,2,1
128,2,1
129,2,1
130,2,1
131,2,1
132,2,1
133,2,1
134,2,1
135,2,1
136,2,1
137,3,1
138,3,1
139,3,1
140,3,1
141,3,1
142,3,1
143,3,1
144,3,1
145,3,1
146,1,2
147,1,2
148,1,2
149,1,2
150,1,2
151,1,2
152,1,2
153,1,2
154,1,2
155,1,2
156,1,2
157,1,2
158,1,2
159,1,2
160,1,2
161,1,2
162,1,2
163,1,2
164,1,2
165,1,2
166,1,2
167,1,2
168,1,2
169,1,2
170,1,2
171,1,2
172,1,2
173,1,2
174,1,2
175,1,2
176,1,2
177,1,2
178,1,2
179,1,2
180,1,2
181,1,2
182,1,2
183,1,2
184,1,2
185,1,2
186,1,2
187,1,2
188,1,2
189,1,2
190,1,2
191,1,2
192,1,2
193,1,2
194,1,2
195,1,2
196,1,2
197,1,2
198,1,2
199,1,2
200,1,2
201,1,2
202,1,2
203,1,2
204,1,2
205,1,2
206,1,2
207,1,2
208,1,2
209,1,2
210,1,2
211,1,2
212,1,2
213,1,2
214,1,2
215,1,2
216,1,2
217,1,2
218,1,2
219,1,2
220,1,2
221,1,2
222,1,2
223,1,2
224,1,2
225,1,2
226,1,2
227,1,2
228,1,2
229,1,2
230,1,2
231,1,2
232,1,2
233,1,2
234,1,2
235,1,2
236,1,2
237,1,2
238,1,2
239,1,2
240,1,2
241,1,2
242,1,2
243,1,2
244,1,2
245,1,2
246,1,2
247,1,2
248,1,2
249,1,2
250,1,2
251,1,2
252,1,2
253,1,2
254,1,2
255,1,2
256,1,2
257,1,2
258,1,2
259,1,2
260,1,2
261,1,2
262,1,2
263,1,2
264,1,2
265,1,2
266,1,2
267,1,2
268,1,2
269,1,2
270,1,2
271,1,2
272,1,2
273,1,2
274,1,2
275,1,2
276,1,2
277,1,2
278,1,2
279,1,2
280,1,2
281,1,2
282,1,2
283,1,2
284,1,2
285,1,2
286,1,2
287,1,2
288,1,2
289,1,2
290,1,2
291,1,2
292,1,2
293,1,2
294,1,2
295,1,2
296,1,2
297,1,2
298,1,2
299,1,2
300,1,2
301,1,2
302,1,2
303,1,2
304,1,2
305,1,2
306,1,2
307,1,2
308,1,2
309,1,2
310,1,2
311,1,2
312,1,2
313,1,2
314,1,2
315,1,2
316,1,2
317,1,2
318,1,2
319,1,2
320,1,2
321,1,2
322,1,2
323,1,2
324,1,2
325,1,2
326,1,2
327,1,2
328,1,2
329,1,2
330,1,2
331,1,2
332,1,2
333,1,2
334,1,2
335,1,2
336,1,2
337,1,2
338,1,2
339,1,2
340,1,2
341,1,2
342,1,2
343,1,2
344,1,2
345,1,2
346,2,2
347,2,2
348,2,2
349,2,2
350,2,2
351,2,2
352,2,2
353,2,2
354,2,2
355,2,2
356,2,2
357,2,2
358,2,2
359,2,2
360,2,2
361,2,2
362,2,2
363,2,2
364,2,2
365,2,2
366,3,2
367,3,2
368,3,2
369,3,2
370,3,2
371,3,2
372,3,2
373,3,2
374,3,2
375,3,2
376,3,2
377,1,3
378,1,3
379,1,3
380,1,3
381,1,3
382,1,3
383,1,3
384,1,3
385,1,3
386,1,3
387,1,3
388,1,3
389,1,3
390,1,3
391,1,3
392,1,3
393,1,3
394,1,3
395,1,3
396,1,3
397,1,3
398,1,3
399,1,3
400,1,3
401,1,3
402,1,3
403,1,3
404,1,3
405,1,3
406,1,3
407,1,3
408,1,3
409,1,3
410,1,3
411,1,3
412,1,3
413,1,3
414,1,3
415,1,3
416,1,3
417,1,3
418,1,3
419,1,3
420,1,3
421,1,3
422,1,3
423,1,3
424,1,3
425,1,3
426,1,3
427,1,3
428,1,3
429,1,3
430,1,3
431,1,3
432,1,3
433,1,3
434,1,3
435,1,3
436,1,3
437,1,3
438,1,3
439,1,3
440,1,3
441,1,3
442,1,3
443,1,3
444,1,3
445,1,3
446,1,3
447,1,3
448,1,3
449,1,3
450,1,3
451,1,3
452,1,3
453,1,3
454,1,3
455,1,3
456,1,3
457,1,3
458,1,3
459,1,3
460,1,3
461,2,3
462,2,3
463,2,3
464,2,3
465,2,3
466,2,3
467,2,3
468,2,3
469,2,3
470,2,3
471,2,3
472,2,3
473,3,3
474,3,3
475,3,3
476,3,3
477,3,3
478,3,3
479,1,4
480,1,4
481,1,4
482,1,4
483,1,4
484,1,4
485,1,4
486,1,4
487,1,4
488,1,4
489,1,4
490,1,4
491,1,4
492,1,4
493,1,4
494,1,4
495,1,4
496,1,4
497,1,4
498,1,4
499,1,4
500,1,4
501,1,4
502,1,4
503,1,4
504,1,4
505,1,4
506,1,4
507,1,4
508,1,4
509,1,4
510,1,4
511,1,4
512,1,4
513,1,4
514,1,4
515,1,4
516,1,4
517,1,4
518,1,4
519,1,4
520,1,4
521,1,4
522,1,4
523,1,4
524,1,4
525,1,4
526,1,4
527,1,4
528,1,4
529,1,4
530,1,4
531,1,4
532,1,4
533,1,4
534,1,4
535,1,4
536,1,4
537,1,4
538,1,4
539,1,4
540,1,4
541,1,4
542,1,4
543,1,4
544,1,4
545,1,4
546,1,4
547,1,4
548,1,4
549,1,4
550,1,4
551,1,4
552,1,4
553,1,4
554,1,4
555,1,4
556,1,4
557,1,4
558,1,4
559,1,4
560,1,4
561,1,4
562,1,4
563,1,4
564,1,4
565,1,4
566,1,4
567,1,4
568,1,4
569,1,4
570,1,4
571,1,4
572,1,4
573,1,4
574,1,4
575,1,4
576,1,4
577,1,4
578,1,4
579,1,4
580,1,4
581,1,4
582,1,4
583,1,4
584,1,4
585,1,4
586,1,4
587,1,4
588,1,4
589,1,4
590,1,4
591,1,4
592,1,4
593,1,4
594,1,4
595,1,4
596,1,4
597,1,4
598,1,4
599,1,4
600,1,4
601,1,4
602,1,4
603,1,4
604,1,4
605,1,4
606,1,4
607,2,4
608,2,4
609,2,4
610,2,4
611,2,4
612,2,4
613,2,4
614,2,4
615,2,4
616,2,4
617,2,4
618,2,4
619,2,4
620,2,4
621,2,4
622,2,4
623,3,4
624,3,4
625,3,4
626,3,4
627,3,4
628,3,4
629,3,4
630,3,4
631,1,5
632,1,5
633,1,5
634,1,5
635,1,5
636,1,5
637,1,5
638,1,5
639,1,5
640,1,5
641,1,5
642,1,5
643,1,5
644,1,5
645,1,5
646,1,5
647,1,5
648,1,5
649,1,5
650,1,5
651,1,5
652,1,5
653,1,5
654,1,5
655,1,5
656,1,5
657,1,5
658,1,5
659,1,5
660,1,5
661,1,5
662,1,5
663,1,5
664,1,5
665,1,5
666,1,5
667,1,5
668,1,5
669,1,5
670,1,5
671,1,5
672,1,5
673,1,5
674,1,5
675,1,5
676,1,5
677,1,5
678,1,5
679,1,5
680,1,5
681,1,5
682,1,5
683,1,5
684,1,5
685,1,5
686,1,5
687,1,5
688,1,5
689,1,5
690,1,5
691,1,5
692,1,5
693,1,5
694,1,5
695,1,5
696,1,5
697,2,5
698,2,5
699,2,5
700,2,5
701,2,5
702,2,5
703,2,5
704,2,5
705,2,5
706,2,5
707,2,5
708,3,5
709,3,5
710,3,5
711,3,5
712,3,5
taken_seats:
screening_id,seat_id,user_id
1,176,NULL
1,181,NULL
2,1,NULL
2,4,NULL
2,5,NULL
3,178,NULL
3,179,NULL
3,180,NULL
3,181,NULL
3,200,NULL
I tried to add GROUP BY and it didn't work because i didn't get ordered first.
Also I tried writing a subquery but I didn't succeed in that.
Why do you join all taken seats for all screenings when you are only interested in screening 3? Make sure you join the taken seats for the screening in question.
SET #hall_id = 2;
SET #screening_id = 3;
SELECT
s.id AS seat_id,
st.type_name AS seat_type,
#screening_id AS screening_id,
CASE WHEN ts.screening_id IS NOT NULL
THEN 'Taken'
ELSE 'Not taken'
END AS is_taken
FROM seats s
JOIN seat_types st ON seats.type_id = seat_types.id
LEFT JOIN taken_seats ts ON ts.seat_id = s.id AND ts.screening_id = #screening_id
WHERE s.hall_id = #hall_id
ORDER BY s.id;
You could try joining the table as sql expressions
SELECT * FROM table1
LEFT JOIN (
SELECT * FROM another ORDER BY something
) as table2
ON table1.id = table2.id
GROUP BY something
Not sure if that structure is correct but that's what I would do.

advice on database design

I am building a database for my website. I have a schema here : enrolin.in/download.png
Introduction to website:
It collects data from students on behalf of colleges.
Now what I have done in database is that I have created a UDT-where data that is fixed to a student is stored. and CST-where data that can change with every new course is stored.
I am very new to database design, so please have a look at the database design and advice me where i can improve. I want it to be perfect so that there are no complications/limitations with database in future
NOTES:
There is not just for one college. It is like a platform where different colleges can add their courses. Every college has different courses with different subjects etc. There can be many CST's, may be a separate CST for every new course. Plus I am not sure about the way I am storing course data , I mean CST and UDT. Every college will have access to the data of students which have applied for a course under that college and every student will have access to data to his previously filled form data,status etc.
Thanks in advance. If anything is unclear, please ask in comments.
Update : -- tables
-- Table CST
CREATE TABLE CST (
cst_id int NOT NULL ,
rollno int NOT NULL ,
semester int NOT NULL ,
CONSTRAINT CST_pk PRIMARY KEY (cst_id)
);
-- Table UDT
CREATE TABLE UDT (
udt_id int NOT NULL ,
id int NOT NULL ,
name varchar(255) NOT NULL ,
gender varchar(255) NOT NULL ,
fatherame varchar(255) NOT NULL ,
mothername varchar(255) NOT NULL ,
dob date NOT NULL ,
signature binary(255) NOT NULL ,
CONSTRAINT UDT_pk PRIMARY KEY (udt_id)
);
-- Table address
CREATE TABLE address (
address_id int NOT NULL ,
id int NOT NULL ,
add_name varchar(255) NOT NULL ,
add_street varchar(255) NOT NULL ,
city varchar(255) NOT NULL ,
state varchar(255) NOT NULL ,
country varchar(255) NOT NULL ,
pin int NOT NULL ,
CONSTRAINT address_pk PRIMARY KEY (address_id)
);
-- Table courses
CREATE TABLE courses (
course_id int NOT NULL ,
course_name varchar(255) NOT NULL ,
CONSTRAINT courses_pk PRIMARY KEY (course_id)
);
-- Table phones
CREATE TABLE phones (
phone_id int NOT NULL ,
id int NOT NULL ,
phone int NOT NULL ,
CONSTRAINT phones_pk PRIMARY KEY (phone_id)
);
-- Table photos
CREATE TABLE photos (
photo_id int NOT NULL ,
id int NOT NULL ,
photo binary(255) NOT NULL ,
CONSTRAINT photos_pk PRIMARY KEY (photo_id)
);
-- Table sub_trans
CREATE TABLE sub_trans (
sub_trans_id int NOT NULL ,
transactions_t_id int NOT NULL ,
subjects_sub_id int NOT NULL ,
CONSTRAINT sub_trans_pk PRIMARY KEY (sub_trans_id)
);
-- Table subjects
CREATE TABLE subjects (
sub_id int NOT NULL ,
course_id int NOT NULL ,
subjectname varchar(255) NOT NULL ,
CONSTRAINT subjects_pk PRIMARY KEY (sub_id)
);
-- Table transactions
CREATE TABLE transactions (
t_id int NOT NULL ,
id int NOT NULL ,
course_id int NOT NULL ,
p_status int NOT NULL DEFAULT 0 ,
phones_phone_id int NOT NULL ,
UDT_udt_id int NOT NULL ,
photos_photo_id int NOT NULL ,
address_address_id int NOT NULL ,
CST_cst_id int NOT NULL ,
CONSTRAINT transactions_pk PRIMARY KEY (t_id)
);
-- Table users
CREATE TABLE users (
id int NOT NULL ,
name varchar(255) NOT NULL ,
email varchar(255) NOT NULL ,
password int NOT NULL ,
CONSTRAINT users_pk PRIMARY KEY (id)
);
`
UDT:
Typo fathername. UDT has an FK to what? Users? Slim down the varchar size in UDT. Sure you want a single column for name? Ok, at least document that you chose to not have more than 2 parents. Otherwise you need another table.
Users:
Passwords run thru a hash function won't be ints and user needs a Salt for optimal security. Imho.
awaiting more clarification below
There is zero purpose of UDT and users both existing. Pick one, pickup lost fields from one you are ditching.
Even hypothetically if it wasn't 1:1 name would denomalize the database.
phones:
Your columns for phones need to be described. Also can a user have more than 1 phone (if so, phoneType varchar(10) or int code? )
Also I would go like varchar(30) on phone value not an int.
Addresses:
Same deal as phones. Can a user have more than one? PostalCode pin should be varchar
Transactions:
This seems to be the hub of your schema. All of your transactions hinge on FKs to data that may have subsequently changed. I see RISK in that. Older transaction could get skewed results for lookups. There are easy ways to fix this issue if you even see it as one. Same goes for courses. What if History 101's id stays the same but next semester that id is Early Roman Empire? Just things to think about.
Lastly the documented tables have no FK relationships shown.

SQL error: 1364 Field 'XXXX' doesn't have a default value

So I have, table courses:
CREATE TABLE IF NOT EXISTS `AppDziennik`.`courses` (
`id_course` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(80) NOT NULL ,
`teachers_id_teacher` INT NOT NULL ,
`end_date` DATE NULL ,
`about` VARCHAR(255) NULL ,
`start_date` DATE NULL ,
PRIMARY KEY (`id_course`) ,
UNIQUE INDEX `id_course_UNIQUE` (`id_course` ASC) ,
CONSTRAINT `fk_courses_teachers1`
FOREIGN KEY (`teachers_id_teacher` )
REFERENCES `AppDziennik`.`teachers` (`id_teacher` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
and second table
CREATE TABLE IF NOT EXISTS `AppDziennik`.`teachers` (
`id_teacher` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`surname` VARCHAR(45) NOT NULL ,
`about` VARCHAR(255) NULL ,
`id_class` INT NULL ,
`rank` VARCHAR(45) NULL ,
`logins_id_login` INT NOT NULL ,
PRIMARY KEY (`id_teacher`) ,
INDEX `fk_teachers_classes1_idx` (`id_class` ASC) ,
INDEX `fk_teachers_logins1_idx` (`logins_id_login` ASC) ,
CONSTRAINT `fk_teachers_classes1`
FOREIGN KEY (`id_class` )
REFERENCES `AppDziennik`.`classes` (`id_class` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_teachers_logins1`
FOREIGN KEY (`logins_id_login` )
REFERENCES `AppDziennik`.`logins` (`id_login` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
When I make that insert
insert into appdziennik.courses
(name, id_teacher, about, start_date, end_date)
values ("Math",'7',"Math",'2013-08-01','2014-06-29');
I get that error:
Error Code: 1364. Field 'teachers_id_teacher' doesn't have a default
value
Where I made mistake? How i could fix it.
If you're sending an INSERT to table courses, there is no field id_teacher (you want teachers_id_teacher). Since you're using a foreign key, that key must exist in table teachers before you INSERT a record into table courses. Also, you should only be using backticks (`) and single quotes (') in all your statements.
you are trying to insert into the courses table for the column id_teacher a value but your not inserting a value for the column teachers_id_teacher. so I would propose to make the statement as followed:
insert into appdziennik.courses (name, teachers_id_teacher, about, start_date, end_date) values ("Math",'7',"Math",'2013-08-01','2014-06-29');
Sarajog

How to Add integer column to an String column in MySQl 5.0

I Want to add an Integer Column to a String that's because i need to generate a varchar variable with a numeric part that automatically increments. For example, P000001,P000002...
In order to do that what i am doing while creation of table i have taken an int field ID which auto_increments and i am Concatenating P with 00000 and the ID value
The Table i have created is :
CREATE TABLE tblAcceptTest(
ID int AUTO_INCREMENT NOT NULL primary key,
PatientID as CONCAT('P' , CONCAT('000000',CAST(ID as char)))
);
It Shows me the error from as keyword.
Please help
MySQL's documentation (http://dev.mysql.com/doc/refman/5.1/en/create-table.html) says, "the default value must be a constant; it cannot be a function or an expression." Why don't you just get the PatientID value afterward as part of the SELECT:
SELECT CONCAT('P', LPAD(ID, 6, 0)) AS PatientID FROM tblAcceptTest;
It looks like you want six digits after the "P", so try this for your expression:
CONCAT('P', LPAD(ID, 6, '0'))
Mysql has little support for computed columns.
Patient ID from your specification could be a char(7)
CREATE TABLE tblAcceptTest(
ID int AUTO_INCREMENT NOT NULL primary key,
PatientID char(7)
);
Then create some triggers. Note that the following insert trigger will cause issues with high concurrency servers.
DELIMITER |
CREATE TRIGGER tblAcceptTest_insert BEFORE INSERT ON tblAcceptTest
FOR EACH ROW BEGIN
DECLARE next_id INT;
SET next_id = (SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='tblAcceptTest');
SET NEW.PatientID = CONCAT('P' , RIGHT(CONCAT('000000',next_id),6)) ;
END;
|
CREATE TRIGGER tblAcceptTest_update BEFORE UPDATE ON tblAcceptTest
FOR EACH ROW BEGIN
SET NEW.PatientID = CONCAT('P' , RIGHT(CONCAT('000000',NEW.ID),6)) ;
END;
|
DELIMITER ;
You use relationships and views to achieve the same result.
CREATE TABLE `patient` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`patient` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `accepted_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`patient_id` int(11) NOT NULL,
`accepted` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `patient_id` (`patient_id`),
CONSTRAINT `accepted_test_ibfk_1` FOREIGN KEY (`patient_id`) REFERENCES `patient` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create or replace view accepted_test_veiw as
select CONCAT('P' , RIGHT(CONCAT('000000',patient_id),6)) patient_key
, accepted
, id accepted_test_id
, patient_id
from accepted_test ;
select * from `accepted_test_veiw`

How to create relationships in MySQL

In class, we are all 'studying' databases, and everyone is using Access. Bored with this, I am trying to do what the rest of the class is doing, but with raw SQL commands with MySQL instead of using Access.
I have managed to create databases and tables, but now how do I make a relationship between two tables?
If I have my two tables like this:
CREATE TABLE accounts(
account_id INT NOT NULL AUTO_INCREMENT,
customer_id INT( 4 ) NOT NULL ,
account_type ENUM( 'savings', 'credit' ) NOT NULL,
balance FLOAT( 9 ) NOT NULL,
PRIMARY KEY ( account_id )
)
and
CREATE TABLE customers(
customer_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
state VARCHAR(20) NOT NULL,
PRIMARY KEY ( customer_id )
)
How do I create a 'relationship' between the two tables? I want each account to be 'assigned' one customer_id (to indicate who owns it).
If the tables are innodb you can create it like this:
CREATE TABLE accounts(
account_id INT NOT NULL AUTO_INCREMENT,
customer_id INT( 4 ) NOT NULL ,
account_type ENUM( 'savings', 'credit' ) NOT NULL,
balance FLOAT( 9 ) NOT NULL,
PRIMARY KEY ( account_id ),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
) ENGINE=INNODB;
You have to specify that the tables are innodb because myisam engine doesn't support foreign key. Look here for more info.
as ehogue said, put this in your CREATE TABLE
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
alternatively, if you already have the table created, use an ALTER TABLE command:
ALTER TABLE `accounts`
ADD CONSTRAINT `FK_myKey` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`) ON DELETE CASCADE ON UPDATE CASCADE;
One good way to start learning these commands is using the MySQL GUI Tools, which give you a more "visual" interface for working with your database. The real benefit to that (over Access's method), is that after designing your table via the GUI, it shows you the SQL it's going to run, and hence you can learn from that.
CREATE TABLE accounts(
account_id INT NOT NULL AUTO_INCREMENT,
customer_id INT( 4 ) NOT NULL ,
account_type ENUM( 'savings', 'credit' ) NOT NULL,
balance FLOAT( 9 ) NOT NULL,
PRIMARY KEY ( account_id )
)
and
CREATE TABLE customers(
customer_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
state VARCHAR(20) NOT NULL,
)
How do I create a 'relationship' between the two tables? I want each account to be 'assigned' one customer_id (to indicate who owns it).
You have to ask yourself is this a 1 to 1 relationship or a 1 out of many relationship. That is, does every account have a customer and every customer have an account. Or will there be customers without accounts. Your question implies the latter.
If you want to have a strict 1 to 1 relationship, just merge the two tables.
CREATE TABLE customers(
customer_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
state VARCHAR(20) NOT NULL,
account_type ENUM( 'savings', 'credit' ) NOT NULL,
balance FLOAT( 9 ) NOT NULL,
)
In the other case, the correct way to create a relationship between two tables is to create a relationship table.
CREATE TABLE customersaccounts(
customer_id INT NOT NULL,
account_id INT NOT NULL,
PRIMARY KEY (customer_id, account_id),
FOREIGN KEY customer_id references customers (customer_id) on delete cascade,
FOREIGN KEY account_id references accounts (account_id) on delete cascade
}
Then if you have a customer_id and want the account info, you join on customersaccounts and accounts:
SELECT a.*
FROM customersaccounts ca
INNER JOIN accounts a ca.account_id=a.account_id
AND ca.customer_id=mycustomerid;
Because of indexing this will be blindingly quick.
You could also create a VIEW which gives you the effect of the combined customersaccounts table while keeping them separate
CREATE VIEW customeraccounts AS
SELECT a.*, c.* FROM customersaccounts ca
INNER JOIN accounts a ON ca.account_id=a.account_id
INNER JOIN customers c ON ca.customer_id=c.customer_id;
Adding onto the comment by ehogue, you should make the size of the keys on both tables match. Rather than
customer_id INT( 4 ) NOT NULL ,
make it
customer_id INT( 10 ) NOT NULL ,
and make sure your int column in the customers table is int(10) also.
Certain MySQL engines support foreign keys. For example, InnoDB can establish constraints based on foreign keys. If you try to delete an entry in one table that has dependents in another, the delete will fail.
If you are using a table type in MySQL, such as MyISAM, that doesn't support foreign keys, you don't link the tables anywhere except your diagrams and queries.
For example, in a query you link two tables in a select statement with a join:
SELECT a, b from table1 LEFT JOIN table2 USING (common_field);
Here are a couple of resources that will help get started: http://www.anchor.com.au/hosting/support/CreatingAQuickMySQLRelationalDatabase and http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561
Also as others said, use a GUI - try downloading and installing Xampp (or Wamp) which run server-software (Apache and mySQL) on your computer.
Then when you navigate to //localhost in a browser, select PHPMyAdmin to start working with a mySQL database visually. As mentioned above, used innoDB to allow you to make relationships as you requested. Makes it heaps easier to see what you're doing with the database tables. Just remember to STOP Apache and mySQL services when finished - these can open up ports which can expose you to hacking/malicious threats.
One of the rules you have to know is that the table column you want to reference to has to be with the same data type as
The referencing table . 2 if you decide to use mysql you have to use InnoDB Engine because according to your question that’s the engine which supports what you want to achieve in mysql .
Bellow is the code try it though the first people to answer this question
they 100% provided great answers and please consider them all .
CREATE TABLE accounts(
account_id INT NOT NULL AUTO_INCREMENT,
customer_id INT( 4 ) NOT NULL ,
account_type ENUM( 'savings', 'credit' ) NOT NULL,
balance FLOAT( 9 ) NOT NULL,
PRIMARY KEY (account_id)
)ENGINE=InnoDB;
CREATE TABLE customers(
customer_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
state VARCHAR(20) NOT NULL,
PRIMARY KEY ( account_id ),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
)ENGINE=InnoDB;
create table departement(
dep_id int primary key auto_increment,
dep_name varchar(100) not null,
dep_descriptin text,
dep_photo varchar(100) not null,
dep_video varchar(300) not null
);
create table newsfeeds(
news_id int primary key auto_increment,
news_title varchar(200) not null,
news_description text,
news_photo varchar(300) ,
news_date varchar(30) not null,
news_video varchar(300),
news_comment varchar(200),
news_departement int foreign key(dep_id) references departement(dep_id)
);