sql query default in oracle - sql

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.

Related

How to add NULL vs NOT NULL contraint based on data in another column?

For my semester project we are creating our own database. I'm currently creating the database and tables. I'm using SQL in Microsoft SQL Server Management Studio.
I want to make columns Beds, Baths, Sqft only be NULL if Lot/Land is selected in the Type column (if any other option is selected, it needs to be NOT NULL).
CREATE TABLE Property(
PropertyID int IDENTITY(1,1) PRIMARY KEY,
StreetAddress1 varchar(n) NOT NULL,
StreetAddress2 varchar(n) NULL,
City varchar(n) NOT NULL,
State varchar(2) NOT NULL,
Type varhar(#) NOT NULL CHECK (Type IN 'Single Family', 'Apartment', 'Condo', 'Townhome', 'Manufactured','Lot/Land'),
Beds varchar(2) NULL,
Baths decimal(3,1) NULL,
Sqft varchar(5) NULL,
Acreage decimal(10,2) NOT NULL)
The solution will need to be in an ALTER function since I'm creating the table now.
Thanks in advance!
You want a separate check constraint for this:
CREATE TABLE Property (
PropertyID int IDENTITY(1,1) PRIMARY KEY,
StreetAddress1 varchar(n) NOT NULL,
StreetAddress2 varchar(n) NULL,
City varchar(n) NOT NULL,
State varchar(2) NOT NULL,
Type varchar(32) NOT NULL CHECK (Type IN 'Single Family', 'Apartment', 'Condo', 'Townhome', 'Manufactured', 'Lot/Land'),
Beds varchar(2) NULL,
Baths decimal(3,1) NULL,
Sqft varchar(5) NULL,
Acreage decimal(10,2) NOT NULL
constraint chk_property_lotland
check ( (type = 'Lot/Land' and Beds is null and Baths is null and Sqft is null) or
(type <> 'Lot/Land' and Beds is not null and Baths is not null and Sqft is not null)
)
);

Error converting data type varchar to numeric while inserting data

I have a problem with my first insert statement. I have searched online and nothing seems to help my understanding of this problem or how to fix it. When I execute the insert statement it gives me the message:
Error converting data type varchar to numeric.
Table structure:
CREATE TABLE BOOKS
(
ISBN INT NOT NULL UNIQUE,
TITLE VARCHAR(25) NOT NULL,
AuthorFname VARCHAR(20) NOT NULL,
AuthorLname VARCHAR(20) NOT NULL,
GENRE VARCHAR(10) NOT NULL,
PUBLISHER VARCHAR(30) NOT NULL,
PUBLISHERDATE DATE NOT NULL,
DESCRIPTION VARCHAR(300) NOT NULL,
EDITION varchar(100) NOT NULL,
FORMAT VARCHAR(20) NOT NULL,
PAGENUMBER INTEGER NOT NULL,
FILESIZE TEXT NOT NULL,
IMAGE varchar(20) NOT NULL,
THUMBNAIL varchar(20) NOT NULL,
PRICE DECIMAL(9,2) NOT NULL,
AVAILABILITY VARCHAR(20) NOT NULL,
PRIMARY KEY(ISBN),
FOREIGN KEY(TITLE) REFERENCES WISHLIST(TITLE)
)
CREATE TABLE USERINFORMATION
(
USEREMAIL VARCHAR(30) NOT NULL UNIQUE,
USERNAME VARCHAR(30) NOT NULL,
PASSWORD TEXT NOT NULL,
QUESTION1 TEXT NOT NULL,
QUESTION2 TEXT NOT NULL,
ANSWER1 VARCHAR(25) NOT NULL,
ANSWER2 VARCHAR(25) NOT NULL,
BIRTHDATE DATE NOT NULL,
TITLE VARCHAR(25) NOT NULL,
PRIMARY KEY(USEREMAIL),
FOREIGN KEY(USERNAME) REFERENCES REVIEWS(USERNAME)
)
CREATE TABLE BILLINGINFO
(
FIRSTNAME VARCHAR(20) NOT NULL UNIQUE,
LASTNAME VARCHAR(20) NOT NULL,
USERNAME VARCHAR(20) NOT NULL,
ADDRESS1 VARCHAR(35) NOT NULL,
ADDRESS2 VARCHAR(35) NOT NULL,
CITY VARCHAR(25) NOT NULL,
STATE VARCHAR(35) NOT NULL,
ZIP INTEGER NOT NULL,
PHONE INTEGER NOT NULL,
PAYMENTTYPE TEXT NOT NULL,
CARDNUMBER INTEGER NOT NULL,
SECURITYCODE INTEGER NOT NULL,
EXPIRATIONDATE DATE NOT NULL,
PRIMARY KEY(FIRSTNAME)
)
CREATE TABLE LENDINGHISTORY
(
TITLE VARCHAR(25) NOT NULL UNIQUE,
CHECKUOTDATE DATE NOT NULL,
DUEDATE DATE NOT NULL,
STATUS TEXT NOT NULL,
USERNAME VARCHAR(20) NOT NULL,
RETURNEDDATE DATE NOT NULL,
PRIMARY KEY(TITLE)
)
CREATE TABLE WISHLIST
(
TITLE VARCHAR(25) NOT NULL UNIQUE,
AUTHORFNAME TEXT NOT NULL,
AUTHORLNAME TEXT NOT NULL,
THUMBNAIL VARCHAR(20) NOT NULL,
GENRE VARCHAR(10) NOT NULL,
PRICE DECIMAL(9,2) NOT NULL,
PRIMARY KEY(TITLE)
)
CREATE TABLE REVIEWS
(
USERNAME VARCHAR(30) NOT NULL UNIQUE,
REVIEWTEXT VARCHAR(40) NOT NULL,
RATING INT NOT NULL,
TITLE VARCHAR(25) NOT NULL,
PRIMARY KEY(USERNAME)
)
ALTER TABLE REVIEWS
ADD FOREIGN KEY(TITLE)
REFERENCES WISHLIST(TITLE)
INSERT INTO BOOKS
VALUES ('0547928220', 'The Hobbit', 'J.J.R.', 'Tolkien', 'Fantasy', 'Houghton Mifflin Harcourt', '9-18-2012',
'"In a hole in the ground, there lived a hobbit." So begins one of the most beloved and delightful tales in the English language. Set in the imaginary world of Middle-earth, at once a classic myth and a modern fairy tale, The Hobbit is one of literature s most enduring and well-loved novels.',
'75Th Anniversary Edition', 'PaperBack', '320', 'N/A', 'the_Hobbit', 'the_Hobbit2', '$8.30', 'Available');
SELECT *
FROM BOOKS
I think there are a few different things going on here. First of all, you have a line that reads drop TABLE BOOKS before the insert, which will remove the table from the registry before you can add any rows to it.
Secondly, the numerics you're trying to insert are surrounded by quotes and SQL doesn't do very well with implicit conversions to begin with. Your ISBN should probably be a VARCHAR since it contains a leading 0, which will be dropped in the event it actually converts. So #marc_s is right, drop the quotes from all your numeric values to be inserted; this includes ISBN (currently), page number, and price.
I believe the error actually refers to what you have entered in the price field, which is $8.30. A numeric doesn't know how to read $ in SQL. Also, there is a data type that coners such needs called money. Drop the $ and change the data type of the price column to money and you should be OK.
Hope this helps.
-C§

SQL database management

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,
...
)

