primary-key

SQL Server Datetime vs Int key performance

假如想象 提交于 2019-11-29 11:49:02
问题 For you database design/performance gurus out there. If you have a database that is intended to track financial data for fiscal year periods, is it better/more performance/more clear to do daterange type searches like PaymentDate Between X and Y or is it better to keep a int-key based table with fiscal year periods defined in it and tag the payment table with the payment date and that key, so the where clause is where FiscalPeriodID = X? I'm sure for smaller datasets it doesn't matter, but

Primary key Ascending vs Descending

我是研究僧i 提交于 2019-11-29 11:16:57
问题 In Sql Server, I have a table with an Identity primary key. Often I want the latest few new records, so I grab the Top n sorted by descending the primary key. Should I define the Primary Key index as Descending, or does it make no difference? i.e. if they are in Ascending order, then can sql just as efficiently work backwards? 回答1: From a purely querying standpoint, it makes no difference whether your key is descending or ascending if you want to pull N most recent or N oldest records: The

Native primary key or auto generated one?

廉价感情. 提交于 2019-11-29 11:03:26
As a rule is it better to use native primary keys (ie existing columns or combination of columns) or set your primary key to an auto generating row of integers? EDIT: It has been pointed out to me that this very similar to this question . The consensus here is to use surrogate keys, which was my natural inclination, but my boss told me I should also use natural keys where possible. His advice may be best for this particular application, as Name in row uniquely identifies it and we have a need to maintain the ability to view old data, thus any changes to the name/rule is going to mean new

Must database primary keys be integers?

别说谁变了你拦得住时间么 提交于 2019-11-29 09:45:34
I always see MySQL database primary keys as integers. Is that because primary keys must be integers, or because of ease of use when setting auto_increment on the column? I am wondering just in case I want my primary key to be a varchar in the future. You can use varchar as well as long as you make sure that each one is unique . This however isn't ideal (see article link below for more info). What you are looking for is called natural key but a primary key with auto-increment and handled by the RDBMS is called surrogate key which is preferred way. Therefore you need to have it to be integer .

Why can I create a table with PRIMARY KEY on a nullable column?

蓝咒 提交于 2019-11-29 09:05:54
The following code creates a table without raising any errors: CREATE TABLE test( ID INTEGER NULL, CONSTRAINT PK_test PRIMARY KEY(ID) ) Note that I cannot insert a NULL, as expected: INSERT INTO test VALUES(1),(NULL) ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null). ********** Error ********** ERROR: null value in column "id" violates not-null constraint SQL state: 23502 Detail: Failing row contains (null). Why can I create a table with a self-contradictory definition? ID column is explicitly declared as NULLable, and it is implicitly not

How to to create unique random integer ID for primary key for table?

白昼怎懂夜的黑 提交于 2019-11-29 08:28:26
问题 I was wondering if anybody knew a good way to create a unique random integer id for a primary key for a table. I'm using MySQL. The value has to be integer. 回答1: If your're open to suggestions and you can implement it, use UUIDs. MySQL's UUID() function will return a 36 chars value which can be used for ID . If you want to use integer, still, I think you need to create a function getRandID() that you will use in the INSERT statement. This function needs to use random + check of existing ids

MySQL non primary foreign key

人走茶凉 提交于 2019-11-29 07:41:51
I'm a bit of a newbie and I can't get my head around primary keys as foreign keys. To me, foreign keys are meant to connect two rows of a table together. Therefore, it would make logical sense to use the, for example, username of the user table as a foreign key in the picture table. This means that the picture in that row belongs to the specified user. However, it appears that general practice favors using meaningless numbers as primary IDs. Furthermore the foreign key must/should refer to the primary key. What if I don't know the primary key, but I know another unique column, in this case

What's the point of adding NOT NULL to primary key field in MySQL?

戏子无情 提交于 2019-11-29 07:40:15
问题 What's the point of adding NOT NULL to a primary key field? Primary key is already not null + unique . Here is an example: CREATE TABLE student ( id int(11) AUTO_INCREMENT NOT NULL, name varchar(255), PRIMARY KEY(id) ) Why not to define it like this instead: CREATE TABLE student ( id int(11) AUTO_INCREMENT, name varchar(255), PRIMARY KEY(id) ) 回答1: They are the same. Primary key got NOT NULL automatically . 回答2: You are asking, why do people bother adding the NOT NULL when it is unnecessary?

Mysql - how to set auto-increment to start from zero

核能气质少年 提交于 2019-11-29 07:35:52
I just want my mysql table id(primary key) to start from 0.. As I have seen, I used ALTER TABLE yourtable AUTO_INCREMENT =0 but it starts from 1..What is that I need to do? Edit1 I also emptied my table with truncate option Hanky Panky SET [GLOBAL|SESSION] sql_mode='NO_AUTO_VALUE_ON_ZERO' NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 0 so that only NULL generates the next sequence number. This mode can be useful if 0 has

performance penalty of strings as primary keys?

最后都变了- 提交于 2019-11-29 07:19:16
问题 What would be the performance penalty of using strings as primary keys instead of bigints etc.? String comparison is much more expensive than integer comparison, but on the other hand I can imagine that internally a DBMS will compute hash keys to reduce the penalty. An application that I work on uses strings as primary keys in several tables (MySQL). It is not trivial to change this, and I'd like to know what can be gained performance wise to justify the work. 回答1: on the other hand I can