3

I am new to SQL and I have this problem, I can't seem to search on google. This is my school work so please be patient with my coding. Now the problem is i am doing a select query and SQL does not seem to recognize the column I need

here is an attached picture, it just says invalid column but it is clear that the column exist

Dropbox link to the picture

here is the complete code

create database Hairsalon

use Hairsalon   

create table Employee
(
    Employee_ID int primary key,
    Name varchar(20),
    Birthday date,
    Gender char(1)
);

create table Inventory
(
    Inventory_ID int primary key,
    Name varchar(30),
    Stock int   
)

create table Record
(
    Transaction_Number int primary key,
    Transaction_Date date,
    Transaction_Time time
);

alter table Record
    drop column Transaction_Date
alter table Record
    drop column Transaction_Time
alter table Record
    add Transaction_DT varchar(30)
alter table Record
    add Transaction_Description varchar(255)

Create table Inventory_Record
(
    Transaction_Number int primary key foreign key references Record(Transaction_Number),
    Inventory_ID int foreign key references Inventory(Inventory_ID),
);

create table Customer_Record
(
    Transaction_Number int foreign key references Record(Transaction_Number),
    Customer_ID int primary key,
    Service_Description varchar(255),
    Pay money
);

create table Customer
(
    Customer_ID int foreign key references Customer_Record(Customer_ID),
    Name varchar(20),
    Payable money,
    Session_DT varchar(20)
);

create table Employee_Record
(
    Transaction_Number int primary key foreign key references Record(Transaction_Number),
    Employee_ID int foreign key references Employee(Employee_ID),
    Time_In time,
    Time_Out time,
    Record_Date date,
    Customer_Count int
);

create table Salon
(
    Customer_ID  int foreign key references Customer_Record(Customer_ID),
    Employee_ID  int foreign key references Employee(Employee_ID),
    Inventory_ID int foreign key references Inventory(Inventory_ID),
    Transaction_Number int foreign key references Record(Transaction_Number)
);

alter table Customer
    add Session_DT varchar(20)

alter table Customer
    drop column Customer_ID
alter table Customer
    add Customer_ID int foreign key references Customer_Record(Customer_ID)

alter table Customer_Record
    add Employee_ID int foreign key references Employee(Employee_ID)

alter table Employee
    add [Type] varchar(15)

alter table Employee
    drop column Birthday
    alter table Employee
    drop column Birthday

alter table Employee_Record
    drop column Time_In
alter table Employee_Record
    drop column Time_Out
alter table Employee_Record
    drop column Record_Date
alter table Employee_Record
    add Time_In varchar(20)
alter table Employee_Record
    add Time_Out varchar(20)
alter table Employee_Record
    add Record_Date varchar(20)
alter table Employee_Record
    drop column Customer_Count

insert into Employee 
values(1,'Test Employee','M','Cashier','bday')
insert into Employee 
values(-1,'Null','N','Null','Null')
insert into Employee values(1,'test1','M','HairDresser','9/8/2014')

INSERT INTO Record  values(2,'')
INSERT INTO Customer_Record values(1,1,'asd',123.00,null)
INSERT INTO Customer values(1,'test1',0,'9/8/2014 8:16 AM')

SELECT * FROM Record
select * from Customer
SELECT * FROM Customer_Record
SELECT * FROM Employee

SELECT DISTINCT Customer.Name as 'Customer Name', Employee.Name as 'Attendee'
FROM Customer, Customer_Record, Employee_Record, Employee
WHERE ( Customer_Record.Transaction_Number = Employee_Record.Transaction_Number 
        AND Employee_Record.Employee_ID = Employee.Employee_ID
        AND Customer.Customer_ID = Customer_Record.Customer_ID
        ) OR ( Customer_Record.Transaction_Number = Employee_Record.Transaction_Number 
        AND Customer_Record.Employee_ID = -1
        AND Customer.Customer_ID = Customer_Record.Customer_ID
        )

insert into Employee_Record values(9,1,'10:00','10:99','date')

select * from Record
select * from Customer
select * from Employee
select * from Employee_Record
select * from Customer_Record

select count(Customer_ID) from Customer
select count(Transaction_Number) from Record

insert into Customer
values(1,'test',120,DATEFROMPARTS(32,12,31))

INSERT INTO Customer_Record values(1,1,'Long Haired Customer: ',0, null)

DELETE FROM Customer WHERE Customer_ID = 1

DELETE FROM Customer
DELETE FROM Customer_Record
DELETE FROM Record

DELETE FROM Employee
DELETE FROM Employee_Record
DELETE FROM Inventory
DELETE FROM Inventory_Record

commit
4
  • Do you actually receive an error when you run the statement? From the attached image it looks like Intellisense doesn't recognise the column but it should run. Commented Sep 8, 2014 at 7:50
  • it runs, with no error, it has the squigly red lines though that says invalid column when I hover Commented Sep 8, 2014 at 8:09
  • 1
    As @Ndech says below, this is probably an Intellisense cache problem, use Ctrl + Shift + R to refresh the cache. Alternatively put the statement GO in your query after you have created your tables and altered them and before your select statements. GO is used by SSMS to break the query up into batches it understands. Commented Sep 8, 2014 at 9:01
  • I would recommend that you ditch the old style JOINS and start using the SQL92 standard INNER JOIN tableB ON tableA.col1 = tableB.col2. See sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins Commented Sep 8, 2014 at 15:09

1 Answer 1

1

According to the DDL you have presented, it seems quite obvious that Customer_Record has no Employee_ID.

You may have mixed up Customer_Record with Employee_Record or Employee_ID with Customer_ID, but something is wrong.

EDIT I had missed that you alter the table in your script to add the missing columns. In that case, Intellisense (the auto-completion system) does not take it into account unless you manually reload it (using Ctrl + Shift + R). Your query is correct and the errors will disappear once Intellisense is up to date.

Sign up to request clarification or add additional context in comments.

4 Comments

To add to this answer it's likely the Object Explorer in SSMS is simply out of sync. Try refreshing the table (right-click, refresh; or hit the refresh button at the top). That, or it's showing a different database/server where you had a different schema.
And furthermore = null will always be false. You must mean is null. See also http://www.sqlservercentral.com/blogs/steve_jones/2010/10/13/common-...ls-null/
well i kind of have this query alter table Customer_Record add Employee_ID int foreign key references Employee(Employee_ID) and on the object explorer it shows that there is a foreign key to Employee_ID i'm on my dead end
Ok I had missed that. As suggested by @lc, it is probably an intellisence problem. Try hitting Ctrl + Shift + R to reload it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.