postgresql syntax error when creating a table

Hey everyone I need some help with creating tables. I have the script below and it creates a few tables. When i try to run the script it give me this error:
psql:script.sql:10: ERROR: syntax error at or near "Group"
LINE 6: CREATE TABLE Group(
Can anyone tell me what is going on?
CREATE TABLE Group(
name varchar(40) PRIMARY KEY NOT NULL
);
CREATE TABLE Artist(
name varchar(30) PRIMARY KEY NOT NULL,
birthplace varchar(20) NOT NULL,
age int NOT NULL CHECK (age > 0),
style varchar(20) NOT NULL
);
CREATE TABLE Artwork(
title varchar(40) PRIMARY KEY NOT NULL,
artist varchar(30) NOT NULL references Artist(name),
group_name varchar(40) NOT NULL references Group(name),
year int NOT NULL CHECK (year > 0),
type varchar(30) NOT NULL,
price money NOT NULL,
);
CREATE TABLE Customer(
cust_id int PRIMARY KEY NOT NULL,
name varchar(40) NOT NULL,
address varcahr(60) NOT NULL,
amount money NOT NULL CHECK(amount > 0),
like_artist varchar(30) NOT NULL references Artist(name),
like_group varchar(40) NOT NULL references Group(name)
);
it had a lot of problems
this worked for me. In postgres, names that are not all lowercase need to be double quoted. also some of your table names are reserved words, money can't be > and int, and there was a comma out of place.
CREATE TABLE "group"( name varchar(40) PRIMARY KEY NOT NULL );
CREATE TABLE artist( name varchar(30) PRIMARY KEY NOT NULL, birthplace varchar(20) NOT NULL, age int NOT NULL CHECK (age > 0), style varchar(20) NOT NULL );
CREATE TABLE artwork( title varchar(40) PRIMARY KEY NOT NULL, artist varchar(30) NOT NULL references artist(name),
group_name varchar(40) NOT NULL references "group"(name), year int NOT NULL CHECK (year > 0), type varchar(30) NOT NULL, price money NOT NULL );
CREATE TABLE customer( cust_id int PRIMARY KEY NOT NULL, name varchar(40) NOT NULL, address varchar(60) NOT NULL,
amount money NOT NULL CHECK(amount > cast(0.0 as money)), like_artist varchar(30) NOT NULL references artist(name), like_group varchar(40) NOT NULL references "group"(name) );

Problem with SQL syntax (probably simple)

I'm doing some custom database work for a module for Drupal and I get the following SQL error when trying to create my table:
user warning: 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 'DEFAULT NULL, rooms INT DEFAULT NULL, adults INT DEFAULT NULL, children' at line 14 query: CREATE TABLE dr_enquiry ( eId INT unsigned NOT NULL auto_increment, eKey VARCHAR(16) NOT NULL, dateSent INT NOT NULL DEFAULT 0, status VARCHAR(30) NOT NULL DEFAULT 'Unanswered', custName VARCHAR(50) NOT NULL, custEmail VARCHAR(200) NOT NULL, custPhone VARCHAR(25) NOT NULL, custCountry VARCHAR(40) NOT NULL, custIP VARCHAR(11) DEFAULT NULL, offerName VARCHAR(100) NOT NULL, offerURL VARCHAR(200) NOT NULL, arrival DATETIME DEFAULT NULL, departure DEFAULT NULL, rooms INT DEFAULT NULL, adults INT DEFAULT NULL, children INT DEFAULT NULL, childAges VARCHAR(32) DEFAULT NULL, toddlers INT DEFAULT NULL, toddlerAges VARCHAR(32) DEFAULT NULL, catering VARCHAR(255) DEFAULT NULL, comments VARCHAR(255) DEFAULT NULL, agent VARCHAR(100) DEFAULT NULL, voucher VARCHAR(100) DEFAULT NULL, PRIMARY KEY (eId) ) /*!40100 DEFAULT CHARACTER SET UTF8 */ in /home/travelco/public_html/includes/database.inc on line 550.
The 'departure' field doesn't seem to have a type eg varchar, mediumint etc. Try adding one and see if that solves your problem :)
Nullability is not set using a DEFAULT constraint. Thus, the compiler is balking at each instance of DEFAULT NULL. Thus, the create statement should look like:
CREATE TABLE dr_enquiry (
eId INT unsigned NOT NULL auto_increment
, eKey VARCHAR(16) NOT NULL
, dateSent INT NOT NULL DEFAULT 0
, status VARCHAR(30) NOT NULL DEFAULT 'Unanswered'
, custName VARCHAR(50) NOT NULL
, custEmail VARCHAR(200) NOT NULL
, custPhone VARCHAR(25) NOT NULL
, custCountry VARCHAR(40) NOT NULL
, custIP VARCHAR(11) NULL
, offerName VARCHAR(100) NOT NULL
, offerURL VARCHAR(200) NOT NULL
, arrival DATETIME NULL
, departure DATETIME NULL
, rooms INT NULL
, adults INT NULL
, children INT NULL
, childAges VARCHAR(32) NULL
, toddlers INT NULL
, toddlerAges VARCHAR(32) NULL
, catering VARCHAR(255) NULL
, comments VARCHAR(255) NULL
, agent VARCHAR(100) NULL
, voucher VARCHAR(100) NULL
, PRIMARY KEY (eId)
)
(Oh and departure did not have a datatype as richsage mentioned. I presumed it was DateTime.)