2017-04-04 20 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