Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all you must create the Categories lookup table. You could create it with a script like this:</p> <pre><code>SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [Categories]( [Id] [uniqueidentifier] ROWGUIDCOL NOT NULL, [Description] [varchar](50) NOT NULL, CONSTRAINT [PK_Categories] 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 SET ANSI_PADDING ON GO ALTER TABLE [Categories] ADD CONSTRAINT [DF_Categories_Id] DEFAULT (newid()) FOR [Id] GO </code></pre> <p>and populate it with the 4 descriptions:</p> <pre><code>INSERT INTO Categories ([Description]) VALUES ('Client') INSERT INTO Categories ([Description]) VALUES ('Worker') INSERT INTO Categories ([Description]) VALUES ('Assessor') INSERT INTO Categories ([Description]) VALUES ('Unknown') </code></pre> <p>After that, you must create the Persons table, with the constraint with the Categories table:</p> <pre><code>SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [Persons]( [Id] [uniqueidentifier] ROWGUIDCOL NOT NULL, [IdCategory] [uniqueidentifier] NOT NULL, [FirstName] [varchar](50) NOT NULL, [LastName] [varchar](50) NOT NULL, [FirstAddress] [varchar](250) NULL, [SecondAddress] [varchar](250) NULL, [ThirdAddress] [varchar](250) NULL, [HomePhone] [varchar](50) NULL, [MobilePhone] [varchar](50) NULL, [Modified] [datetime] NOT NULL, [Comment] [varchar](500) NULL, CONSTRAINT [PK_Persons] 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 SET ANSI_PADDING ON GO ALTER TABLE [Persons] ADD CONSTRAINT [DF_Persons_Id] DEFAULT (newid()) FOR [Id] GO ALTER TABLE [Persons] WITH CHECK ADD CONSTRAINT [FK_Persons_Categories] FOREIGN KEY([IdCategory]) REFERENCES [Categories] ([Id]) GO ALTER TABLE [Persons] CHECK CONSTRAINT [FK_Persons_Categories] GO </code></pre> <p>Now you have the two tables :)</p> <p>These scripts run on Microsoft SQL Server.</p> <p>PS: Best practice is to do another table for multiple addresses and multiple phones ;)</p> <p>To retrieve data with the SELECT, you could do:</p> <pre><code>SELECT FirstName, LastName, Description AS Category FROM Persons INNER JOIN Categories ON Categories.Id = Persons.IdCategory </code></pre> <p>If you're using NUMBER for the IDs, change the IdCategory datatype in NUMBER. After that, the storedprocedure could be like:</p> <pre><code>CREATE PROCEDURE [AddPerson] ( @Category varchar(50), @FirstName varchar(50), @LastName varchar(50), @FirstAddress varchar(250) = NULL, @SecondAddress varchar(250) = NULL, @ThirdAddress varchar(250) = NULL, @HomePhone varchar(50) = NULL, @MobilePhone varchar(50) = NULL, @Comment varchar(500) = NULL ) AS BEGIN Declare @idCategory NUMBER Set @idCategory = SELECT Id FROM Categories WHERE Description = Category INSERT INTO Persons ([IdCategory], [FirstName], [LastName], [FirstAddress], [SecondAddress], [ThirdAddress], [HomePhone], [MobilePhone], [Modified], [Comment]) VALUES (@idCategory, @FirstName, @LastName, @FirstAddress, @SecondAddress, @ThirdAddress, @HomePhone, GETDATE(), @MobilePhone, @Comment) END GO </code></pre> <p>I'm sorry if this codes contains some errors but I wrote it with notepad :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload