How to make tables in a database [closed] - sql-server

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am making a database about cars going around a certain street. So I made a table that keep the information of the CarId and the date when the car passed the particular street. The car can pass the street millions time, I know I need to make another table for that to keep all the dates but I am confused. Please help. How the database should look like

Without knowing what database you're using, I'm going to assume MS SQL Server / SQL Express. The following SQL should fulfil your requirements
-- Stores details about individual cars
create table Car
(
ID int identity(1,1) primary key,
Registration varchar(10),
Make varchar(20),
Model varchar(50)
-- Add any other fields here
)
go
-- Stores details about each time a "known" car passes by the location
create table Sightings
(
ID int identity(1,1) primary key,
CarID int not null,
constraint Sightings_FK_Car foreign key (CarID) references Car (ID),
SightedAt datetime not null
-- Add any other fields here
)
Using meaningless integers as primary keys on the tables is a matter of personal preference. You could use something like the registration number of the car as the primary key on the first table and a combination of the car registration and time of sighting on the second. I prefer the meaningless integer for performance reasons.

Related

How to improve a SQL Server table insertion performance by primary key or non-clustered index with uniqueness constraint? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
Improve this question
In order to control the duplicate rows in a SQL Server table, which approach will have the better performance for insert times in high loads?
Create a primary key constraint on a column that should have a unique value in the table (type of column is varchar(100) and the possible value is like g_12546987456_13-9. It means a composite primary key and no specific character orders)
Create a numeric and auto-incremented primary key and set a non-clustered index with uniqueness constraint on the string column (g_12546987456_13-9)
One thing you have to be vigilant about, when creating these Alpha-Numeric Primary keys, you have to make sure that your Alpha-Numeric values are Incremental.
The first option you have mentioned with Random values, will most certainly impact the performance massively. Because of the random Primary Key, it will end up inserting new rows on the existing data pages, thus pushing records down and ending up with Page splits - Very bad for SQL Server Performance. (One of the main reason why GUID is not a good candidate for a Primary Key and MS had to introduced sequential GUID).
I would suggest make use of SQL Server Sequence Object to Auto-Increment values and with your desired Alphabets but still make sure the new values are sequential and incremental.

SQL Server : primary key vs unique Index [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a question about designing a table for events.
Which one is better using a multi-column primary key, or using a sequential primary key with multi-column unique index?
Columns of this table are like this:
Generally in SQL Server, PRIMARY KEY is created as unique clustered index in the background.
So, it is good practice to keep clustered index key as:
Unique (avoids effort to add uniquifier to make the value unique)
Narrow (does not occupy lot of space)
Incremental (avoids fragmentation)
So, in your case , it is better to go for
Sequential Primary key & multi column unique index

How does using char(17) as primary key to store VIN numbers in a table with SQL Server affect performance? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am designing a database with a table to store vehicles and since the vehicle identification number is a 17 digit alphanumeric serial number my idea is to use it as the primary key, with a datatype of char(17).
Numerous other tables will then have the VIN as a foreign key.
A number of queries/searches will run with the VIN number as parameter since it's how we would like to track the vehicles as well as other data related to it.
The VIN number will never change, but I'm unsure if it would cause any serious performance degradation (or other complications I'm not aware of) since some queries will use joins and others not :/
By using the VIN as primary key I do not have to create a unique constraint / additional index - BUT it has to be char(17) a data type other than int for which primary keys are supposedly optimized...
What I'm also not 200% sure of is that every VIN number out there is the same length (very unlikely) but in that case how would using a varchar(17) affect the whole situation... if at all.
Thanks!
Just a personal opinion..
I always use int as a primary key. Primary key is in most cases always a clustered index. It's 4 bytes vs. 17 bytes and you can always put a non-clustered index on your VIN column. Keep things simple and clear. It's just my opinion though.
In my opinion, regarding performance, it indeed is not a good idea. It very much depends how many cars you will store in the database though. On the other hand, if your applications and queries use the VIN as parameter then it is the best option as the column is indexed and must be unique.
hope this helps
ps: akward seeing other people's suggestions on this topic!

how to design data base for this structure in sql server [duplicate]

