Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. /*CREATE TABLE Doctor(
  2. id BIGINT IDENTITY(1,1) NOT NULL,
  3. fio nVARCHAR(255) NOT NULL,
  4. department nVARCHAR(255),
  5. constraint pk_doctor PRIMARY KEY(id));
  6.  
  7. CREATE TABLE Patient(
  8. id BIGINT IDENTITY(1,1) NOT NULL,
  9. fio CHAR(30) NOT NULL,
  10. sex CHAR(7) null,
  11. birth_dt DATE NULL,
  12. constraint pk_patient PRIMARY KEY(id),
  13. check (sex='мужской' or sex='женский'));
  14.  
  15. create table Diagnosis(
  16. id bigint identity(1,1) not null,
  17. name nvarchar(255) not null,
  18. code char(7),
  19. constraint code_length check (len(code) between 4 and 7),
  20. constraint pk_diagnosis primary key(id)
  21. );
  22.  
  23. create table Case1(
  24. patient_id bigint not null,
  25. main_diag_id bigint not null,
  26. start_dt datetime null,
  27. end_dt datetime default getDate(),
  28. class_val int,
  29. constraint start_dt_check check(start_dt<=getDate()),
  30. constraint class_val_check check(class_val between 1 and 3),
  31. constraint patient_diag_pk primary key(patient_id,main_diag_id),
  32. constraint fk_patient foreign key(patient_id)
  33. references Patient(id),
  34. constraint fk_diag foreign key(main_diag_id)
  35. references Diagnosis(id));
  36.  
  37. create table Step(
  38. patient_id bigint not null,
  39. main_diag_id bigint not null,
  40. diagnosis_id bigint not null,
  41. doctor_id bigint not null,
  42. days_off int,
  43. constraint days_check check(days_off>0),
  44. constraint doctor_id_fk foreign key(doctor_id) references Doctor(id),
  45. constraint patient_diag_doctor_pk primary key(patient_id,main_diag_id,doctor_id),
  46. constraint patient_fk foreign key (patient_id)
  47. references Patient(id),
  48. constraint main_diag_fk foreign key (main_diag_id)
  49. references Diagnosis(id),
  50. constraint diagnosis_fk foreign key(diagnosis_id)
  51. references Diagnosis(id));
  52.  
  53. 1е задание:
  54. alter table Doctor add nauchnaya_stepen varchar(30),
  55. constraint stepen_check check (nauchnaya_stepen='специалист 1й степени' or nauchnaya_stepen='кандидат наук' or nauchnaya_stepen='специалист 2й степени');
  56.  
  57. 2е задание:
  58. alter table Case1 drop constraint start_dt_check;
  59. alter table Case1 add constraint start_dt_check2 check(start_dt between '01.01.2014' and '01.12.2014' and end_dt<'31.12.2014');
  60.  
  61. 3е задание:
  62. alter table Doctor drop constraint doctor_id_fk;
  63. alter table Doctor drop constraint pk_doctor;
  64. alter table Doctor drop column id;
  65. alter table Step add doctor_number bigint identity(1,1) not null;
  66. alter table Step add constraint number_doctor_fk foreign key(doctor_number) references Doctor(fio);
  67.  
  68. 4е задание:
  69.  
  70. insert into Patient (fio,sex,birth_dt) values
  71. ('Иванов Иван Иванович','мужской','20.10.1990'),
  72. ('Иванова Таня','женский','20.12.1980'),
  73. ('Табуреткин Александр Иванович','мужской','10.10.1990');
  74.  
  75. 5е задание:
  76. delete from Case1 where start_dt>'31.04.2014 0:00:00' and start_dt<'01.07.2014 0:00:00';
  77. insert into Diagnosis ([name], code) values
  78. ('name1', 'ktyu'),
  79. ('name2','kenr'),
  80. ('name3', 'gion');
  81. insert into Case1 (patient_id,main_diag_id,start_dt) values
  82. (1,1, '12.04.2014'),
  83. (2,2, '12.05.2014'),
  84. (3,3, '01.05.2014');*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement