SQL database management - sql

I'm currently having issues with my database. It shows this error:
Msg 1911, Level 16, State 1, Line 13
Column name 'suffix_desc' does not exist in the target table or view.
Msg 1750, Level 16, State 0, Line 13
Could not create constraint. See previous errors.
This is the code,
create table prefix
(
prefix_desc varchar(4) not null,
primary key (prefix_desc)
)
create table suffix
(
suffix_desc varchar(4) not null,
primary key (suffix_desc)
)
create table member
(
member_id varchar(80) not null,
First_name varchar(50) not null,
Last_name varchar(50) not null,
middle_name varchar(50) null,
Dob date not null,
Email varchar(80) not null,
UserID varchar(80) null,
Password varchar(16) not null,
phone_friendly_ind varchar(3) not null,
SMS_ind varchar(3) not null,
return_ind varchar(3) not null,
fuel_prepay_ind varchar(3) not null,
post_mail_ind varchar(3) not null,
email_ind varchar(3) not null,
privacy_ind varchar(3) not null,
primary key (member_id, suffix_desc, prefix_desc),
foreign key (suffix_desc) references suffix (suffix_desc),
foreign key (prefix_desc) references prefix (prefix_desc)
)

I think you forgot to add this :
create table member(
...
suffix_desc varchar(4) not null,
prefix_desc varchar(4) not null,
...
)

Related

there is already an object named '' in the database

This is my code:
CREATE TABLE supplier -- creating table supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO
I get the the error:
Msg 2714, Level 16, State 6, Line 34
There is already an object named 'supplier' in the database.
Any help? Thanks!
Please try this code.
IF EXISTS(SELECT 1 FROM sys.tables WHERE name = 'supplier')
DROP TABLE dbo.supplier;
CREATE TABLE dbo.supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO
You need to check if the table exists first
IF OBJECT_ID('dbo.supplier', 'U') IS NOT NULL
DROP TABLE dbo.supplier;
CREATE TABLE dbo.supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO
If you are using 2016+ you can use
DROP TABLE IF EXISTS dbo.supplier;
CREATE TABLE dbo.supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO

i am trying to create tables but it show me 2 errors, how i fix it?

2 errors were found during analysis.
Unexpected beginning of statement. (near "admin_id" at position 349)
Unrecognized statement type. (near "INT" at position 358)
CREATE TABLE Users (
user_id INT NOT NULL AUTO_INCREMENT,
email VARCHAR(80) NOT NULL,
password CHAR(41) NOT NULL,
name VARCHAR(50) NOT NULL,
phone VARCHAR(20) NOT NULL,
gender VARCHAR(6) NOT NULL,
fbinfo VARCHAR(50) NOT NULL,
age INT NOT NULL,
PRIMARY KEY (user_id),
UNIQUE INDEX (email)
)
CREATE TABLE Admin (
admin_id INT NOT NULL AUTO_INCREMENT,
email VARCHAR(80) NOT NULL,
password CHAR(41) NOT NULL,
PRIMARY KEY (admin_id),
UNIQUE INDEX (email)
)
CREATE TABLE Task (
task_id INT NOT NULL AUTO_INCREMENT,
tex VARCHAR(140) NOT NULL,
datetime DATE,
status INT,
calendar BOOLEAN,
PRIMARY KEY (task_id),
lat DECIMAL(9,6),
long DECIMAL(9,6)
)
If you are doing this in one execution, you would need to include a ; at the end of each of your table creation statements.
CREATE TABLE Users (
user_id INT NOT NULL AUTO_INCREMENT,
email VARCHAR(80) NOT NULL,
password CHAR(41) NOT NULL,
name VARCHAR(50) NOT NULL,
phone VARCHAR(20) NOT NULL,
gender VARCHAR(6) NOT NULL,
fbinfo VARCHAR(50) NOT NULL,
age INT NOT NULL,
PRIMARY KEY (user_id),
UNIQUE INDEX (email)
);
CREATE TABLE Admin (
admin_id INT NOT NULL AUTO_INCREMENT,
email VARCHAR(80) NOT NULL,
password CHAR(41) NOT NULL,
PRIMARY KEY (admin_id),
UNIQUE INDEX (email)
);
CREATE TABLE Task (
task_id INT NOT NULL AUTO_INCREMENT,
tex VARCHAR(140) NOT NULL,
datetime DATE,
status INT,
calendar BOOLEAN,
PRIMARY KEY (task_id),
lat DECIMAL(9,6),
long DECIMAL(9,6)
);