This question already has answers here:
how to store multiple value in column in sql server
(4 answers)
Closed 8 years ago.
In my first table the columns are This is my master table
ID
Position
jobDescription
minExp
maxExp
LastDate
InterviewDate
Project
HiringManager
interviewer
Primaryskills
SecondorySkills
and second table contains the primary skills PrimarySkill table
ID
PrimarySkills
third Table contains the secondory skills
ID
SecondarySkills
fourth Table contains the Interviewer
the problem is that if a person have multiple skills and multiple interviewer for a job, Then how can insert the value from primary skill table, secondary skill table and interviewer table in Master table. i do not want to store value in comma separated
You usually do multiple links by an extra table that will just store the links, so that you can have multiple entries with the same MasterID:
table PrimarySkillsLinks:
MasterID
PrimarySkillID
You create a junction table which is a third table joining the two (and something that you need if you normalize the data model). An example model would use these tables:
Person (ID, Position, ...)
Skills (ID, Skill)
PrimarySkills (Person.ID, Skill.ID)
SecondarySkills (Person.ID, Skill.ID)
Although splitting skills into primary and secondary might not be needed, and it might be better to add a boolean to indicate if a skill is primary or secondary, like this:
PersonSkills (Person.ID, Skill.ID, IsPrimary (bool))

How to model a custom type in a relational database? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm fairly new to database design, but I understand the fundamentals. I'm creating a relational database and I'd like to do something similar to creating a reusable type or class. For example, let's say I have a Customer table and a Item table. Customer and Item are related by a standard 1-to-many relationship, so Item has a column called CustomerId.
I'd also like to have multiple "notes" for each Customer and each Item. In an normal OOP model, I'd just create a Note class and create instances of that whenever I needed. Of course, a relational database is different. I was thinking about having a Note table, and I'd like a 1-to-many relationship between Customer and Note, as well as Item and Note. The problem then is that the Note table will have to have a column for each other table that wishes to use this "type". (see example below)
I also thought that instead, I could create an intermediate table between Note and Customer/Item (or others). This would allow me to avoid having extra columns in Note for each table referencing it, so note could remain unchanged as I add more tables that require notes. I'm thinking that this is the better solution. (see example)
How is this sort of situation usually handled? Am I close to correct? I'd appreciate any advice on how to design my database to have the sort of functionality I've described above.
Yes, your concluding example is correct, and should be the way to go.
You model a "complex type" in relational databases by creating tables. You can consider the table as a class: in fact ORM solutions often map a class directly to a table. An instance of the custom type is a row in its table, and the instance can be referenced by the value of the primary key.
You can use your custom "complex type" for fields in other tables by using the same data type as the primary key of the "complex type", and enforcing the relationship with a foreign key constraint:
Let's build a complex type for "countries":
CREATE TABLE countries (
iso_code char(2) NOT NULL,
name varchar(100) NOT NULL,
population bigint
PRIMARY KEY (iso_code)
);
And let's add a couple of "country" instances:
INSERT INTO countries VALUES ('IE', 'Republic of Ireland', 4470700);
INSERT INTO countries VALUES ('US', 'United States of America', 310403000);
Now we're going to use our complex "countries" type in a "users" table:
CREATE TABLE users (
id int NOT NULL, -- primitive type
name varchar(50) NOT NULL, -- primitive type
age int, -- primitive type
country char(2), -- complex type
PRIMARY KEY (id),
FOREIGN KEY (country) REFERENCES countries (iso_code)
);
With the above model, we are guaranteed that the country field of the users table can only be a valid country, and nothing but a valid country.
In addition, using a junction table, as you suggested, is also a suitable approach to deal with that kind of polymorphic relationship. You may be interested in checking out the following Stack Overflow posts for some further reading on this topic:
How can you represent inheritance in a database?
Possible to do a MySQL foreign key to one of two possible tables?
I think you can best add a Note field to the Customer table and the Item table. In this note field (foreign key) you can store the id of the nota that belongs the the Customer / Item. To make sure you can attach multiple notes to a Customer or Item you could choose to add a Notes table and attach single Notes to the "Notes" table that you can attach to your Customer / Item table.

Resources