Operating mysql database table

MySQL library, detailed operating table
MySQL database

Contents of this section

A library operations
two table operations
three lines of operation

A library operation

  1. Create a database

    1.1 Syntax

      CREATE DATABASE database name of the charset utf8;

    1.2 database naming rules

      May consist of letters, numbers, underscore, @, #, $

      Case sensitive

      Uniqueness

      You can not use keywords such as create select

      You can not use digital alone

      Up to 128

      # Basically the same with the naming of python or js

  2, database-related actions

    Also some basic operations, and before we said almost.

Copy the code
1 View database
Show Databases;
Show the Create Database db1;
the SELECT Database ();

2 Select the database
USE database name

3. Delete the database
DROP DATABASE database name;

4 modify the database
alter database db1 charset utf8;
duplicated code

  Details about the library, let's say, the right, ha ha, is not it a little less, not our focus, look at the following table operations ~~~

Two operating table
  1, the storage engine

    That table storage engine type, mysql tables according to the different types of treatment have different mechanisms on the introduction of a storage engine I see this blog: https://www.cnblogs.com/clschao/articles/9953550.html

  2, table describes the

    Table corresponding to the file, a record in the table of contents of the file is equivalent to a line, a table corresponding to the title is recorded, called fields of the table

    

    id first row, name2, age, and the remaining is a field of a record is called a single line.

  3. Create a table

    Table 3.1 build grammar    

Copy the code

grammar:

create table table (
Field Name Type 1 [(width) constraints],
Field Name Type 2 [(width) constraints],
Field Name Type 3 [(width) constraints]
);

note:

  1. In the same table, field names are not the same
  2. Alternatively the width and constraints, not necessarily, refers to the width of the field length constraints, for example: char (10) inside 10
  3. Field names and types is a must
    copy the code

Copy the code
mysql> create database db1 charset utf8;

mysql> use db1;

mysql> create table t1(
-> id int,
-> name varchar(50),
-> sex enum('male','female'),
-> age int(3)
-> );

mysql> show tables; # View all the table names under db1 library

mysql> desc t1;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+

mysql> select id,name,sex,age from t1;
Empty set (0.00 sec)

mysql> select * from t1;
Empty set (0.00 sec)

MySQL> SELECT ID, name from T1;
Empty SET (0.00 sec)
copying the code

复制代码
mysql> insert into t1 values
-> (1,'chao',18,'male'),
-> (2,'sb',81,'female')
-> ;
mysql> select * from t1;
+------+------+------+--------+
| id | name | age | sex |
+------+------+------+--------+
| 1 | chao | 18 | male |
| 2 | sb | 81 | female |
+------+------+------+--------+

mysql> insert into t1(id) values
-> (3),
-> (4);
mysql> select * from t1;
+------+------+------+--------+
| id | name | age | sex |
+------+------+------+--------+
| 1 | chao | 18 | male |
| 2 | sb | 81 | female |
| 3 | NULL | NULL | NULL |
| 4 | NULL | NULL | NULL |
+------+------+------+--------+
复制代码

  4, view the table structure

Copy the code
mysql> describe t1; # see table structure can be abbreviated as: desc table name
+ -------- + ------- - + ------ + ----- + --------- + ------- +
| Field, | Type | Null | Key | the Default | Extra |
+ --- ----------------------- + ---- + ------ + ----- + -------- - + ------- +
| ID | int (. 11) | YES | | NULL | |
| name | VARCHAR (50) | YES | | NULL | |
| Sex | enum ( 'MALE', 'FEMALE' ) | YES | | NULL | |
| Age | int (3) | YES | | NULL | |
+ ------- + ------------------- ---- + ------ + ----- + --------- + ------- +

mysql> show create table t1 \ G ; # Display detailed structure, can be added \ G
duplicated code

  5, MySQL basic data types

    About data types, I see this blog: https://www.cnblogs.com/clschao/articles/9959559.html

  6, the table integrity constraints

    Integrity constraints on the table, I see this blog: https://www.cnblogs.com/clschao/articles/9968396.html

    

   7, modify the table alter table

    The following content is not with us demonstrates, we look at a simple band, are fixed syntax, in accordance with this written on the line, there is no logic at all, so do not do a lot of presentations, we all go back to practice it:

    