sql query default in oracle

create table dvds(
dvdid number not null primary key,
dvdname varchar(60) not null,
numdisks number not null default 1,
yearrlsd date not null,
mtypeid varchar(4) not null,
stuid varchar(4) not null,
ratingid varchar(4) not null,
formid char(2) not null,
statid char(3) not null,
foreign key (mtypeid) references movietypes (mtypeid)
)
whrn i am running this command in oracle it is giving error:
missing right parenthesis
but after removing default it is working
The default value should come before the constraints (not null);
numdisks number default 1 not null,
SQLfiddle.

Microsoft SQL, foreign key not referencing table, can't see what's causing it

I've spent a while trying to firgure it but but I can't see anything that would be causing this error. I'm thinking maybe something to do with the "Unique" statement.
Msg 1767, Level 16, State 0, Line 40
Foreign key 'FK_Loan_ItemNo__0AD2A005' references invalid table
'Item'.
Here's the code if someone wants to use it to replicate problem:
CREATE DATABASE LibrarySystem10
GO
USE LibrarySystem10
GO
CREATE TABLE MemberType(
MemberTypeNo int NOT NULL,
Name varchar(50) NOT NULL,
Description varchar(250) NOT NULL,
MaxNumberLoans int NOT NULL,
MaxLoanDuration int NOT NULL
PRIMARY KEY (MemberTypeNo)
)
insert into MemberType values ('0','UnderGraduate','A student at a college or university who has not yet earned a bachelor''s or equivalent degree.','5','10')
insert into MemberType values ('1','PostGraduate','A student undertaking study after completing a first degree.','10','10')
insert into MemberType values ('2','Staff','Staff at the university','15','15')
CREATE TABLE Member(
MemberNo int NOT NULL,
MemberTypeNo int NOT NULL,
FirstName varchar(150) NOT NULL,
LastName varchar(150) NULL,
DateOfBirth varchar (200) NULL,
HouseNo int NOT NULL,
Street varchar(50) NOT NULL,
Suburb varchar(100) NOT NULL,
PostCode int NOT NULL,
EmailAddress varchar(250) NULL,
HomePhoneNo varchar(250) NULL,
MobileNo varchar(250) NULL,
MembershipStartDate varchar (200) NOT NULL,
MembershipEndDate varchar (200) NOT NULL,
MembershipStatus varchar(100) NOT NULL,
PinNo int NOT NULL
PRIMARY KEY (MemberNo)
FOREIGN KEY (MemberTypeNo)REFERENCES MemberType ON UPDATE CASCADE
)
insert into member values ('0','0','Shane','Lindsay','15-11-1992','90','fake st','FauxTon','2250','shane#hotmai.com','0243296356','0415657164','15-11-2010','15-11-2020','current','0105')
insert into member values ('1','0','Shaune','Lincoln','18-12-1992','92','faken st','FauxTone','2350','shaune27#hotmai.com','0243253357','041565757','14-12-2010','14-12-2020','deferred','0123')
insert into member values ('2','0','Sarah','richards','08-08-1990','45','Small st','Hornsby','2279','Sarah67#hotmai.com','02432567154','0416451845','01-01-2012','01-01-2022','current','0123')
CREATE TABLE Loan(
MemberNo int NOT NULL FOREIGN KEY(MemberNo) REFERENCES Member ON UPDATE CASCADE,
ItemNo int NOT NULL FOREIGN KEY(ItemNo) REFERENCES Item ON UPDATE CASCADE,
DateLoaned varchar (50) NOT NULL,
DueDate varchar (50) NOT NULL,
Status varchar(50) NOT NULL,
FinesImposed bit NOT NULL DEFAULT '0' CHECK (finesImposed IN ('0','1')) ,
Renewed bit NOT NULL DEFAULT '0' CHECK (Renewed IN ('0','1')),
UNIQUE(MemberNo,ItemNo,DateLoaned)
)
insert into Loan values ('0','0','10-10-2012','15-10-2012','loaned','0','0')
insert into Loan values ('1','0','12-10-2012','15-10-2012','loaned','0','1')
CREATE TABLE Item(
ItemNo int NOT NULL,
Title varchar(50) NOT NULL,
Subject varchar(100) NULL,
ISBN int NULL,
PhysicalDescription varchar(150) NULL,
Author varchar(75) NULL,
PRIMARY KEY (ItemNo)
)
insert into Item values ('0','Book1','IT','0501425252','Big,42pages','John Doe')
insert into Item values ('1','Book2','IT','0501425253','Big,42pages','John Doe')
CREATE TABLE ItemCopy(
ItemNo int NOT NULL,
CallNumber varchar(50) NOT NULL,
Condition varchar(50) NULL,
UNIQUE(ItemNo,CallNumber),
PRIMARY KEY (CallNumber)
)
insert into ItemCopy values ('0','0','good')
CREATE TABLE Hold(
HoldNo int NOT NULL,
MemberNo int NOT NULL FOREIGN KEY(MemberNo) REFERENCES Member ON UPDATE CASCADE,
ItemNo int NOT NULL FOREIGN KEY(ItemNo) REFERENCES Item ON UPDATE CASCADE,
DateTimeHeld datetime NOT NULL,
comments varchar(200) NULL,
Status varchar(50) NOT NULL
PRIMARY KEY (HoldNo)
)
CREATE TABLE Fine(
FineNo int NOT NULL,
MemberNo int NOT NULL FOREIGN KEY(MemberNo) REFERENCES Member ON UPDATE CASCADE,
Description varchar(50) NULL,
Amount int NOT NULL,
PRIMARY KEY (FineNo)
)
CREATE TABLE AudioRecording(
Length varchar(50) NULL,
BitRate varchar(50) NULL,
Size varchar(50) NULL
)
CREATE TABLE ItemCollection(
ItemNo int NULL,
CollectionName varchar(75) NULL
UNIQUE (ItemNo,CollectionName)
)
CREATE TABLE Collection(
CollectionName varchar(75)NOT NULL
PRIMARY KEY (CollectionName)
)
CREATE TABLE Book(
PublisherInfo varchar(150) NULL,
Edition int NULL,
Notes varchar(250) NULL,
Status varchar(50) NULL
)
CREATE TABLE Journal(
Series int NULL,
Notes varchar(250) NULL,
OtherTitles varchar(150) NULL,
PriorTitles varchar(250) NULL
)
SELECT m.FirstName, l.Status, l.DueDate
FROM Member m, Loan l
WHERE m.MemberNo = '0' AND l.MemberNo = m.MemberNo
Loan references Item, but you create Loan before you create Item. Create the Item table first.

