Why do my CREATE TABLES work alone but not together? [duplicate] - sql

This question already has answers here:
How do I execute multiple SQL Statements in Access' Query Editor?
(6 answers)
Closed 4 years ago.
I am trying to run a script with multiple CREATE TABLE statements in MS Access.
When I try to run the script all together I get a CREATE TABLE syntax error on the 2nd CREATE TABLE, but when I run each CREATE TABLE on its own through a SQL validator they pass successfully.
What am I doing wrong by lumping all the CREATE TABLEs into one script?
CREATE TABLE MODEL (
ModelNum INT NOT NULL,
Capacity INT NOT NULL,
MaxRange INT NOT NULL,
CONSTRAINT ModelPK PRIMARY KEY(ModelNum)
);
CREATE TABLE AIRPLANE (
RegNum INT NOT NULL,
ModelNum INT NOT NULL,
CONSTRAINT AirplanePK PRIMARY KEY(RegNum),
CONSTRAINT AirplaneFK FOREIGN KEY(ModelNum) REFERENCES MODEL(ModelNum)
);
CREATE TABLE EMPLOYEE (
SSN INT NOT NULL,
EmpName VARCHAR(100) NOT NULL,
Phone INT NULL,
UnionMemberNum INT NULL,
CONSTRAINT EmployeePK PRIMARY KEY(SSN)
);
CREATE TABLE TECHNICIAN (
SSN INT NOT NULL,
Salary INT NOT NULL,
CONSTRAINT TechnicianPK PRIMARY KEY(SSN),
CONSTRAINT TechnicianFK FOREIGN KEY(SSN) REFERENCES EMPLOYEE(SSN)
);
CREATE TABLE TRAFFICCONTROLLER (
SSN INT NOT NULL,
DateOfTraining DATE NULL,
CONSTRAINT TrafficControllerPK1 PRIMARY KEY(SSN),
CONSTRAINT TrafficControllerFK1 FOREIGN KEY(SSN) REFERENCES EMPLOYEE(SSN)
);
CREATE TABLE EXPERT(
SSN INT NOT NULL,
ModelNum INT NOT NULL,
CONSTRAINT ExpertPK1 PRIMARY KEY(SSN),
CONSTRAINT ExpertFK1 FOREIGN KEY(ModelNum) REFERENCES AIRPLANE(ModelNum)
);
CREATE TABLE TEST(
Test_Num INT NOT NULL,
TestName VARCHAR(100) NOT NULL,
MaxScore INT NOT NULL,
CONSTRAINT TestPK PRIMARY KEY(Test_Num)
);
CREATE TABLE TESTEVENTS(
RegNum INT NOT NULL,
Test_Num INT NOT NULL,
SSN INT NOT NULL,
TestDate DATE NOT NULL,
Score INT NULL,
CONSTRAINT TestEventsPK1 PRIMARY KEY(RegNum),
CONSTRAINT TestEventsFK1 FOREIGN KEY(RegNum) REFERENCES AIRPLANE(RegNum),
CONSTRAINT TestEventsPK2 PRIMARY KEY(Test_Num),
CONSTRAINT TestEventsFK2 FOREIGN KEY(Test_Num) REFERENCES TEST(Test_Num),
CONSTRAINT TestEventsPK3 PRIMARY KEY(SSN),
CONSTRAINT TestEventsFK3 FOREIGN KEY(SSN) REFERENCES TECHNICIAN(SSN),
CONSTRAINT TestEventsPK4 PRIMARY KEY(TestDate)
);

MS Access supports only one statement at a time.

Related

"Syntax error in constraint clause" MS Access creating a table

I keep getting the syntax error in access when trying to add a foreign key to a table. The foreign key is LendorID and it is referenced in another table. What am I doing wrong?
Here is my code for creating the table:
CREATE TABLE BOOK_INVENTORY
(
CRN int NOT NULL PRIMARY KEY,
AuthorLastName varchar(20) NOT NULL,
AuthorFirstName varchar(20) NOT NULL,
BookTitle varchar (100) NOT NULL,
LendorID varchar(5) FOREIGN KEY REFERENCES LENDOR_INFO(LendorID),
);
Here is my code for the table that contains LendorID:
CREATE TABLE LENDOR_INFO
(
LendorID varchar(5) NOT NULL,
LendorLastName varchar(20) NOT NULL,
LendorFirstName varchar(20) NOT NULL,
LendorEmail varchar(100) NOT NULL,
PRIMARY KEY (LendorID)
);

How to reference to primary key?

