primary-key

How to automatically create the primary key that has the following series A001000001 … A001xxxxxx in SQL?

随声附和 提交于 2019-12-06 10:53:14
问题 I want to create a primary key(auto-increment) which is start with A001000001 . Here A001 would be constant and 000001 would be iterate. I want rows like this: How can I do it using SQL query? 回答1: For SQL Server (you didn't clearly specify which RDBMS you're using), my suggestion would be: add a INT IDENTITY column to your table - and quite frankly, I would make that column the primary key add a computed column to your table that does this "prefixing" Something like: CREATE TABLE dbo

IntegrityError: ERROR: null value in column “user_id” violates not-null constraint

喜夏-厌秋 提交于 2019-12-06 07:50:52
Using: postgres (PostgreSQL) 9.4.5 I just migrated a sqlite3 db onto a postgresql db. For some reason since this migration, when I try to create a user, an error regarding the user_id (which is a primary key) is being raised. This was not an issue before with sqlite3 . I have spent time looking through the docs and stack questions, but remain confused. Inside api.create_user() : api.create_user(username ='lola ', firstname ='cats ', lastname ='lcatk', email='cags@falc.com') sqlalchemy db Model: class User(Base): __tablename__ = 'users' #user_id = Column(Integer, primary_key=True) #changed to:

Sql Server: uniqueidentifier plus integer compound PK … what type of index to use?

不问归期 提交于 2019-12-06 07:41:48
I have a junction table in my SQL Server 2005 database that consist of two columns: object_id (uniqueidentifier) property_id (integer) These values together make a compound primary key. What's the best way to create this PK index for SELECT performance? If the columns were two integers, I would just use a compound clustered index (the default). However, I've heard bad things about clustered indexes when uniqueidentifiers are involved. Anyone have experience with this situation? Yes, GUID's are really bad for clustered indexes, since the GUIDs is by design very random and thus leads to massive

Primary Key Identity Value Increments On Unique Key Constraint Violation

孤街醉人 提交于 2019-12-06 07:10:36
问题 I have a SqlServer 2008 table which has a Primary Key (IsIdentity=Yes) and three other fields that make up a Unique Key constraint. In addition I have a store procedure that inserts a record into the table and I call the sproc via C# using a SqlConnection object. The C# sproc call works fine, however I have noticed interesting results when the C# sproc call violates the Unique Key constraint.... When the sproc call violates the Unique Key constraint, a SqlException is thrown - which is no

Reset primary key

徘徊边缘 提交于 2019-12-06 06:29:40
I've tried to find an answer to do this online but apparently it can't be done (I mean at the app level, not database). I have a need to clear out my dataset completely and reset the primary key at the same time. Any ideas? Alternatively, one hack i can use is to reinitialize the dataset but that doesn't seem possible as well since the dataset is shared between different classes in the app (I'm creating the shared dataset in Program.cs). Thanks Farooq Update: ok i tried this: MyDataSet sharedDS = new MyDataSet(); . . . CleanDS() { MyDataSet referenceDS = new MyDataSet(); sharedDS.Table1.Reset(

How do I create a table with a two primary keys of two foreign keys?

心不动则不痛 提交于 2019-12-06 06:18:57
create table Machine ( Machine_ID int primary key, Machine_Name varchar(30) Machine_Title varchar(30) ) create table Part ( Part_ID int primary key, Part_Name varchar(30), Part_Description varchar(30) ) //How do I make this table below? create table Machine-Part ( Machine_ID int foreign key references (Machine.Machine_ID), Part_ID int foreign key references (Part.Part_ID) Factory_Note varchar(30); ) Mysql complains there is a problem with syntax? Desired Result: have Table 'Machine-Part' use 'Machine_ID' & 'Part_ID' as both primary keys (which are both foreign keys). If you declare the

Foreign Keys Slowing down Delete

孤街醉人 提交于 2019-12-06 05:20:59
问题 I have a table X which has an auto-incremented ID column as its primary key. I have other tables A, B, C and D that compliment the info in table X. Each of these have to contain a column that references the ID from table X. I have done that and in my code (Java) and I have a way of returning the ID of each entry to table X and using that when inserting to the other tables. All is working well. Now, I've been advised to assign those ID columns on tables A, B, C and D as FOREIGN KEYS because

Should I use Oracle's sys_guid() to generate guids?

做~自己de王妃 提交于 2019-12-06 05:13:32
问题 I have some inherited code that calls SELECT SYS_GUID() FROM DUAL each time an entity is created. This means that for each insertion there are two calls to Oracle, one to get the Guid , and another to insert the data. I suppose that there may be a good reason for this, for example - Oracle's Guids may be optimized for high-volume insertions by being sequential and thus they maybe are trying to avoid excessive index tree re-balancing. Is there a reason to use SYS_GUID as opposed to building

Sequences with composite primary key

跟風遠走 提交于 2019-12-06 05:05:48
问题 Using PostgreSQL, how do I manage a sequence (auto incremented Integer id) for the following scenario- Table named 'businesses' which has 'id' and some more columns. Table named 'users' which has: a composite primary key, composed of a 'business_id', and an 'id', and have the 'id' reset it's counting for each 'business_id' when I insert now rows. a simple column 'name'. something like having a separate sequence per business. Example When I run the following queries: insert into users

Mysql Index Being Ignored

时间秒杀一切 提交于 2019-12-06 04:10:16
问题 EXPLAIN SELECT * FROM content_link link STRAIGHT_JOIN content ON link.content_id = content.id WHERE link.content_id = 1 LIMIT 10; +----+-------------+---------+-------+---------------+------------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------+-------+---------------+------------+---------+-------+------+-------+ | 1 | SIMPLE | link | ref | content_id | content_id | 4 | const | 1 | | | 1