i created my tables and I'm stuck at the last one,
here is the tables that been created correctly
CREATE TABLE Staff (
Staff_ID INT NOT NULL PRIMARY KEY,
First_Name VARCHAR(50),
Last_Name VARCHAR(50),
Username VARCHAR(10),
Password VARCHAR(10),
Address VARCHAR(30)
)
CREATE TABLE Category (
Category_ID INT NOT NULL PRIMARY KEY,
Name VARCHAR(30)
)
CREATE TABLE Author (
Author_ID INT NOT NULL PRIMARY KEY,
First_Name VARCHAR(50),
Last_Name VARCHAR(50),
Birth_Place VARCHAR(30),
Birth_Date DATE
)
CREATE TABLE Publisher (
Publisher_ID INT NOT NULL PRIMARY KEY,
Name VARCHAR(50)
)
and this is the one I'm getting an error :
CREATE TABLE Book (
Book_ID INT NOT NULL PRIMARY KEY,
Title VARCHAR(50),
Edition INT(30),
Year_Published INT(4),
FOREIGN KEY (Publisher_ID) REFERENCES Publisher(Publisher_ID),
FOREIGN KEY (Author_ID) REFERENCES Author(Author_ID),
FOREIGN KEY (Category_ID) REFERENCES Category(Category_ID)
)
the error says:
"ORA-00907: missing right parenthesis"
INT can not have a scale associated with it so YEAR_PUBLISHED and EDITION are incorrect definitions.
I believe that, generally, you would be better off sticking to NUMBER for numeric datatypes, eg NUMBER(4), NUMBER(30).
In the database the INT datatype is simply a sub-type of NUMBER so you aren't gaining anything by using it:
type NUMBER is NUMBER_BASE;
subtype INTEGER is NUMBER(38,0);
subtype INT is INTEGER;
If you want to see the definitions for the various 'other' numeric datatypes take a look at the SYS.STANDARD package.
The INT data type does not have a precision.
You also need to define the Publisher_ID, Author_ID and Category_ID columns.
It is good practice to name your constraints.
A PRIMARY KEY column is both NOT NULL and UNIQUE so you do not need to include a second NOT NULL constraint.
Like this:
CREATE TABLE Book (
Book_ID INT CONSTRAINT Book__Book_id__PK PRIMARY KEY,
Title VARCHAR(50),
Edition INT,
Year_Published INT,
Publisher_ID INT CONSTRAINT Book__Publisher_ID__FK REFERENCES Publisher(Publisher_ID),
Author_ID INT CONSTRAINT Book__Author_id__FK REFERENCES Author(Author_ID),
Category_ID INT CONSTRAINT Book__category_ID__FK REFERENCES Category(Category_ID)
);
Related
Working on small ASP.Net MVC project, and got requirement from business analyst who gave me PDF file with below SQL staff to create in database.
Looking on how to achieve result from the below SQL question.
By the way should all be created using T-SQL
Student - Table
studentNo Identity column INT NOT NULL PK
lastName VARCHAR(30) NOT NULL
firstName VARCHAR(30) NOT NULL
gender CHAR NOT NULL
phoneNumber VARCHAR(20) NOT NULL
salaryAmount INT,
departmentID INT FK
Department - Table
departmentID Identity Column INT NOT NULL PK
name VARCHAR(40) NOT NULL
priceCentreID INT FK
PriceCentre - Table
priceCentreID
name VARCHAR(50)
accountID varchar(50)
As I understand you want help to create the sql code to generate the tables you provided?
Below is the code to create the tables and the relations that you are looking for.
code:
CREATE TABLE PriceCentre(
priceCenterID INT PRIMARY KEY,
name VARCHAR(50),
accountID VARCHAR(50)
);
GO
CREATE TABLE Department(
departmentID INT NOT NULL PRIMARY KEY,
name VARCHAR(40) NOT NULL,
priceCenterID int FOREIGN KEY REFERENCES PriceCentre (priceCenterID)
)
GO
CREATE TABLE Student(
studentNo INT NOT NULL PRIMARY KEY,
lastName VARCHAR(30) NOT NULL,
firstName VARCHAR(30) NOT NULL,
gender CHAR NOT NULL,
phoneNumber VARCHAR(20) NOT NULL,
salaryAmount INT,
departmentID INT FOREIGN KEY REFERENCES Department(departmentID)
);
I am making a database which links together two tables via the primary key of the first table. The one with the primary key which links the two is created first but how do i make the second record get the ID of the record I just created?
create table Person
(
Person_ID int IDENTITY(100000,1) primary key,
First_Name varchar(20) not null,
Last_Name varchar(20) not null,
)
create table Employee
(
Employee_ID int identity(100000,1) primary key,
Person_ID int references Person(Person_ID),
Employee_Type varchar(10)
)
insert into Person(First_Name, Last_Name) values ('Michael', 'Chu');
insert into Employee(Person_ID, Employee_Type,) values (????????, 'Admin');
I've had a look at the 'last()' function but not really sure how to utilise that. Other then that, I have no idea. Can someone help me out or guide me in the right direction.
try this:
create table Person
(
Person_ID int IDENTITY(100000,1) primary key,
First_Name varchar(20) not null,
Last_Name varchar(20) not null,
)
create table Employee
(
Employee_ID int identity(100000,1) primary key,
Person_ID int references Person(Person_ID),
Employee_Type varchar(10)
)
DECLARE #myID AS INT
insert into Person(First_Name, Last_Name) values ('Michael', 'Chu');
SET #myID = ##IDENTITY
insert into Employee(Person_ID, Employee_Type,) values (#myID , 'Admin');
I have a table already created, here's the code:
CREATE TABLE "Préstamo_Biomédica" (
ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Entregado CHAR(100),
Fech_Pres DATE,
Fech_Devu DATE,
Nota TEXT
)
And I'm trying to create a table with a FOREIGN KEY with this code:
CREATE TABLE Inventario_Biomédica (
Id_Pieza INT PRIMARY KEY,
Nom_Pieza CHAR(100),
Cantidad INT,
Des_Pieza TEXT,
Gastable BIT,
Fech_Ent DATE,
P_Módulo BIT,
FOREIGN KEY (ID) REFERENCES Préstamo_Biomédica(ID_Préstamo),
)
I hope you guys can help me...
You should use only ASCII characters in identifiers. If not, you must quote them.
To get an autoincrementing ID, you must use INTEGER, not INT.
The FOREIGN KEY clause refers to a column that already must exist in the table's column list.
And the parent's table/column names come after the REFERENCE:
CREATE TABLE "Inventario_Biomédica" (
Id_Pieza INTEGER PRIMARY KEY,
Nom_Pieza CHAR(100),
Cantidad INT,
Des_Pieza TEXT,
Gastable BIT,
Fech_Ent DATE,
"P_Módulo" BIT,
"ID_Préstamo" INT,
FOREIGN KEY ("ID_Préstamo") REFERENCES "Préstamo_Biomédica"(ID),
);
CREATE TABLE "Préstamo_Biomédica" (
ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Entregado CHAR(100),
Fech_Pres DATE,
Fech_Devu DATE,
Nota TEXT
)
CREATE TABLE Inventario_Biomédica (
Id_Pieza INT PRIMARY KEY,
Nom_Pieza CHAR(100),
Cantidad INT,
Des_Pieza TEXT,
Gastable BIT,
Fech_Ent DATE,
P_Módulo BIT,
FOREIGN KEY (ID_Préstamo) REFERENCES Préstamo_Biomédica(ID),
)
Give foreign Key Column Name right after FOREIGN KEY and parent tables Primary key column after REFERENCES
I have 4 tables, 3 of which have their own primary keys. In the 4th table, the primary keys of all the other 3 tables are referenced as foreign keys and these are made into a primary key (combination of all 3 foreign keys). However, upon entering data into the columns, I get constraint violation errors.
The code snippets are as below:
--Table # 1
create table PartSupplier(
partSupplierID int primary key,
fName varchar(20) NOT NULL,
lName varchar(20),
houseNO varchar(20),
streetName varchar(20),
city varchar(20),
dob date,
contactNo varchar(50)
)
--Table # 2
create table PartSpecialist(
partSpecialistID int primary key,
fName varchar(20) NOT NULL,
lName varchar(20),
houseNO varchar(20),
streetName varchar(20),
city varchar(20),
dob date,
contactNo varchar(15)
)
--Table # 3
create table CarPart(
partNumber int primary key,
description varchar (250),
vehicleID int NOT NULL,
imagePath varchar(50)
)
--Table # 4 (with multiple foreign keys made into one primary key)
create table SpecialistSuppliedPart(
partSpecialistID int NOT NULL,
partSupplierID int NOT NULL,
partNumber int NOT NULL,
condition varchar(10),
dateAcquired date,
costPrice int NOT NULL,
constraint specialist_supplier_part_PK primary key (partSpecialistID, partSupplierID, partNumber),
foreign key (partSpecialistID) references PartSpecialist(partSpecialistID),
foreign key (partSupplierID) references PartSupplier(partSupplierID),
foreign key (partNumber) references CarPart(partNumber)
)
I tried inserting the following values in the table:
3 3 5 USED 5000 2011-03-01
3 13 18 NEW 6000 2011-06-26
upon which I got the following error:
http://i.stack.imgur.com/HhFk2.jpg
where FK_SpecialistSuppliedPart_PartSupplier is the Foreign Key existing between Table # 1 and Table # 4.
I have all of these entries existing in the respective tables. After an exhaustive search on the internet (including stackoverflow.com), I could not find a solution.
What am I doing wrong here?
P.S. I tried adding the Foreign and Primary keys using MSSQLs Design Tool but it still results in the same error.
I have a simple schema for a sports registration system till the concept of a season is introduced:
A player registers as part of a Team
A Team registers as part of a Club
A team plays in a Division
An Organization creates Divisions
This happens every season. So I need a way to keep track of all of this every season. What would be the best approach here? Should I introduce a "Season" entity and make many-to-many relationships between all the entities above as applicable? Or is there a way to just "Archive" everything every season but also be able to pull all the season based data up on the fly (for reporting and what not)?
This design allows teams to move to a different division from season to season, while not having to be re-created every year (though it doesn't allow for things like the Winnipeg Jets moving to Phoenix and keeping their old name with their historical season stats).
CREATE TABLE dbo.Organizations
(
OrgID INT PRIMARY KEY,
Name NVARCHAR(255) NOT NULL UNIQUE
-- , ... other columns
);
CREATE TABLE dbo.Divisions
(
DivisionID INT PRIMARY KEY,
Name NVARCHAR(255) NOT NULL,
OrgID INT NOT NULL FOREIGN KEY REFERENCES dbo.Organizations(OrgID)
-- , ... other columns
-- UNIQUE might be (Name) or (OrgID, Name)
);
CREATE TABLE dbo.Clubs
(
ClubID INT PRIMARY KEY,
Name NVARCHAR(255) NOT NULL UNIQUE,
-- , ... other columns
);
CREATE TABLE dbo.Teams
(
TeamID INT PRIMARY KEY,
Name NVARCHAR(255) NOT NULL,
ClubID INT NOT NULL FOREIGN KEY REFERENCES dbo.Clubs(ClubID)
-- , ... other columns
);
Now to keep track of seasons:
CREATE TABLE dbo.Seasons
(
SeasonID INT PRIMARY KEY,
StartDate DATE NOT NULL,
EndDate DATE,
Name NVARCHAR(255) NOT NULL -- e.g. '1997-98' for seasons that cross Jan 1
-- , ... other columns
);
CREATE TABLE dbo.SeasonTeams
(
SeasonID INT FOREIGN KEY REFERENCES dbo.Seasons(SeasonID),
DivisionID INT FOREIGN KEY REFERENCES dbo.Divisions(DivisionID),
TeamID INT FOREIGN KEY REFERENCES dbo.Teams(TeamID),
PRIMARY KEY (SeasonID, DivisionID, TeamID)
);
You probably want constraints or triggers to prevent a team from belonging to more than one division in any given year.
Now you'll also want to denote a roster for a given season / team.
CREATE TABLE dbo.Players
(
PlayerID INT PRIMARY KEY,
Name NVARCHAR(255) NOT NULL,
-- , ... other columns
);
CREATE TABLE dbo.TeamRoster
(
PlayerID INT NOT NULL FOREIGN KEY REFERENCES dbo.Players(PlayerID),
TeamID INT NOT NULL,
SeasonID INT NOT NULL,
DivisionID INT NOT NULL,
FOREIGN KEY (TeamID, SeasonID, DivisionID) REFERENCES
dbo.SeasonTeams (TeamID, SeasonID, DivisionID)
);
You'll also want tables for schedules, standings, player stats etc... but that should be a start.