Invalid identifier error but i can't see why - sql

Started learning SQL and am having a go at creating a script. The code looks perfectly fine to me but I keep getting the invalid identifier error. I've checked the code over and over again but everything seems ok. I'm going mad here. I am using oracle by the way.
create table Products ( ID int not null, Item varchar(30) not null, Size
varchar(1) not null);
insert into Products values ( 321, 'T-shirt', 'M');
insert into Products values ( 211, 'Jeans', 'L');

Size is a reserved word in Oracle, try changing the column name to an unreserved word.
See here for the full list of reserved words

size is a reserved word in Oracle's SQL (not sure if it is according to the ANSI standard, but some databases, like MySQL, definitely allow it).
You could escape it by using double quotes ("):
CREATE TABLE Products (
ID INT NOT NULL,
Item VARCHAR(30) NOT NULL,
"Size" VARCHAR(1) NOT NULL
);
But it would be much easier to just choose a name that isn't a reserved word:
CREATE TABLE Products (
ID INT NOT NULL,
Item VARCHAR(30) NOT NULL,
ProductSize VARCHAR(1) NOT NULL
);

Related

Using like for numeric in a constraint

I am creating a table which is like this:
CREATE TABLE Peeps
(
Name VARCHAR(255) NOT NULL,
PhoneNum BIGINT NOT NULL,
CONSTRAINT PhoneNum_Check CHECK (PhoneNum LIKE '08%')
)
Every phone number has to start with 08. However when I tried insert there's an error because LIKE can't be used for numeric (or that's what my friend said). The alternative would be using VARCHAR for PhoneNum, but this is an assignment and we have to use numeric for the phone number.
If a phone number can start with a 0 then you need to use a string:
CREATE TABLE Peeps (
Name VARCHAR(255) NOT NULL,
PhoneNum VARCHAR(255) NOT NULL,
CONSTRAINT PhoneNum_Check CHECK (PhoneNum LIKE '08%')
);
Although you can use LIKE on a number, it is highly not recommended. What happens is that the number is converted to a string. However, that string will never start with a 0 -- well, at least never when the value is greater than 1.

SQL Server 2014 : help creating tables

I am new to MSSQL 2014 Server, my professor listed these steps to make a table, I don't know the proper steps to create tables in the pictures listed below, please help.
Create and populate (insert values) the following tables per table description and data values provided
DEPARTMENT
EMPLOYEE
PROJECT
ASSIGNMENT
Add a SQL Comment to include /* * Your First Name_Your Last Name* */ when inserting corresponding values for each table.
What I tried so far:
CREATE TABLE DEPARTMENT(
DepartmentName Text(35) PRIMARY KEY,
BudgetCode Text(30) NOT NULL,
OfficeNumber Text(15) NOT NULL,
Phone Text(12) NOT NULL, );
I have put this to my query and the error is
Msg 2716, Level 16, State 1, Line 1 Column, parameter, or variable #1: Cannot specify a column width on data type text.
Try this(I assume that your table exists in dbo schema):
IF OBJECT_ID(N'dbo.DEPARTMENT', N'U') IS NOT NULL
BEGIN
DROP TABLE DEPARTMENT
END
GO
CREATE TABLE DEPARTMENT(
DepartmentName varchar(35) PRIMARY KEY,
BudgetCode varchar(30) NOT NULL,
OfficeNumber varchar(15) NOT NULL,
Phone varchar(12) NOT NULL
);
You can not define width for Text data type. In case which you need to define width you can use char or varchar data types. Also keep in mind that if you need to work with Unicode characters then you will need to use nchar or nvarchar instead.

Oracle SQL Query - Age

So I have the following table in my Oracle Database:
create table FilmStar (
filmStarID char(25) not null,
filmStarName char(50) not null,
birthplace char(50) not null,
yearBorn char(25) not null,
yearDied char(25),
primary key (filmStarID)
);
insert into FilmStar values ('0001','Tim Robbins','California, United States','1958',null);
insert into FilmStar values ('0005','Marlon Brando','Nebraska, United States','1924','2004');
I need to complete the following query: List the unique numbers, names, and ages of all film stars who are deceased.
I believe that I need to
select * from FilmStar where yearDied is not null
but how to find out the age?
I prefer to be explicit about type conversions:
select . . .,
(cast(yearDied as int) - cast(yearBorn as int) ) as approximate_age
from FilmStart
where yearDied is not null;
Relying on implicit conversions can result in very hard to find errors. Storing numbers as strings just exacerbates this problem.
Note: in Oracle, one is inclined to use to_number() rather than cast() for this purpose.

SQL server Invalid Column name Invalid object name

I'm having a problem with a table I created. I am trying to run a query however a red line appears under my code ('excursionID', and 'excursions'), claiming 'Invalid Column name 'excursionID' and 'Invalid object name 'dbo.excursions' even though I have created the table already!
Here is the query
SELECT
excursionID
FROM [dbo].[excursions]
Here is the query I used to create the table
USE [zachtravelagency]
CREATE TABLE excursions (
[excursionID] INTEGER NOT NULL IDENTITY (1,1) PRIMARY KEY,
[companyName] NVARCHAR (30) NOT NULL,
[location] NVARCHAR (30) NOT NULL,
[description] NVARCHAR (30) NOT NULL,
[date] DATE NOT NULL,
[totalCost] DECIMAL NOT NULL,
I've tried dropping the table and inserting table again.
For some reason all my other tables work, it's just this table that doesn't identify itself. I'm very new to SQL so thank you for your patience!
You use DB [zachtravelagency] for create table.And You dont use this DB in your query. Default used db master in SSMS. Try
SELECT
excursionID
FROM [zachtravelagency].[excursions]

Why cant i use the field user in SQL Server 8?

Maybe not literally but the query below gets an error near user. If i change it to userZ it works. WHY can i not use that name? Is there a way to specific its a field instead of a keyword? (or whatever it is)
create table Post2 (
id INTEGER PRIMARY KEY NOT NULL,
title nvarchar(max)
NOT NULL,
body nvarchar(max)
NOT NULL,
user integer REFERENCES Post1(id));
Reserved words, like user should be enclosed in brackets.
Take a look at Using Identifiers for a more in depth explanation.
CREATE TABLE Post2 (
id INTEGER PRIMARY KEY NOT NULL
, title NVARCHAR(MAX)NOT NULL
, body NVARCHAR(MAX) NOT NULL
, [User] INTEGER REFERENCES Post1(id)
);
USER is a reserved word.
Try delimiting it with "" (e.g. "user" or [user])
Read more on using delimited identifiers here.