简体   繁体   中英

How should I go about using strings for primary keys in mysql

I have a table of people in mysql that currently uses auto-incrementing integers as primary keys. This data is interfaced with c#. The people who will be interacting with the data in the c# program won't have access to the primary key integers. If I change the primary key datatype to a varchar of the name then that solves the problem of deleting people and editing people. However doing so raises the issue of creating a new person with the same primary key (name) as an existing person. I could use a compound primary key that uses first and last name. However I don't know how to do that. My knowledge of mysql is limited and I know a good bit of c#. Any suggestions for how to go about this are what I am looking for.

Note: I would prefer a graphical method in MySQL workbench but I am not opposed to writing an sql query to achieve the same result.

I do not have enough reputation that's why i cannot comment under your query. What i have got from your query, you want to apply primary key on Name but the name will be different. So while inserting the new record with same name, it will display error, the corresponding entry already exist.. If your query is like this then I will give you answer in next dialogue and if not then please elaborate your query for more info.... And also send me all column name for particular table where you want to use primary key concept ....

Are you 100% Sure that you will never have 2 people with the same first name and last name? Otherwise I'd say, tell the people their primary key integer and make them remember it, or implement a username system where they pick their name. But to answer your question:

string SQLCommand =  "INSERT INTO table_name (colFullName, column2, column3) VALUES (" + strFirstName + strLastName + ", value2, value3)";

This will insert it into your table where their full name will be in one field, which I think is what you are asking.

据我说,告诉大家他们的主键整数,并要求他们记住它!

string SQLCommand =  "INSERT INTO table_name (colFullName, column2, column3) VALUES (" + strFirstName + strLastName + ", value2, value3)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM