Monday, October 4, 2010

MYSQL - TABLE - ALTERS

Question 1 : Change the Column Name in a MYSQL TABLE ?
Command :
ALTER TABLE 'TABLE NAME' CHANGE 'Column Need to Change'
'New Column Name' varchar(50) NOT NULL;
Example :
 ALTER TABLE attribute_variable_map CHANGE title cluster_title varchar(100);

Question 2 : ADD a Existing Column in the Primary Key ?
Ans :
If u have Column already set a Primary key, if you want to Add another Column 
as Primary Key [ Call it as Primary Key ]
Command :
ALTER TABLE table1 DROP INDEX `PRIMARY`, ADD PRIMARY KEY (column1, column2);
Question : How to take the Backup of a Table in MY-SQL Database?
Syntax :
mysqldump -uusername -ppassword databasename tableName > fileName.sql
Example:
mysqldump -uroot -ppublic configaudit Attribute_Variable_Map > attributeTable.sql;
Here :
root = username;
public = password;
databasename = configaudit;
tablename = Attribute_Variable_Map;
filename = attributeTable;

Question : How to Dump/Restore this Table Backup In Another Database ?
Answer :
Step 1 : Go to the Directory where u copied the filename.sql & Execute the Following Command:
Synatx : mysql -uusername -ppassword databaseName < style="font-weight:bold;">Example :
mysql -uroot -ppublic configaudit <>

Question : How to Add New Column to Existing table?
Answer 1 : Alter table "tablename" add "column name" "datatype";
Example : Alter table myTable add title varchar(60);

Question : If column want's to be the first column in table ?
Answer 2 : Alter table add "column name" "datatype" FIRST;
Question : If column want's to be the next column after some column ?
Answer 3 : Alter table add "column name" "datatype" After "another Column name";
Question: change value on particular Row in MY-SQL ?
Answer : UPDATE set column = "value" where "CONDITION"
Example : update attribute_variable_map set title="BIO Slowpath CPU" where id=45;
Question : How to Rename/change the Table name in MY-SQL?
Answer : RENAME TABLE "Table Name " To "Table New Name"
Question : How to Remove Duplicate Entries in a MY-SQL Table?
Command :  DELETE FROM bad_table WHERE id=1;
Observation : The Above command will delete all the record in table where id = 1;

Question : Suppose i have two records with Duplicate entries, i want to delete only 
one record?
Example Scenario
21 | KPI: Failed Connection Attempts (FCA) | 1 |
21 | KPI: Failed Connection Attempts (FCA) | 1 |

Command : DELETE FROM bad_table WHERE id=1 LIMIT 1;
Observation : The above Command will delete Only one Row.

How to create a new table with the use of structure of Old table?
Command : CREATE TABLE om_data_nonDailies LIKE om_data;
Question : How to redirect the output of mysql command to a file ?
Ans : Select * into "outputfile.txt" from table_name where condition;
Question : How to limit the select query result set ?
Answer : for example if select * from table_name results the more number
of lines, you can limit the lines by specifying the limit.
Command : Select * from table_name LIMIT 1;
Question : How to delete a column from an table ?
Command : ALTER TABLE table_name DROP column_name;

No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...