primary-key

What data type is recommended for ID columns?

[亡魂溺海] 提交于 2019-12-04 13:10:52
问题 I realize this question is very likely to have been asked before, but I've searched around a little among questions on StackOverflow, and I didn't really find an answer to mine, so here goes. If you find a duplicate, please link to it. For some reason I prefer to use Guid s ( uniqueidentifier in MsSql) for my primary key fields, but I really don't know why this would be better. In many of tutorials I've walked myself through lately an automatically incremented int has been used. I can see pro

MySQL order by primary key

烈酒焚心 提交于 2019-12-04 11:47:04
问题 Some SQL servers allow for a generic statement such as ORDER BY PRIMARY KEY . I don't believe this works for MySQL, is there any such workaround that would allow for automated selects across multiple tables or does it require a lookup query to determine the primary key? The workaround I have been working on involves calling a SHOW COLUMNS FROM before running the query. Is there a more efficient way of doing this? Can MySQL determine the primary key of a table during the select process? Update

SQLAlchemy with multiple primary keys does not automatically set any

混江龙づ霸主 提交于 2019-12-04 11:38:10
I had a simple table: class test(Base): __tablename__ = 'test' id = Column(Integer, primary_key=True) title = Column(String) def __init__(self, title): self.title = title When using this table, id was set automatically. I want to add another field that is unique and efficient to search, so I added the field: id2 = Column(String, primary_key=True) And updated the constructor: def __init__(self, id2, title): self.id2 = id2 self.title = title Now, id is no longer automatically set, or rather I get the error: IntegrityError: (IntegrityError) test.id may not be NULL u'INSERT INTO test (id2, title)

Mysql Index Being Ignored

我只是一个虾纸丫 提交于 2019-12-04 10:05:29
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 | SIMPLE | content | const | PRIMARY | PRIMARY | 4 | const | 1 | | +----+-------------+---------+------

insert query with varchar as data type for primary key

白昼怎懂夜的黑 提交于 2019-12-04 06:24:24
问题 The program is not accepting the query given below- public class loginDaos { public void create(loginBean bean) { ConnectionPool c = ConnectionPool.getInstance(); c.initialize(); Connection con = c.getConnection(); try { String sql = "INSERT INTO login VALUES(?,?,?,?)"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, bean.getCode()); pstmt.setString(2, bean.getUserid()); pstmt.setString(3, bean.getPassword()); pstmt.setString(4, bean.getPosition()); pstmt.executeUpdate

Room composite Primary Key link to Foreign Key

被刻印的时光 ゝ 提交于 2019-12-04 05:51:08
I am implementing an android application with Room database and have a small question about relationships in this DB. I have two tables: @Entity(tableName = "foods", primaryKeys = {"id", "language_id"}, indices = {@Index(value = {"id", "language_id"}, unique = true)}, inheritSuperIndices = true) public class Food { @NonNull @ColumnInfo(name = "id") private String mId; @NonNull @ColumnInfo(name = "language_id") private String mLanguageId; } @Entity(tableName = "ingredients", primaryKeys = {"id", "language_id"}, indices = {@Index(value = {"id", "language_id"}, unique = true), @Index(value = {

Primary and foreign keys in xml schema

 ̄綄美尐妖づ 提交于 2019-12-04 05:35:53
I am trying to insert primary and foreign keys in XSD schema file below :- Primary key is StudentID and Foreign Keys are courseID, AddressID, GradeID. <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Student"> <xs:complexType> <xs:sequence> <xs:element name="Title" type="xs:string"/> <xs:element name="FirstName" type="xs:string"/> <xs:element name="LastName" type="xs:string"/> <xs:element name="Dateborn" type="xs:date"/> <xs:element name="Gender" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> However above code is

Get back the primary key value after insertion to db2 table by KeyHolder

穿精又带淫゛_ 提交于 2019-12-04 05:35:16
问题 Good day, I have a table in db2, where the primary is_autoincrement is set to Yes . Means that everytime insert data to this table, no need to pass in the primary key value, because it will auto generate. However, I need to get back the primary key value after insertion. The code is something as follow: public integer insertRecord() { //insertion sql code here KeyHolder keyHolder = new GeneratedKeyHolder(); PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory(

On SQL INSERT can I use the identity column for part of the insert data in another column at the same time?

风格不统一 提交于 2019-12-04 05:26:46
问题 CREATE TABLE Table1 : Id int IDENTITY(1,1), PK_Column1 nvarchar(50) Primary Key. INSERT INTO Table1 (PK_Column1) VALUES ('Name'+Id) Result: Id PK_Column1 1 Name1 Is this possible? Or do I need to manage the Id column myself for this to work? 回答1: From the documentation: After an INSERT, SELECT INTO, or bulk copy statement completes, @@IDENTITY contains the last identity value generated by the statement. This applies to all the other identity checkers. You should probably write a little SP to

How to change value of primary key and update foreign key in the same time

99封情书 提交于 2019-12-04 05:10:16
I have a record in table with wrong primary key. I want change it to correct value, but this value is used in many other tables. Is there any simple way to update primary key and foreign key at the same tim? If the foreign keys are set to cascade changes then the value should change automatically. Make sure that your foreign key relationships have ON UPDATE CASCADE specified, and the foreign key will automatically update to match the primary key. From Books Online: http://msdn.microsoft.com/en-us/library/ms174123%28v=SQL.90%29.aspx ON UPDATE {CASCADE | NO ACTION | SET DEFAULT | SET NULL}