Is ID column required in SQL?

If you really do have some pre-existing column in your data set that already does uniquely identify your row – then no, there’s no need for an extra ID column. The primary key however must be unique (in ALL circumstances) and cannot be empty (must be NOT NULL). In my 20+ years of experience in … Read more

SQL Server: drop table primary key, without knowing its name

You’ll have to use dynamic SQL for that, since ALTER TABLE does not accept variables or subqueries. CREATE TABLE PKTest ( ID INT PRIMARY KEY ) ; DECLARE @SQL VARCHAR(4000) SET @SQL = ‘ALTER TABLE PKTEST DROP CONSTRAINT |ConstraintName| ‘ SET @SQL = REPLACE(@SQL, ‘|ConstraintName|’, ( SELECT name FROM sysobjects WHERE xtype=”PK” AND parent_obj = … Read more

Generating a non-sequential ID/PK for a Django Model

There is built-in Django way to achieve what you want. Add a field to the model of “custom page” with primary_key=True and default= name of key generation function, like this: class CustomPage(models.Model): … mykey = models.CharField(max_length=6, primary_key=True, default=pkgen) … Now, for every model instance page, page.pk becomes an alias for page.mykey, which is being auto-assigned … Read more

How to create foreign key that is also a primary key in MySQL?

Add FOREIGN KEY (sale_id) REFERENCES Sale(sale_id) to each foreign table: CREATE TABLE Sale( sale_id CHAR(40), PRIMARY KEY(sale_id), discount DOUBLE, type VARCHAR(255), price DOUBLE ) ENGINE=INNODB; CREATE TABLE Normal_Sale( sale_id CHAR(40), PRIMARY KEY(sale_id), FOREIGN KEY (sale_id) REFERENCES Sale(sale_id) ) ENGINE=INNODB; CREATE TABLE Special_Sale( sale_id CHAR(40), PRIMARY KEY(sale_id), FOREIGN KEY (sale_id) REFERENCES Sale(sale_id) ) ENGINE=INNODB; Just make … Read more

Django + PostgreSQL: How to reset primary key?

In your app directory try this: python manage.py help sqlsequencereset Pipe it into psql like this to actually run the reset: python manage.py sqlsequencereset myapp1 myapp2 | psql Edit: here’s an example of the output from this command on one of my tables: BEGIN; SELECT setval(‘”project_row_id_seq”‘, coalesce(max(“id”), 1), max(“id”) IS NOT null) FROM “project_row”; COMMIT;

Can a table have two foreign keys?

create table Table1 ( id varchar(2), name varchar(2), PRIMARY KEY (id) ) Create table Table1_Addr ( addid varchar(2), Address varchar(2), PRIMARY KEY (addid) ) Create table Table1_sal ( salid varchar(2),`enter code here` addid varchar(2), id varchar(2), PRIMARY KEY (salid), index(addid), index(id), FOREIGN KEY (addid) REFERENCES Table1_Addr(addid), FOREIGN KEY (id) REFERENCES Table1(id) )

Why is negative id or zero considered a bad practice?

To be clear, this question and answer are about using negative numbers for surrogate keys, not for natural keys. As far as I know, there are three reasons for considering it to be a bad practice. It violates the principle of least surprise. Some people assume all ID numbers are non-negative. Some people use negative … Read more

tech