create database [PostaShpejte]
use PostaShpejte
create table Posta
(
ID_Posta int not null Primary Key,
Emri varchar(50)not null,
Qyteti varchar(15) not null,
)
create table Dergesa
(
ID_Dergesa int IDENTITY(1,1) not null Primary Key,
Emri_Dergeses varchar(30) not null,
Pershkrimi varchar(100),
Qmimi int not null,
Statusi varchar(30) not null,
CONSTRAINT CHK_Statusi CHECK (Statusi='E regjistruar' or Statusi='E nisur' or Statusi='Ne depo' or Statusi='E refuzuar' or Statusi='E derguar'),
)
create table Menaxhon
(
ID_Dergesa int not null references Dergesa (ID_Dergesa),
ID_Posta int not null references Posta(ID_Posta),
Primary Key(ID_Dergesa,ID_Posta),
)
--drop table TelBleresi
create table TelBleresi
(
ID_Tel_Bleresi int not null,
--ID_Bleresi int not null,
NumriTel int not null Unique,
Primary Key(ID_Tel_Bleresi),
)
--drop table Bleresi
create table Bleresi
(
ID_Bleresi int not null,
ID_Tel_Bleresi int not null,
Emri varchar(20) not null,
Mbiemri varchar(20) not null,
Shteti varchar(20) not null,
Qyteti varchar(20) not null,
Rruga varchar(50) not null,
ZIPKodi int not null,
FOREIGN KEY(ID_Tel_Bleresi) references TelBleresi(ID_Tel_Bleresi),
Primary Key (ID_Bleresi , ID_Tel_Bleresi),
)
create table Dergohet
(
ID_Dergesa int not null,
ID_Bleresi int not null,
Data_e_regj date not null,
Data_e_mbrritjes date not null,
----------------PROBLEM HERE---------------------------
FOREIGN KEY (ID_Dergesa) references Dergesa(ID_Dergesa),
FOREIGN KEY (ID_Bleresi) references **Bleresi**(ID_Bleresi),
*Error: There are no primary or candidate key to table Bleresi ....*
---------------------------------------------------------
PRIMARY KEY (ID_Dergesa,ID_Bleresi),
)
Bleresi has a compound primary key (ID_Bleresi, ID_Tel_Bleresi), so you need to reference all columns. That means adding ID_Tel_Bleresi to Dergohet.
create table Dergohet(
ID_Dergesa int not null,
ID_Bleresi int not null,
ID_Tel_Bleresi int not null, -- add this column
Data_e_regj date not null,
Data_e_mbrritjes date not null,
FOREIGN KEY (ID_Dergesa) references Dergesa(ID_Dergesa),
-- Reference the full compound key
FOREIGN KEY (ID_Bleresi, ID_Tel_Bleresi) references Bleresi(ID_Bleresi, ID_Tel_Bleresi),
PRIMARY KEY (ID_Dergesa,ID_Bleresi),
)
While they have some uses, compound primary keys are annoying as they create a proliferation of foreign key columns and complicate indexing. Some of yours seem unnecessary: Bleresi already has a ID_Bleresi, is that not unique?
In general, I'd recommend using simple big integer (2 billion creeps up on you surprisingly fast) auto incrementing primary keys. If you need to guarantee other uniquenesses, make a unique index.
The error say that ID_Bleresi is not the primary key on Bleresi table and for that can't be a foreign key. The primary key is:
Primary Key (ID_Bleresi , ID_Tel_Bleresi)
If ID_Bleresi is not a unique column, I recommend that in the table, you create a new unique column that is the primary key. In case it is, it would be best to set ID_Bleresi as the unique primary key

Add constraint to compare two attributes of different tables?