Copy the code
syntax:

  1. Modify table
    ALTER TABLE table
    RENAME new table name;

  2. Add Field
    ALTER TABLE table name
    ADD Field Name Data Type [integrity constraints ...], # Note that can be divided by commas, this Add more constraints
    ADD Field Name Data Type [integrity constraints ...];
    ALTER TABLE table name
    ADD field name data type [integrity constraints ...] fIRST; # add this field time, put it to the first field position.
    ALTER TABLE table name
    ADD name field data type [integrity constraints ...] AFTER field name; #after is behind the field after he had put it, we passed after a first and a new field can be added into the any field position of the table.

  3. Delete field
    ALTER TABLE table
    DROP field name;

  4. Modify the field
    ALTER TABLE table
    MODIFY Field Name Data Type [integrity constraints ...];
    ALTER TABLE table name
    CHANGE old legacy field name field name new data type [integrity constraints ...]; #change further modify more than one change the name of the function, which is only a change of name of a field
    ALTER tABLE table name
    cHANGE old name for the new field name field new data types [integrity constraints ...]; # this is a field in addition to the name change, but also to change the data type, content integrity constraints like
    copy the code
        to add a foreign key field attribute statement: alter table tABLE 2 add foreign key (a field in table 2) references a table (table 1, a field);

    Note that: the inside mysql table name is not case sensitive, if you will a named t1 (t1 lowercase) changed its name to a T1 (uppercase T1), is completely useless, because the table name in a database They are lowercase.

Copy the code
sample:

  1. Modify storage engine
    MySQL> Table ALTER-Service
    -> Engine = InnoDB;

  2. 添加字段
    mysql> alter table student10
    -> add name varchar(20) not null,
    -> add age int(3) not null default 22;

mysql> alter table student10
after adding // name field;> add stu_num varchar (10) not null after name -

MySQL> ALTER Table student10
-> Sex the Add enum ( 'MALE', 'FEMALE') default 'MALE' First; // add to the front

  1. Delete field
    MySQL> the ALTER student10 the Table
    -> drop Sex;

mysql> alter table service
-> drop mac;

  1. Modify the field type Modify
    MySQL> ALTER Table student10
    -> Age Modify int (. 3);
    MySQL> ALTER Table student10
    -> Modify ID int (. 11) Not null Primary Key AUTO_INCREMENT; // modify the primary key

  2. Adding constraints (increased auto_increment for existing primary key)
    MySQL> ALTER Modify Table student10 ID int (. 11) Not null auto_increment Primary Key;
    ERROR 1068 (42000): Multiple Primary Key defined

mysql> alter table student10 modify id int(11) not null auto_increment;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

  1. Table increase existing composite primary key
    MySQL> ALTER Table Service2
    -> the Add Primary Key (host_ip, Port);

  2. Increase the master key
    MySQL> ALTER Table student1
    -> Modify name VARCHAR (10) Not null Primary Key;

  3. And automatically increase the master key growth
    MySQL> ALTER Table student1
    -> Not Modify ID int Primary Key AUTO_INCREMENT null;

  4. Drop Primary

. a Delete increment constraint
mysql> alter table student10 modify id int (11) not null;

. b Drop Primary
MySQL> ALTER Table student10
-> Primary Key drop;
duplicated code

    

   8, copy table

    

    We check out the results of the existing table structure by select another table records (data), we re-create a table and it is the same time, also need to create their own before using the table structure written, then insert those data to the new table, a lot of trouble, then we can directly use the copy function provided in the table mysql :( copy table with a little expensive, look on the line)

    Syntax: copy table structure + recording (key will not be copied: primary keys, foreign keys, and indexes)
    MySQL> the Create the Table new_service the SELECT * from service; # This means that you query from a table service inside out data not on the screen print, and direct you to my new table new_service

    We wrote an example:

      

      Although we can not copy the key, but we can give him back ah: alter table xxx

      

    Just copy the table structure, not data
    mysql> select * from service where 1 = 2; // condition is false, can not find any records, so we can just copy table structure through it, look at the following sentence
    Empty set (0.00 sec)
    mysql> create table new1_service select * from service where 1 = 2; condition # filtered data is false, then only got structure, and did not check out any data, so do just copy the table structure
    query OK, 0 rows affected ( sec 0.00)
    Records: 0 Duplicates: 0 Warnings: 0

    mysql> create table t4 like employees; # obtaining this effect can be achieved with a like

And then have time, you can preview the back of my blog, operating on the line (data line \ records). Blog is:

Guess you like

Origin www.cnblogs.com/hualibokeyuan/p/11468659.html