mysql

  1. CREATE DATABASE database_name;  //CREATE DB
  2. SHOW DATABASES;   // SHOWS ALL DB'S
  3. USE database_name;  //FIND DB AND SHOWS TABLES OF THAT DATABASE 
  4. DROP DATABASE database_name;  //DELETES DB
  5. CREATE TABLE table_name (column_name column_type...);
     
    Example:
    Here, we will create a table named "cus_tbl" in the database "customers".
  • CREATE TABLE cus_tbl(  
  •    cus_id INT NOT NULL AUTO_INCREMENT,  
  •    cus_firstname VARCHAR(100) NOT NULL,  
  •    cus_surname VARCHAR(100) NOT NULL,  
  •    PRIMARY KEY ( cus_id )  
  • );  
  • Note:
    • Here, NOT NULL is a field attribute and it is used because we don't want this field to be NULL. If you will try to create a record with NULL value, then MySQL will raise an error.
    • The field attribute AUTO_INCREMENT specifies MySQL to go ahead and add the next available number to the id field.PRIMARY KEY is used to define a column as primary key. You can use multiple columns separated by comma to define a primary key.
    1. SHOW tables; 
    2. DESCRIBE cus_tbl; //displays structure of table ie., 
    3. MySQL ALTER statement is used when you want to change the name of your table or any table field. It is also used to add or delete an existing column in a table.
      The ALTER statement is always used with "ADD", "DROP" and "MODIFY" commands according to the situation.
    4. ADD a column in the table

      • ALTER TABLE table_name  
      • ADD new_column_name column_definition  
      • FIRST | AFTER column_name ]; 
        example:
      • ALTER TABLE cus_tbl  
      • ADD cus_age varchar(40) NOT NULL;  
    5. Add multiple columns in table

      Add multiple columns in the table

    •  ALTER TABLE table_name  
    •  ADD new_column_name column_definition  
    •  [ FIRST | AFTER column_name ],  
    • ADD new_column_name column_definition  
    • FIRST | AFTER column_name ],  
    •   ...  

    • example
    • ALTER TABLE cus_tbl  
    • ADD cus_address varchar(100) NOT NULL  
    • AFTER cus_surname,  
    • ADD cus_salary int(100) NOT NULL  
    • AFTER cus_age ;  
    • MODIFY column in the table

    • ALTER TABLE table_name  
    • MODIFY column_name column_definition  
    • FIRST | AFTER column_name ]; 

    DROP column in table

    Syntax:
    1. ALTER TABLE table_name  
    2. DROP COLUMN column_name;  
    Let's take an example to drop the column name "cus_address" from the table "cus_tbl".
    Use the following query to do this:
    1. ALTER TABLE cus_tbl  
    2. DROP COLUMN cus_address;  

    ) RENAME column in table

    Syntax:
    1. ALTER TABLE table_name  
    2. CHANGE COLUMN old_name new_name   
    3. column_definition  
    4. FIRST | AFTER column_name ]  
    Example:
    In this example, we will change the column name "cus_surname" to "cus_title".
    Use the following query to do this:
    1.  ALTER TABLE  cus_tbl  
    2. CHANGE COLUMN cus_surname cus_title  
    3. varchar(20) NOT NULL;  


     RENAME table

    Syntax:
    1. ALTER TABLE table_name  
    2. RENAME TO new_table_name;  
    Example:
    In this example, the table name cus_tbl is renamed as cus_table.
    1. ALTER TABLE cus_tbl  
    2. RENAME TO cus_table;  


    TRUNCATE TABLE  table_name; 
    1. DROP TABLE  table_name;  



































              Comments