2017-04-04 19 views
1

我在sql「empleado」中有一個表,從中可以計算出我想要的屬性之一。如何在PosgreSQL中創建計算屬性

這是代碼:

create table empleado (
    dni char(9) not null, 
    contrasena varchar (20) not null, 
    nombre varchar (30) not null, 
    apellidos varchar (60) not null, 
    direccion varchar (80) not null, 
    telefono char (9) not null, 
    tipo varchar(30) not null, 
    fechaIngreso date not null, 
    antiguedad Integer as getdate()-fechaIngreso, 
    salario real not null check (salario >0), 

    primary key (dni) 
); 

屬性安蒂格達德必須是實際的日期 - 「fechaIngreso」 以天爲單位。我怎麼能解決這個問題?

謝謝

+0

您使用的是MySQL還是SQL Server? MySQL不支持'getdate()',計算列或檢查約束。請適當地標記你的問題。 –

+0

我使用的是ProsgreSQL,我認爲它是mysql – vps

+0

。 。 Postgres不同於MySQL和SQL Server。 –

回答

0

您的語法建議您使用的是SQL Server,而不是MySQL。你可以做你想做的,在SQL Server中的計算列:

create table empleado (
    dni char(9) not null, 
    contrasena varchar (20) not null, 
    nombre varchar (30) not null, 
    apellidos varchar (60) not null, 
    direccion varchar (80) not null, 
    telefono char (9) not null, 
    tipo varchar(30) not null, 
    fechaIngreso date not null, 
    antiguedad as (cast(getdate() - fechaIngreso as int)), 
    salario real not null check (salario > 0), 
    primary key (dni) 
); 

在MySQL中,你可以使用一個視圖做同樣的事情。

+0

它必須是MySQL,因爲隨着你的解決方案它對我說followinh: – vps

+0

語法錯誤在或接近「as」 – vps

0
create table empleado (
    dni char(9) not null, 
    contrasena varchar (20) not null, 
    nombre varchar (30) not null, 
    apellidos varchar (60) not null, 
    direccion varchar (80) not null, 
    telefono char (9) not null, 
    tipo varchar(30) not null, 
    fechaIngreso date not null, 
    antiguedad Integer as DATEDIFF(dd,fechaIngreso, getdate()) 
    salario real not null check (salario >0), 

    primary key (dni) 
); 
+0

它是mySql代碼 – vps