MySQL Primary Key
snippet in sql

mysql change primary key

user9992

ALTER TABLE tableName MODIFY COLUMN id INT; /* First you should drop auto increment */
ALTER TABLE tableName DROP PRIMARY KEY; /* Dop primary key */
ALTER TABLE tableName ADD PRIMARY KEY (new_id); /* Set primary key to the new column */
ALTER TABLE tableName MODIFY COLUMN new_id INT AUTO_INCREMENT; /* Set auto increment to the new primary key */

create a table in sql with primary key

user503


CREATE TABLE table_name
(
   Column1 datatype,		// column1 and datatype
   Column2 datatype,		// column2 and datatype
   Column3 datatype,		// column3 and datatype
  
   PRIMARY KEY (Column1);	// column1 is the primary key
); 

mysql drop key

user5666

ALTER TABLE tbl_quiz_attempt_master
  DROP INDEX `PRIMARY`;

MySQL Primary Key

user8775

Typically, you define the primary key for a table in the CREATE TABLE statement.

If the primary key has one column, you can use the PRIMARY KEY constraint as a column constraint:

CREATE TABLE table_name(
    primary_key_column datatype PRIMARY KEY,
    ...
);

sql primary key

user8513

-- NOTE: this is for SQL-Oracle specifically

-- example:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = 'CUSTOMERS' 
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner;

-- syntax:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = '<table-name>' -- Replace <table-name> with your table-name
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner;

alter table add key sql server

user7655

ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);