primary-key

Creating a Primary Key on a temp table - When?

萝らか妹 提交于 2019-11-29 05:31:30
问题 I have a stored procedure that is working with a large amount of data. I have that data being inserted in to a temp table. The overall flow of events is something like CREATE #TempTable ( Col1 NUMERIC(18,0) NOT NULL, --This will not be an identity column. ,Col2 INT NOT NULL, ,Col3 BIGINT, ,Col4 VARCHAR(25) NOT NULL, --Etc... -- --Create primary key here? ) INSERT INTO #TempTable SELECT ... FROM MyTable WHERE ... INSERT INTO #TempTable SELECT ... FROM MyTable2 WHERE ... -- -- ...or create

How do I rename a primary key column in MySQL?

大兔子大兔子 提交于 2019-11-29 03:18:23
How do I rename a primary key column in MySQL? it's no different than altering any other column -- ALTER TABLE `pkey` CHANGE `keyfield` `keyfield2` INT( 11 ) NOT NULL AUTO_INCREMENT this changes the column keyfield in table pkey to be called keyfield2 -- you have to supply the definition afterwards, as usual. Maybe you have a foreign key constraint in place. You can disable those by SET foreign_key_constraints=0 but you have to remember to update the database afterwards. Leave off the PRIMARY KEY part of the alter statement. The primary key will be updated automatically. Possible a bad

What is the difference between Primary Key and unique key constraint?

陌路散爱 提交于 2019-11-29 03:06:22
What is the difference between Primary key And unique Key constraint ? What's the use of it?? Both are used to denote candidate keys for a table. You can only have one primary key for a table so would just need to pick one if you have multiple candidates. Either can be used in Foreign Key constraints. In SQL Server the Primary Key columns cannot be nullable. Columns used in Unique Key constraints can be. By default in SQL Server the Primary Key will become the clustered index if it is created on a heap but it is by no means mandatory that the PK and clustered index should be the same. A

Composite primary key

穿精又带淫゛_ 提交于 2019-11-29 03:05:33
I am working on the design of a database that will be used to store data that originates from a number of different sources. The instances I am storing are assigned unique IDs by the original sources. Each instance I store should contain information about the source it came from, along with the ID it was associated by this source. As an example, consider the following table that illustrates the problem: ---------------------------------------------------------------- | source_id | id_on_source | data | ---------------------------------------------------------------- | 1 | 17600 | ... | | 1 |

How to use MySQL index columns?

天大地大妈咪最大 提交于 2019-11-29 02:35:36
问题 When do you use each MySQL index type? PRIMARY - Primary key columns? UNIQUE - Foreign keys? INDEX - ?? For really large tables, do indexed columns improve performance? 回答1: Primary The primary key is - as the name suggests - the main key of a table and should be a column which is commonly used to select the rows of this table. The primary key is always a unique key (unique identifier). The primary key is not limited to one column, for example in reference tables (many-to-many) it often makes

Reasons not to use an auto-incrementing number for a primary key

巧了我就是萌 提交于 2019-11-29 02:23:09
问题 I'm currently working on someone else's database where the primary keys are generated via a lookup table which contains a list of table names and the last primary key used. A stored procedure increments this value and checks it is unique before returning it to the calling 'insert' SP. What are the benefits for using a method like this (or just generating a GUID) instead of just using the Identity/Auto-number? I'm not talking about primary keys that actually 'mean' something like ISBNs or

Compound primary key in Table type variable

血红的双手。 提交于 2019-11-29 02:06:41
问题 SQL Server 2008: DECLARE @MyTable TABLE( PersonID INT NOT NULL, Person2ID INT NOT NULL, Description NVARCHAR(100), CONSTRAINT PK PRIMARY KEY CLUSTERED (PersonID, Person2ID) ); Gives: Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'CONSTRAINT'. Is there any way to have compound Primary key in Table valued variables? 回答1: You can define a composite primary key like this: DECLARE @MyTable TABLE ( PersonID INT NOT NULL, Person2ID INT NOT NULL, Description NVARCHAR(100),

I need to auto_increment a field in MySQL that is not primary key

岁酱吖の 提交于 2019-11-29 01:04:51
Right now, I have a table whose primary key is an auto_increment field. However, I need to set the primary key as username , date (to ensure that there cannot be a duplicate username with a date). I need the auto_increment field, however, in order to make changes to row information (adding and deleting). What is normally done with this situation? Thanks! Just set a unique index on composite of (username, date). ALTER TABLE `table` ADD UNIQUE INDEX `name` (`username`, `date`); Alternatively, you can try to ALTER TABLE `table` DROP PRIMARY KEY, ADD PRIMARY KEY(`username`,`date`); and I think in

How to get generated keys from JDBC batch insert in Oracle?

旧街凉风 提交于 2019-11-28 23:44:14
I am inserting many records using JDBC batch inserts. Is there any way to get the generated key for each record? Can I use ps.getGeneratedKeys() with batch inserts? I am using oracle.jdbc.OracleDriver final String insert = "Insert into Student(RollNumber, Name, Age) values(StudentSEQ.nextval, ? , ?)"; final int BATCH_SIZE = 998; int count = 0; Connection con = null; PreparedStatement ps = null; try { con = getConnection(); ps = con.prepareStatement(insert); for (Student s : students) { ps.setString(1, s.getName()); ps.setInt(2, s.getAge()); ps.addBatch(); count++; if (count % BATCH_SIZE == 0)

Primary key and unique key in django

拟墨画扇 提交于 2019-11-28 21:25:36
I had a custom primary key that need to be set up on a particular data in a model. This was not enough, as an attempt to insert a duplicate number succeeded. So now when i replace primary_key=True to unique=True it works properly and rejects duplicate numbers!!. But according this document ( which uses fields ). primary_key=True implies null=False and unique=True. Which makes me confused as in why does it accept the value in the first place with having an inbuilt unique=True ? Thank you. Updated statement: personName = models.CharField(primary_key=True,max_length=20) mipadi Use an AutoField