SQL查询错误:列名无效

时间:2014-09-08 07:43:12

标签: sql sql-server

我是SQL的新手,我遇到了这个问题,我似乎无法在谷歌上搜索。 这是我的学校工作,所以请耐心等待我的编码。 现在的问题是我正在进行选择查询,而SQL似乎无法识别我需要的列

这是附图,它只是说无效列,但很明显该列存在

Dropbox link to the picture

这是完整的代码

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

1 个答案:

答案 0 :(得分:1)

根据您提供的DDL,很明显Customer_Record没有Employee_ID。

您可能已将Customer_Record与Employee_Record或Employee_ID与Customer_ID混淆,但出现了问题。

编辑我错过了您在脚本中更改表以添加缺少的列。在这种情况下,Intellisense(自动完成系统)不会考虑它,除非您手动重新加载它(使用Ctrl + Shift + R)。您的查询是正确的,一旦Intellisense是最新的,错误就会消失。