MySQL (operation data table)

MySQL create a data table

Create a MySQL data tables need the following information:

  • Table Name
  • Table Field Name
  • The definition of each table field

grammar

CREATE TABLE IF NOT EXISTS 表名(
   `runoob_id` INT UNSIGNED AUTO_INCREMENT, `runoob_title` VARCHAR(100) NOT NULL, `runoob_author` VARCHAR(40) NOT NULL, `submission_date` DATE, PRIMARY KEY ( `runoob_id` ) )ENGINE=InnoDB DEFAULT CHARSET=utf8;

Living Example:

  • If you do not want field is  NULL  can set field properties for  the NOT NULL , when the operation of the database if you enter the field data is NULL  , it will error.
  • It is defined as the increment AUTO_INCREMENT attribute, generally used for the primary key, the value added by 1 automatically.
  • PRIMARY KEY keyword is used to define a column as the primary key. You can use multiple columns to define a primary key columns separated by commas.
  • ENGINE setting storage engine, CHARSET set encoding.
  • MySQL Delete Data Sheet

    Be very careful when deleting data in MySQL tables are very easy to operate, but you then delete the operating table, because the delete command all data will be lost.

    grammar

    The following MySQL data tables for deletion general syntax:

    DROP TABLE 表名 ;
  • MySQL table to delete several situations:

    1, drop the Table table_name: delete all the data tables and table structure immediately free up disk space, either Innodb and MyISAM;

    Instance, remove the student table:

    drop table student;

    2, TRUNCATE table_name the Table: delete all the data table, reserved table structure, immediately free up disk space, either Innodb and MyISAM;

    Instance, remove the student table:

    truncate table student;

    3, the Delete from table_name: delete all the data table, the table structure unchanged, to immediately free up disk space MyISAM, InnoDB does not release disk space;

    Instance, remove the student table:

    delete from student;

    4, the Delete from the WHERE table_name xxx: Conditional deletion, change table structure, whether or innodb MyISAM will not free up disk space;

    Instance, remove the student table name as "John Doe" data:

    delete from student where T_name = "张三";

    5, after the delete operation,  optimize table table_name will immediately free up disk space, whether it is innodb or myisam;

    Instance, remove the student table name as "John Doe" data:

    deletefrom student where T_name ="张三";  

    Example, the release of table space student table:

    optimize table student;

    6, after delete from the table, although not free up disk space, but the next time the data is inserted, you can still use this space.

  • MySQL inserting data

    MySQL table using the  INSERT INTO  SQL statements to insert the data.

  • INSERT a plurality of data

    INSERT INTO table_name  (field1, field2,...fieldN) VALUES (valueA1,valueA2,...valueAN),(valueB1,valueB2,...valueBN),(valueC1,valueC2,...valueCN)......;
  • When adding data can be specified columns added.

    If all columns should add data columns may not add data requirements:

    mysql> INSERT INTO runoob_tbl
        -> VALUES
        -> (0, "JAVA 教程", "RUNOOB.COM", '2016-05-06');

    The first column if no primary key increment (PRINARY KEY AUTO_INCREMENT), then add the first column of data easier to confusion, to continue to look at the data look-up table.

    If the primary key is added through the increment (PRINARY KEY AUTO_INCREMENT) the first column of the increase in the data, it can be written as 0 or null, so add the data can be incremented, so you can add all the data, but do not specifically prescribed that a few columns to add data.

  • MySQL DELETE statement

    You can use the SQL DELETE FROM command to delete records MySQL data tables.

  • grammar

    The following is the SQL DELETE statement to delete data from a MySQL data tables general syntax:

    DELETE FROM 表名[WHERE Clause]
    • If you do not specify a WHERE clause, all records MySQL table will be deleted.
    • You can specify any condition in the WHERE clause
    • You can delete a one-time records in a single table.

    When you want to delete the data specified in the table record WHERE clause is very useful.

  • delete statement is used to delete the data in the table, the basic usage is:

    delete from table name where delete conditions;

    The following are examples of students in the table:

    Id delete row 3: delete from students where id = 3;

    Delete all younger than 21 years of data: delete from students where age <20;

    Delete all the data in the table: delete from students;

  • delete, drop, truncate table has a delete action, except that:

    •  1, delete and truncate table just delete data, delete data tables and even with the drop table structure, an analogy, delete a single kill, truncate a group off, the computer is a drop fell.
    •  2, delete a DML statement after the operation is complete If you do not want to commit the transaction can not be rolled back, truncate and drop a DDL statement, the operation is complete take effect immediately and can not be rolled back, an analogy, delete the letter is a micro break up, regret can withdrawn, truncate and drop directly slapped in the face, said roll, can not go back.
    •  3, the speed of execution, drop> TRUNCATE> the Delete , analogy, drop Shenzhou rocket, truncate is Harmony EMU, delete the bike.

      MySQL UPDATE update

      If we need to modify or update the data in MySQL, we can use the SQL UPDATE command to operate.

      grammar

      The following is the UPDATE command to modify MySQL Data Sheet Data Universal SQL syntax:

      UPDATE 表名  SET field1=new-value1, field2=new-value2 [WHERE Clause]
      • You can update one or more fields at the same time.
      • You can specify any condition in the WHERE clause.
      • You can also update data in a separate table.

      When you need to update the data specified in the table rows WHERE clause is very useful.

    • update statement can be used to modify the data in the table, to use simply the basic form:

      update the table column name = name set an update condition where the new value;

      The following are examples of students in the table:

      The id is the phone number 5 was changed to default  -: update students settel = default where id = 5;

      The age of all people increased 1: update students set age = age + 1;

      The phone number is 13288097888 name to "Bob", age changed to 19: update students setname = "Bob", age = 19 wheretel = "13288097888";

    • UPDATE replace a character in a field

      When we need a particular string field batch modify another string, can be used the following:

      UPDATE table_name SET field=REPLACE(field, 'old-string', 'new-string') [WHERE Clause]

      Example:

      The following examples will be updated to runoob_title runoob_id field value 3 "C ++" is replaced with "Python":

      UPDATE runoob_tbl SET runoob_title = REPLACE(runoob_title, 'C++', 'Python') where runoob_id = 3;
    • MySQL ALTER command

      When we need to modify the data table or modify data table field, you need to use the MySQL ALTER command.

    • Delete, add or modify fields table

      If you need to modify the field type and name, you can use MODIFY or CHANGE clauses in the ALTER command.

      For example, the field from c to CHAR type CHAR (1) (10), execute the following command:

      mysql> ALTER TABLE testalter_tbl MODIFY c CHAR(10);

      Use CHANGE clause grammar are very different. After the CHANGE keyword, followed by a field name that you want to modify, and then specify a new field name and type. Try the following example:

      mysql> ALTER TABLE testalter_tbl CHANGE i j BIGINT;
    • I use the following command field and more than ALTER DROP command to delete the clause to create a table:

      mysql> ALTER TABLE testalter_tbl  DROP i;

      If the data in the table only remaining one field you can not use DROP to delete the field.

    • mysql> ALTER TABLE testalter_tbl ADD i INT;
    •  

      If you now want to field j = "" the = "" bigint = "" changed to = "" int, sql statement is as follows:

      mysql> ALTER TABLE testalter_tbl CHANGE j j INT;
    • ALTER TABLE impact on the Null value and default values

      When you modify fields, you can specify whether to include the value or whether to set the default value.

      The following examples, j is specified field NOT NULL and the default value is 100.

      mysql> ALTER TABLE testalter_tbl 
          -> MODIFY j BIGINT NOT NULL DEFAULT 100;

      If you do not set a default value, MySQL will automatically set the field defaults to NULL.


      Modify the field defaults

      You can use the ALTER to modify the default value of a field, try the following examples:

      mysql> ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; mysql> SHOW COLUMNS FROM testalter_tbl; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | c | char(1) | YES | | NULL | | | i | int(11) | YES | | 1000 | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec)

      You can also use the default values ​​ALTER and DROP clause delete command field, the following examples:

      mysql> ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
      mysql> SHOW COLUMNS FROM testalter_tbl; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | c | char(1) | YES | | NULL | | | i | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec) Changing a Table Type:
    • Modify the table name

      If you need to modify the name of the data table, you can use the RENAME clause in ALTER TABLE statement to achieve.

      The following examples attempt to rename the data table testalter_tbl alter_tbl:

      mysql> ALTER TABLE testalter_tbl RENAME TO alter_tbl;
    • alter other uses:

      Modify storage engine: revised to myisam

      alter table tableName engine=myisam;

      Remove the foreign key constraint: keyName is a foreign key Alias

      alter table tableName drop foreign key keyName;

      Modifying the relative position of the fields: Field herein name1 is desired to modify, type1 original type field, first and after that a second election, it should be apparent, first the first place, after a field on the back name2

      alter table tableName modify name1 type1 first|after name2;

Guess you like

Origin www.cnblogs.com/wuyujun/p/11184344.html