SQL table creation code gives error

could someone have a look at this for me, I can't seem to find why it is not working.
CREATE TABLE Person(
Person_ID int auto_increment NOT NULL,
Person_Type_ID int NOT NULL,
Create_Date datetime NOT NULL ,
Modify_Date datetime NOT NULL ,
First_Name varchar(50) NOT NULL,
Surname varchar(50) NOT NULL,
DOB date NOT NULL,
Gender char(1) NOT NULL CHECK (Gender ='f' OR Gender ='m'),
Archive char(1) NULL,
Allergies varchar(200) NOT NULL,
Dietry_Requirements varchar(200) NOT NULL,
Disabilities varchar(200) NOT NULL,
Medicine_Requirements varchar(200) NOT NULL,
username varchar (30) NOT NULL,
password varchar (30) NOT NULL,
CONSTRAINT PK_Person_ID PRIMARY KEY (Person_ID)
CONSTRAINT FK_Person_Type_ID FOREIGN KEY (Person_Type_ID)
REFERENCES Person_Type (Person_Type_ID));
You missed a comma! This should work...
CREATE TABLE Person(
Person_ID int auto_increment NOT NULL,
Person_Type_ID int NOT NULL,
Create_Date datetime NOT NULL ,
Modify_Date datetime NOT NULL ,
First_Name varchar(50) NOT NULL,
Surname varchar(50) NOT NULL,
DOB date NOT NULL,
Gender char(1) NOT NULL CHECK (Gender ='f' OR Gender ='m'),
Archive char(1) NULL,
Allergies varchar(200) NOT NULL,
Dietry_Requirements varchar(200) NOT NULL,
Disabilities varchar(200) NOT NULL,
Medicine_Requirements varchar(200) NOT NULL,
username varchar (30) NOT NULL,
password varchar (30) NOT NULL,
CONSTRAINT PK_Person_ID PRIMARY KEY (Person_ID),
CONSTRAINT FK_Person_Type_ID FOREIGN KEY (Person_Type_ID)
REFERENCES Person_Type (Person_Type_ID));