Thursday, September 29, 2011

MSSQL table create with foreign key

USE [MyDb]
GO

CREATE TABLE [dbo].[MachineWorkInfoes](
[Id] [int] IDENTITY(1,1) NOT NULL,
[WorkDate] [datetime] NOT NULL,
[FieldEngineerId] [int] NULL,
[MachineId] [int] NOT NULL,
[TransportCount] [int] NOT NULL,
[TimeStart] [float] NOT NULL,
[TimeEnd] [float] NOT NULL,
[Kilometer] [float] NOT NULL,
[WorkTime] [float] NOT NULL,
[Code] [nvarchar](50) NULL,
[Name] [nvarchar](150) NOT NULL,
[Description] [nvarchar](500) NULL,
[Ordering] [int] NULL,
[IsActive] [bit] NOT NULL,
[Created] [datetime] NOT NULL,
[CreatedName] [nvarchar](50) NULL,
[Modified] [datetime] NOT NULL,
[ModifiedName] [nvarchar](50) NULL,
[ParentId] [int] NULL,
[SiteId] [int] NOT NULL,
[SectionId] [int] NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[MachineWorkInfoes]  WITH CHECK ADD  CONSTRAINT [MachineWorkInfo_FieldEngineer] FOREIGN KEY([FieldEngineerId])
REFERENCES [dbo].[EmployeeInfoes] ([Id])
GO

ALTER TABLE [dbo].[MachineWorkInfoes] CHECK CONSTRAINT [MachineWorkInfo_FieldEngineer]
GO

ALTER TABLE [dbo].[MachineWorkInfoes]  WITH CHECK ADD  CONSTRAINT [MachineWorkInfo_Machine] FOREIGN KEY([MachineId])
REFERENCES [dbo].[EmployeeWithCars] ([Id])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[MachineWorkInfoes] CHECK CONSTRAINT [MachineWorkInfo_Machine]
GO

ALTER TABLE [dbo].[MachineWorkInfoes]  WITH CHECK ADD  CONSTRAINT [MachineWorkInfo_Section] FOREIGN KEY([SectionId])
REFERENCES [dbo].[SectionInfoes] ([Id])
GO

ALTER TABLE [dbo].[MachineWorkInfoes] CHECK CONSTRAINT [MachineWorkInfo_Section]
GO


энэ жишээгээр бол relationship class ийг дураараа үүсгэж загварчилж болно.

Жишээ нь: ASP.NET entity framework дээр entity Class нь нэг иймэрхүү бичиглэлтэй болно
ямар ажилчин ямар тушаал дээр хэзээ ажилласанг бүртгэх кодын жишээ


public class MachineWorkInfo : NamedMetaData
    {
        [DisplayName("Ажил хийсэн өдөр")]
        [Required(ErrorMessage = "Ажил хийсэн өдөр заавал байх ёстой!")]
        public DateTime WorkDate { get; set; }
        [DisplayName("Хариуцсан ажилтан")]
        [Required(ErrorMessage = "Хариуцсан ажилтан заавал байх ёстой!")]
        public Nullable<int> FieldEngineerId { get; set; }
        public virtual EmployeeInfo FieldEngineer { get; set; }
        [DisplayName("Амжил хийсэн машин")]
        [Required(ErrorMessage = "Амжил хийсэн машин заавал байх ёстой!")]
        public int MachineId { get; set; }
        public virtual EmployeeWithCar Machine { get; set; }
        [DisplayName("Рэйсийн тоо")]
        [Required(ErrorMessage = "Рэйсийн тоо заавал байх ёстой!")]
        [RegularExpression(@"^\$?\d+(\.(\d{2}))?$", ErrorMessage = "Тоон утга байх ёстой!")]
        public int TransportCount { get; set; }
        [DisplayName("Спидометр явахад")]
        [Required(ErrorMessage = "Спидометр явахад заавал байх ёстой!")]
        [RegularExpression(@"^(-{0,1})([0-9]+)(\.{0,1})([0-9]*)$", ErrorMessage = "Тоон утга байх ёстой!")]
        public double TimeStart { get; set; }
        [DisplayName("Спидометр ирэхэд")]
        [Required(ErrorMessage = "Спидометр ирэхэд заавал байх ёстой!")]
        [RegularExpression(@"^(-{0,1})([0-9]+)(\.{0,1})([0-9]*)$", ErrorMessage = "Тоон утга байх ёстой!")]
        public double TimeEnd { get; set; }
        [DisplayName("Явсан километр")]
        [Required(ErrorMessage = "Явсан километр заавал байх ёстой!")]
        [RegularExpression(@"^(-{0,1})([0-9]+)(\.{0,1})([0-9]*)$", ErrorMessage = "Тоон утга байх ёстой!")]
        public double Kilometer { get; set; }
        [DisplayName("Ажилласан цаг")]
        [Required(ErrorMessage = "Ажилласан цаг заавал байх ёстой!")]
        [RegularExpression(@"^(-{0,1})([0-9]+)(\.{0,1})([0-9]*)$", ErrorMessage = "Тоон утга байх ёстой!")]
        public double WorkTime { get; set; }
    }


how to delete constrainted column from table

ALTER TABLE [dbo].[LanguageInfoes] DROP CONSTRAINT [FK_LanguageInfoes_SectionInfoes_SectionId]
GO
Exec ('DROP INDEX [' + @indexName + '] ON [' + @tableName + ']')
GO

No comments: