primary-key

How to update primary key?

佐手、 提交于 2019-12-25 01:22:01
问题 I am writing a script that has to update some rows without changing the contents of another script that creates a few tables. Another condition is that you cannot alter or drop constraints. Contents of Create table script: CREATE TABLE TRUCK( REGNUM VARCHAR(10) NOT NULL, CAPACITY DECIMAL(7) NOT NULL, WEIGHT DECIMAL(5) NOT NULL, STATUS VARCHAR(10) NOT NULL, CONSTRAINT TRUCK_PKEY PRIMARY KEY(REGNUM), CONSTRAINT TRUCK_STATUS CHECK (STATUS IN ('AVAILABLE', 'MAINTAINED', 'USED')); and there are

SQL Create Table that Auto-Increments one column based on another column

大城市里の小女人 提交于 2019-12-24 21:50:28
问题 I'm still a beginner with SQL so please bear with me. I need to create a table that stores information from a form. Each form has an ID associated with it that has two parts. The first part is MMYY and the second part is a 5 digit auto-incrementing integer. An example ID is 0714-00001. And every time the month changes, the 5 digit numbering should start over. So, I want the columns to look something like this: Order_No. | Order_ID ---------------------------- 0714 | 0001 0714 | 0002 0714 |

change primary key value

落花浮王杯 提交于 2019-12-24 20:24:03
问题 I have 10 tables. Each table referenced by foreign keys of other 5 tables. I need to change the primary key value of those 10 tables. Is there any way to change it so that it will change automatically all the foreign keys? I am using sql server 2008 and have the management studio. 回答1: You need to set ON UPDATE CASCADE for those foreign keys: ALTER TABLE bar ADD CONSTRAINT FK_foo_bar FOREIGN KEY (fooid) REFERENCES foo(id) ON UPDATE CASCADE Then you simply update the FKs and referring fields

Structuring database relationships for tracking different variations of app settings

放肆的年华 提交于 2019-12-24 19:03:44
问题 An app I'm currently designing allows users to create custom competitive leagues (think of it kind of like fantasy sports) and each user can join different leagues and each league consists of multiple rounds where the users (hereafter referred to as Players) will compete and can earn points for different criteria/accomplishments established for each league. Here's some key info to note: Points are accrued across all the rounds during a league/season The custom point criteria/weight settings

Make one ID with auto_increment depending on another ID - possible?

痴心易碎 提交于 2019-12-24 18:34:59
问题 I want to make a small ticket-system for a project server which has several projects. So far the TicketID will be counted globally, i.e. there is a project A and a ticket with TicketID 1 and another project B and the ticket for this project will get TicketID 2 - like this: (TicketID, ProjectID) (1, 1) (2, 1) (3, 1) (4, 2) (5, 2) (6, 3) But I think it would be better to count the TicketID depending on the ProjectID such as: (TicketID, ProjectID) (1, 1) (2, 1) (3, 1) (1, 2) (2, 2) (1, 3) Here

How to get a Primary ID before or at the same time it gets created?

China☆狼群 提交于 2019-12-24 17:19:44
问题 What I am doing: (wp_posts has ID as primary key and it auto-increase) mysql_query("INSERT INTO wp_posts (post_content, post_title) VALUES ('$addEP', '$title')"); $rs = mysql_query("SELECT ID FROM wp_posts WHERE post_title='$title'"); $add_row = mysql_fetch_array($rs); $add_postid = $add_row['ID']; mysql_query("INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES ('$add_postid', '$cat_id')"); So basically, what I am doing is; I insert content into wp_posts and then run

Composite primary key limit?

淺唱寂寞╮ 提交于 2019-12-24 17:19:35
问题 Is it OK if I create a composite primary key from 4 columns? Like: PRIMARY KEY(name, slug, type, parent) So there should not be more than one row with the same name, slug, type and parent. Are there too many columns? Will it affect performance? I'm using sqlite btw. 回答1: It's usually recommended to have an ID field that is unique on its own. Comparing INTEGER values is faster than comparing strings, so your composite key will affect performance negatively. Adding a column with the following

Merging two tables, duplicate keys

て烟熏妆下的殇ゞ 提交于 2019-12-24 16:23:22
问题 *I'm very new to this, I might (probably will?) be talking some jibberish now and then, feel free to correct me ;) I'm playing around with different primary key/index strategies and was wondering about the following. In many cases the choice for a GUID as the primary key is providing the record with unique identification across any database. Now there are some performance issues with using GUIDs as primary key (and default behaviour of using it as the clustered index). There are a few

mysql not unique auto increment, primary key two fields

假如想象 提交于 2019-12-24 05:43:34
问题 I want to create a table structure like this in MySQL: id | area | name ----+----------+------------ 1 | name-1 | test | | 1 | name-1 | value2 | | 2 | name-2 | test-value | | 3 | name-3 | test | | 3 | name-3 | test ie. the primary key will be: primary_key( id, area ) and the id will be auto_increment , but i only want the id to increment for every new unique area is this possible? 回答1: What you want is not possible. You want id and area to be the primary key but in your example they are not

SQL Server how to maintain GUID across tables in same DB

余生颓废 提交于 2019-12-24 00:28:24
问题 I want to create a DB , where each table's PK will be GUID and which will be unique across the DB, Example: my DB name is 'LOCATION'. And I have 3 table as 'CITY' , 'STATE' and 'COUNTRY'. I want that all the 3 tables have same PK field as GUID ,and that value will be unique across DB. How to do this in SQL Server, any idea? I have never used SQL Server before, so it will be helpful if briefly explained. 回答1: create table CITY ( ID uniqueidentifier not null primary key default newid(), . . . )