PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: add primary key
add primary key - phponwebsites.com
Showing posts with label add primary key. Show all posts

12 Feb 2014

Primary key in mysql

                         The primary key in mysql is used to identify the unique values in table. It don't allow duplicate entries in mysql table. The primary key column should be NOT NULL. It don't allow null values in mysql table. The mysql table can have only one primary key.


How to create primary key in mysql:


                         The mysql query for  create primary key is:

         Create table table_name (column_name datatype(length), primary key(column_name))

For example,
                        create table table1(name varchar(30), address varchar(100), primary key(name))

Where the column 'name' is primary key.


How to add primary key to existing table in mysql:


                       You created table without primary like this:
                
                   create table table1(name varchar(30),address varchar(100))
                     But you need to add primary key in order to remove duplicate entries in mysql. At the time you can add primary key to existing table in mysql. Following mysq query is used for add primary key.

                 Alter table table_name add primary key(column_name)

For example,
                 Alter table table1 add primary key(num)   


How to remove primary key in mysql:


                You created table with primary. But you need not primary key for particular table. Then how can remove primary key from table in mysql. The following mysql query is used to drop the primary key from table in mysql.

                    alter table table_name drop primary key

For example,
               
                      alter table table1 drop primary key 
          where, 

23 Jan 2014

Add primary key to existing column in mysql

                      Primary key is used to avoid duplicate entries in mysql table. A mysql table can have only one primary key.


Create table with primary key in mysql:

                                                The mysql query for create table with primary key is:

                       CREATE TABLE table_name(column_name datatype(length), 
                             PRIMARY KEY(column_name))

For example,
                       CREATE TABLE table1(No int(5) datatype(length), ,Game_name varchar(15),                                     Image varchar(15), PRIMARY KEY(No))

Now the table column 'No' is primary key in this table.

How to add primary key to existing column in mysql:

                         
                              You can add primary while creating new table. But can you add primary key to existing column in mysql? Yes, you can add primary key to existing column in mysql. The mysql query is:
                     
                       Alter table table_name add PRIMARY KEY(column_name)


Consider the example:
 table1 is name of this table.

changing column size in mysql at phponwebsites

When you click the structure of table in your database, it is look like this:


add primary key in mysql


                    There is no index defined.  Suppose you've to add primary key to column 'Game_name'. On that time you can add the primary key. The mysql query is:

                     Alter table table1 add PRIMARY KEY(Game_name)

Now you click the structure of table in mysql database, the index is defined as below:

add primary key existing column in mysql

                 Now your Game_name column  is changed from NULL into NOT NULL with primary key. Because the primary key should not be null. Only one primary key in a mysql table. A mysql table haven't more than one primary key.

Related Post:
Change column name in mysql
Change column size in mysql table
Delete column in mysql table
Add column after specific field in mysql table
Move columns in mysql table