Here are my tables. I need to check that the 'program' attribute referenced in StudentsBranch with the table Students and the table Branches is the same. How can I do it?
CREATE TABLE Programmes (
name VARCHAR(200) UNIQUE NOT NULL,
CONSTRAINT pk_Programmes PRIMARY KEY (name)
);
CREATE TABLE Students (
id NUMERIC(10,0) UNIQUE NOT NULL,
program VARCHAR(200) NOT NULL,
CONSTRAINT pk_Students PRIMARY KEY (idnr),
FOREIGN KEY (program) REFERENCES Programmes(name)
);
CREATE TABLE Branches (
name VARCHAR(200) UNIQUE NOT NULL,
program VARCHAR(200) NOT NULL,
CONSTRAINT pk_Branches PRIMARY KEY (name, program),
FOREIGN KEY (program) REFERENCES Programmes(name)
);
CREATE TABLE StudentsBranch (
student NUMERIC(10,0) NOT NULL,
program VARCHAR(200) NOT NULL,
branch VARCHAR(200) NOT NULL,
CONSTRAINT pk_StudentsBranch PRIMARY KEY (student),
/* Below how the foreign keys I think should be */
FOREIGN KEY (student, program) REFERENCES Students(idnr, program),
FOREIGN KEY (branch, program) REFERENCES Branches(name, program)
/* I need to add a constraint to verify that the 'program' in Students
* and the 'program' in Branches are equivalent. How?
*/
);
You can't write that constraint with that existing database model you have now.
The only way I see you could do it, is by changing the primary key of Students to (id, program):
CREATE TABLE Students (
id NUMERIC(10,0) UNIQUE NOT NULL,
program VARCHAR(200) NOT NULL,
CONSTRAINT pk_Students PRIMARY KEY (id, program),
FOREIGN KEY (program) REFERENCES Programmes(name)
);
Then the table StudentsBranch could naturally enforce both FKs with using the single column program, as in:
CREATE TABLE StudentsBranch (
student NUMERIC(10,0) NOT NULL,
program VARCHAR(200) NOT NULL,
branch VARCHAR(200) NOT NULL,
CONSTRAINT pk_StudentsBranch PRIMARY KEY (student),
FOREIGN KEY (student, program) REFERENCES Students (id, program),
FOREIGN KEY (branch, program) REFERENCES Branches (name, program)
);
It is always a good idea to have an numeric column of primary key and have primary key for every table.
Once you have primary key for every table you can referece primary key of a specific table and refere it as foerign key.
CREATE TABLE Programmes (
ID INT,
name VARCHAR(200) UNIQUE NOT NULL,
CONSTRAINT pk_Programmes PRIMARY KEY (ID)
);
CREATE TABLE Students(
id INT,
ProgrammID INT NOT NULL,
CONSTRAINT pk_Students PRIMARY KEY (ID),
FOREIGN KEY (ProgrammID) REFERENCES Programmes(ID)
);
CREATE TABLE Branches (
BranchID INT,
ProgrammID INT NOT NULL,
name VARCHAR(200) UNIQUE NOT NULL,
--program VARCHAR(200) NOT NULL,
CONSTRAINT pk_Branches PRIMARY KEY (BranchID, ProgrammID),
FOREIGN KEY (ProgrammID) REFERENCES Programmes(ID)
);
CREATE TABLE StudentsBranch (
StudentsBranchID INT,
studentID INT NOT NULL,
ProgrammID INT NOT NULL,
BranchID INT NOT NULL,
CONSTRAINT pk_StudentsBranch PRIMARY KEY (StudentsBranchID),
FOREIGN KEY (ProgrammID) REFERENCES Programmes(id),
FOREIGN KEY (studentID) REFERENCES Students(id),
FOREIGN KEY (BranchID, ProgrammID) REFERENCES Branches(BranchID, ProgrammID)
);

SQL ALTER TABLE foreign key

I have a problem with adding foreign keys with alter table command. I don't know how to make it so it works.
I need to add ISIK_ID and STAADION_ID to ISIK_STAADIONIL table as foreign key.
Here is my code:
CREATE TABLE ISIK(
ISIK_ID INT NOT NULL,
EESNIMI VARCHAR(25) NOT NULL,
PEREKONNANIMI VARCHAR(25) NOT NULL,
ISIKUKOOD VARCHAR(20),
KODAKONDSUS VARCHAR(30),
SUGU CHAR(1) NOT NULL,
HARIDUSTASE CHAR(1) NOT NULL,
TELEFONI_NR VARCHAR(20),
SYNNIPAEV DATE,
CONSTRAINT ISIK_ID_PK PRIMARY KEY (ISIK_ID)
);
CREATE TABLE ISIK_STAADIONIL(
ISIK_STAADIONIL_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_STAADIONIL_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP
);
CREATE TABLE STAADION(
STAADION_ID INT NOT NULL,
NIMETUS VARCHAR(20),
KIRJELDUS VARCHAR(100),
ASUKOHT VARCHAR(50),
SUURUS VARCHAR(20),
MAHUTAVUS INT,
EHITATUD VARCHAR(20),
EHITAJA VARCHAR(20),
CONSTRAINT STAADION_ID_PK PRIMARY KEY (STAADION_ID)
);
ALTER TABLE ISIK_STAADIONIL
ADD CONSTRAINT ISIK_ID_FK
FOREIGN KEY(ISIK_ID)
REFERENCES ISIK(ID);
You need to add the columns first (to the table) before you can create FK constraints using them.
CREATE TABLE ISIK_STAADIONIL(
ISIK_STAADIONIL_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_STAADIONIL_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP,
ISIK_ID INT,
STAADION_ID INT
);
And then your ALTER statement should work fine.
UPDATE
Changing the table-design slightly would make more sense, as per following. The logic is that ISIK_ID and STAADION_ID would together be sufficient to determine uniqueness in the ISIK_STAADIONIL table, so a separate ID is redundant:
CREATE TABLE ISIK_STAADIONIL(
ISIK_ID INT NOT NULL,
STAADION_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_ID, STAADION_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP
);

