SQL Server - Ayuda a hacer una relacion de tablas

 
Vista:
Imágen de perfil de José Manuel

Ayuda a hacer una relacion de tablas

Publicado por José Manuel (1 intervención) el 04/10/2015 22:43:23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
create database ejercicio4_alter
use ejercicio4_alter
 
 create table tipohabitacion(
	codtipohabitacion varchar(2) not null,
	precio int,
	descripcion varchar(100),
	constraint pk_tipohabitacion primary key(codtipohabitacion)
 )
 
create table habitacion (
	nrohabitacion varchar(4) not null,
	descripcion varchar(100),
	codtipohabitacion varchar(2),
	ubicacion varchar(20),
	tienebaño bit,
	tienetv bit,
	tieneaireacondicionado bit,
	tienecalefaccion bit,
	tienefriobar bit,
	tieneyacusi bit,
	estado tinyint,
	constraint pk_habitacion primary key (nrohabitacion),
	constraint fk_tipohabitacion_habitacion foreign key (codtipohabitacion) references tipohabitacion (codtipohabitacion),
 )
 
 create table turno (
	codturno varchar(2) not null,
	codempleado varchar(4),
	descripcion varchar(20),
	constraint pk_turno primary key (codturno),
 )
 create table cliente(
	codcliente int not null,
	nombreorazonsocial varchar(100),
	direccionciudad varchar(100),
	tipopersona tinyint,
	dni varchar(8),
	ruc varchar(11),
	telefono varchar(30),
	movil varchar(30),
	constraint pk_cliente primary key (codcliente)
 )
 
 create table alquiler(
	nroalquiler int not null,
	codturno varchar(2),
	codcliente int,
	nrohabitacion varchar(4),
	fechaalquiler datetime,
	nrodias int,
	monto numeric(18,2),
	observaciones varchar(200),
	estado tinyint,
	constraint pk_alquiler primary key (nroalquiler),
	constraint fk_habitacion_alquiler foreign key (nrohabitacion) references habitacion (nrohabitacion),
	constraint fk_turno_alquiler foreign key (codturno) references turno (codturno),
	constraint fk_cliente_alquiler foreign key (codcliente) references cliente (codcliente)
 
 )
 
 
 create table productos(
	codproducto varchar(8) not null,
	descripcion varchar(100),
	umedida varchar(5),
	stock numeric(18,2),
	precioventa numeric(18,2),
	ucosto numeric(18,4),
	constraint pk_productos primary key (codproducto)
)
 
create table consumos (
	nroalquiler int not null,
	codproducto varchar(8) not null,
	cantidad numeric(18,2),
	precio numeric (18,2),
	subtotal numeric(18,2),
	constraint pk_consumos primary key (nroalquiler,codproducto),
	constraint fk_alquiler_consumos foreign key (nroalquiler) references alquiler(nroalquiler),
	constraint fk_productos_consumos foreign key (codproducto) references productos (codproducto)
)
 
 
 
-------------segunda etapa------------
create table empleados(
	codempleado varchar(2) not null,
	nombres varchar(100),
	direccion varchar(100),
	fechaingreso datetime,
	constraint pk_empleados primary key (codempleado),
)
alter table consumos drop constraint fk_alquiler_consumos
alter table alquiler drop constraint fk_turno_alquiler
alter table alquiler drop constraint pk_alquiler
alter table alquiler drop column codturno
alter table alquiler add  codempleado varchar(2) not null
alter table alquiler add constraint pk_alquiler primary key(nroalquiler,codempleado)
 
 
alter table alquiler add constraint fk_empleados_alquiler foreign key (codempleado) references empleados (codempleado)
 
alter table consumos add constraint fk_alquiler_consumos foreign key (nroalquiler) references alquiler (nroalquiler)-...............................AQUI EL PROBLEMA

A LA TABLA CONSUMOS LLEGAN DOS FORANEAS LA DE ALQUILER Y PRODUCTO
BORRE LA RELACION CON ALQUILER PARA CAMBIAR TIPO DE DATO

LUEGO INTENTE RELACIONARLOS NUEVAMENTE PERO NO ME DEJA
PERO LA RELACION PRODUCTOS_CONSUMOS ESA SI ESTA NORMAL

INTENTE BORRAR LA TABLA CONSUMO Y CREAR LA RELACION NUEVAMENTE Y NO ME DEJA
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Isaias
Val: 3.250
Oro
Ha mantenido su posición en SQL Server (en relación al último mes)
Gráfica de SQL Server

Ayuda a hacer una relacion de tablas

Publicado por Isaias (4558 intervenciones) el 05/10/2015 18:04:47
Cuando dices: "No me deja", sera acaso porque te manda este mensaje:

Msg 1776, Level 16, State 0, Line 7
There are no primary or candidate keys in the referenced table 'alquiler' that match the referencing column list in the foreign key 'fk_alquiler_consumos'.
Msg 1750, Level 16, State 0, Line 7
Could not create constraint. See previous errors.

Y creo que el mensaje es muy claro, NO TIENES una PK definida en la tabla ALQUILER.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar