Syntax Error- SQL Microsoft Access - sql

I will appreciate nay help I am new to SQL and I am using Microsoft Access. I wrote this myself so if something looks wrong, please let me know!
Also if you could answer this question, I have to write and run a SQL query to list all occurrences of a certain course.How do I begin going about doing this? I cannot locate anything on it in my book.
CREATE TABLE CUSTOMER (
CustomerNumber Int PRIMARY KEY,
CustomerLastName Char (25) NOT NULL,
CustomerFirstName Char (25) NOT NULL,
Phone Char (12) NULL
);
CREATE TABLE COURSE (
CourseNumber Int PRIMARY KEY,
Course Char ( 15) NOT NULL,
CourseDate DateTime NOT NULL,
Fee Currency NOT NULL
);
CREATE TABLE ENROLLMENT (
CustomerNumber Int PRIMARY KEY,
CourseNumber Int NOT NULL,
AmountPaid Currency NOT NULL
);

I test your queries on MS Acces 2007 and MS Access 2010 and works fine,
You should run each query at a time, not all at once, when I try to execute the entire SQL I get the same error message.
SQL1:
CREATE TABLE CUSTOMER (
CustomerNumber Int PRIMARY KEY,
CustomerLastName Char (25) NOT NULL,
CustomerFirstName Char (25) NOT NULL,
Phone Char (12) NULL
);
SQL2:
CREATE TABLE COURSE (
CourseNumber Int PRIMARY KEY,
Course Char ( 15) NOT NULL,
CourseDate DateTime NOT NULL,
Fee Currency NOT NULL
);
SQL3:
CREATE TABLE ENROLLMENT (
CustomerNumber Int PRIMARY KEY,
CourseNumber Int NOT NULL,
AmountPaid Currency NOT NULL
);

Few comments that may help you, first check the valid data types in MS-Access in your current version, for example: MSDN-MS-Access DataTypes. You will see that Char and Int are not valid, also Numeric does not take arguments (you probably wanted to use Dobule). Also you are missing a comma (,) after the PRIMARY KEY in the first CREATE TABLE CUSTOMER statement. It may be a good practice using the GUI interface to create tables then move into DDL later.

Related

Trying to create a Composite Primary Key in MSSQL with only a part of 2 columns

I'm new to SQL and am following a course, however it does not cover the "create table" part.
It only covers statements etc.
What I would like to have is, my primary key (cust_id) to be generated with the "first_name" and the first 3 letters of "last_name".
I.E. I want "John Smith" to become "custid"; JOHNSMI.
I have below code which works (without composite).
CREATE TABLE NL_client (
custid INT PRIMARY KEY IDENTITY (10000, 1),
userid VARCHAR (50) NOT NULL,
first_name VARCHAR (50) NOT NULL,
last_name VARCHAR (50) NOT NULL,
birthday DATE
);
And I found below code (last line added)
CREATE TABLE SAMPLE_TABLE (
custid INT,
userid VARCHAR (50) NOT NULL,
first_name VARCHAR (50) NOT NULL,
last_name VARCHAR (50) NOT NULL,
PRIMARY KEY (first_name, last_name),
);
However, when trying to execte the second query as displayed above it does not create a Primary key. Or when I execure the below queries;
custid INT PRIMARY KEY (first_name, last_name),
Either above in the query or at the end, it does not make a primary key.
Furthermore, I have no idea, nor was I able to find (perhaps I searched wrongly, surely I'm not the first with this "problem") how to select only the first 3 letters of "last_name" to be used as a part of the "custid".
Perhaps this is not possible and I should use "custerid" as an INT Primary key and use "userid" as a composite.
But it would surely help me in the future to be able to use the Primary Key as a reference in Python.
Many thanks in advance for your help and let me learn to understand why it doesn't work!

I am creating a table in pgweb Heroku, and get this error "ERROR: pq: syntax error at or near "(""

Here is my exact query
CREATE Table Publisher
(Publisher_Id Int primary key not null,
Name varchar(20) not null,
Address varchar(50) not null,
Phone Int(10),
Isbn varchar(13) references books (Isbn) not null
)
Any help would be greatly appreciated.
datatype int does not take a length. So:
create table publisher (
publisher_id int primary key,
name varchar(20) not null,
address varchar(50) not null,
phone int,
isbn varchar(13) references books (isbn) not null
);
Notes:
not null is redondant on a primary key column
int does not seem like a good pick for a phone number; you would typically need to store leading 0s, or allow special characters such as + or () - int cannot do that. A string datatype would probably be a better pick

what does the error "missing right parenthesis" in oracle sql means

I'm trying to run this code and it seems correct to me but I'm getting an error stating that there's a right parenthesis missing.
The code is the following:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
DATE_OF_BIRTH DATE,
PHONE_NB CHAR(8) NOT NULL,
ADDRESS VARCHAR(50),
TOTAL_SPENDING FLOAT NOT NULL DEFAULT 0.0,
PRIMARY KEY(CUSTOMER_ID)
);
Can anyone help me in solving my problem?
Since you tagged SQL Developer...
...the tool tries to give you a heads-up there will be a problem before you even hit the Execute button
The default value for the column is confusing the parser because it's not expected at that point.
Move it to after the data type and you'll be good
CREATE TABLE customer (
customer_id INT NOT NULL,
name VARCHAR2(30) NOT NULL,
date_of_birth DATE,
phone_nb CHAR(8) NOT NULL,
address VARCHAR(50),
total_spending FLOAT DEFAULT 0.0 NOT NULL,
PRIMARY KEY ( customer_id )
);
PS In oracle, use VARCHAR2, not VARCHAR. While VARCHAR will 'work', it's reserved and could mean something different in a future release.
You are using wrong order of column definition clauses: the constraint (NOT NULL) should follow the default value.
This is the right way:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
DATE_OF_BIRTH DATE,
PHONE_NB CHAR(8) NOT NULL,
ADDRESS VARCHAR(50),
TOTAL_SPENDING FLOAT DEFAULT 0.0 NOT NULL ,
PRIMARY KEY(CUSTOMER_ID)
);