SQL SMS - Error - There is already an object named "genre" in the database

I'm trying to create a hypothetical videostore database and this error message comes up everytime I execute this query:
DROP DATABASE videostore
CREATE DATABASE videostore
CREATE TABLE genre
(
genre_id INT NOT NULL PRIMARY KEY IDENTITY,
genre_name VARCHAR(15) NOT NULL
);
CREATE TABLE classification
(
rating VARCHAR(2) NOT NULL CONSTRAINT classification_pk PRIMARY KEY,
description VARCHAR(100) NOT NULL,
minimum_age INT NULL,
);
CREATE TABLE format
(
format_id INT NOT NULL CONSTRAINT format_pk PRIMARY KEY IDENTITY,
format_name VARCHAR(8) NOT NULL,
purchase_cost FLOAT NOT NULL
);
CREATE TABLE rental_cost
(
rental_cost_id INT NOT NULL CONSTRAINT rental_cost_pk PRIMARY KEY IDENTITY,
rental_name VARCHAR(15) NOT NULL,
rental_cost FLOAT NOT NULL,
rental_days TINYINT NOT NULL
);
CREATE TABLE customer
(
customer_id INT NOT NULL CONSTRAINT customer_pk PRIMARY KEY IDENTITY,
first_name VARCHAR(20) NOT NULL,
surname VARCHAR(20) NOT NULL,
dob DATETIME NOT NULL,
home_address VARCHAR(50) NOT NULL,
contact_number VARCHAR(10) NOT NULL,
referrer_id INT NULL FOREIGN KEY REFERENCES customer(customer_id),
);
CREATE TABLE movie
(
movie_id INT NOT NULL CONSTRAINT movie_pk PRIMARY KEY IDENTITY,
movie_name VARCHAR(40) NOT NULL,
year SMALLINT NOT NULL,
duration SMALLINT NULL,
descrip VARCHAR(120) NULL,
classification VARCHAR(2) NOT NULL FOREIGN KEY REFERENCES classification(rating),
rental_cost_id INT NOT NULL FOREIGN KEY REFERENCES rental_cost(rental_cost_id),
);
CREATE TABLE copy
(
copy_id INT NOT NULL CONSTRAINT copy_pk PRIMARY KEY IDENTITY,
movie_id INT NOT NULL FOREIGN KEY REFERENCES movie(movie_id),
format_id INT NOT NULL FOREIGN KEY REFERENCES format(format_id),
);
CREATE TABLE rental
(
rental_id INT NOT NULL CONSTRAINT rental_pk PRIMARY KEY IDENTITY,
copy_id INT NOT NULL FOREIGN KEY REFERENCES copy(copy_id),
customer_id INT NOT NULL FOREIGN KEY REFERENCES customer(customer_id),
rental_date DATETIME NOT NULL,
return_date DATETIME NULL
);
CREATE TABLE genre_movie
(
genre_id INT NOT NULL FOREIGN KEY REFERENCES genre(genre_id),
movie_id INT NOT NULL FOREIGN KEY REFERENCES movie(movie_id),
CONSTRAINT genre_movie_pk PRIMARY KEY (genre_id, movie_id)
);
(Sorry it's not in the correct format for reading a script with ease, I just really couldn't work out how to do that.)
Basically when I execute the SQL script it tells me that the object 'genre' already exists, but I can't see it anywhere else in my code.
And the table shouldn't already exist because I drop the database each time the script is executed right?
Sidenote - if there's a better way to make it so the database is dropped only if it exists please help me with that also.
Appreciate it.
I think you need to add USE videostore after the CREATE DATABASE videostore. Otherwise you are creating tables in whatever database you are currently in.
I think you are executing this in the context of the master database. If you check it you will probably see all your tables are actually in master.
Change the first part to this:
DROP DATABASE videostore
CREATE DATABASE videostore
USE videostore
CREATE TABLE genre
It's generally best to execute sql statements when required - try:
DROP DATABASE videostore
GO
CREATE DATABASE videostore
GO
USE videostore
CREATE TABLE genre
(
genre_id INT NOT NULL PRIMARY KEY IDENTITY,
genre_name VARCHAR(15) NOT NULL
);
GO
etc.