SQL column maximum input values

hi please how do i give sql column maximum input example if i want max_num column to take only three(3) result sets or inputs
It's mostly the same in other rdbms. You need to specify right after the column type
MYSQL
CREATE TABLE TestTable(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
three_char_demo VARCHAR(3) NOT NULL,
)
PostgreSQL
CREATE TABLE TestTable(
ID INT PRIMARY KEY NOT NULL,
three_char_demo CHAR(3) NOT NULL,
);

create table in Oracle BD but gives error

CREATE TABLE employees (
id INT NOT NULL auto_increment PRIMARY KEY (ID),
first_name VARCHAR(20) DEFAULT NULL,
last_name VARCHAR(20) DEFAULT NULL,
salary INT DEFAULT NULL);
I think this is correct query to create table in Oracle database.. but it gives the following error:
ORA-00907: missing right parenthesis
How to correct the statement?
You can validate your SQL using formatting tools such as http://www.dpriver.com/pp/sqlformat.htm
auto_increment seems like a proprietary MySQL extension, so it's not valid for Oracle.
also, "id int not null auto_increment primary key (id)" does not need the last "(id)"
Using Oracle, you shoud try something like this
CREATE SEQUENCE seq;
CREATE TABLE employees
(
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR2(20) DEFAULT NULL,
last_name VARCHAR2(20) DEFAULT NULL,
salary INTEGER DEFAULT NULL
);
INSERT INTO employees
VALUES (seq.NEXTVAL,
'name',
'last name',
1);
Sometimes, SQL is fancy, because even having a standard (ANSI), most DBMS vendors add their proprietary extensions to the SQL creating their own languages, so it's rare the situation where you can port one SQL from one DB into another without any changes.
Also, it's a pretty useless error message. It could at least say which position. (also, there's no missing parenthesis, but an unexpected token)
EDITED : New feature 12c
CREATE TABLE employees(
id NUMBER GENERATED ALWAYS AS IDENTITY,
first_name VARCHAR2(30)
etc.
);
Why would you do default null?
The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings.
Replace
id INT NOT NULL auto_increment PRIMARY KEY (ID),
with
id INT NOT NULL auto_increment PRIMARY KEY,
this is more efficient
CREATE TABLE EMPLOYEES_T(
ID NUMBER,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(20) DEFAULT NULL,
SALARY INTEGER DEFAULT NULL,
CONSTRAINT PK_EMPLOYEES_T PRIMARY KEY(ID)
);