10.29 SQL command

First, the operating table:
by:
Syntax:
Create table Table (
[Optional Parameters] among the type field, a comma remember #
Field Type ranked [Optional Parameters], # remember comma
field among the types [optional parameters] # last line without commas
)
column constraints:
AUTO_INCREMENT: incremented. 1
primary key: primary key index, speed up queries, column values can not be repeated
nOT NULL identifier of the field can not be blank
field is set for the default dEFAULT value

MySQL primary key and relationships increment of:
1, the primary key mysql not necessarily self-energizing; contrary: setting the self-energizing properties columns must be the primary key, or add UNIQUE index
2, the primary key is unique in nature, i.e., can not enter the same the value

 整型的每一种都分为:无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在除char以外的数据类型中,默认情况下声明的整型变量都是有符号的类型;char在默认情况下总是无符号的。在除char以外的数据类型中,如果需声明无符号类型的话就需要在类型前加上unsigned。

Examples: Create Database TTT charset UTF8;
-> use TTT;
-> Create Table T2 (
-> ID int,
-> name char (. 5)
->) charset = UTF8;
Query the OK, 0 rows affected (0.03 sec)
to create a data table t2 success!
Increasing the data:
Syntax:
INSERT INTO table name (column 1, column 2) values (values 1, 'value 2');
examples:
INSERT INTO T2 (ID, name) values (1, 'Dudu');
INSERT INTO T2 ( id, name) values (2, 'haha');
query data:
syntax:
SELECT column 1, column 2 from table; ( representative for all columns)
For example: select
from T2;
+ ------ + - + -----
| the above mentioned id | name |
+ ------ + ------- +
| 1 | dudu |
+ ------ + ------- +
1 Row in set (0.00 sec)
example:
the Create the Table T3 (
id int auto_increment primary key,
name char(10)
)charset=utf8;
insert into t3(name) values('ldd');
例子:(推荐)
create table t4(
id int unsigned auto_increment primary key,
name char(10) not null default 'lla',
age int not null default 0
)charset=utf8;

    mysql> insert into t4 (age) values (10);
    Query OK, 1 row affected (0.05 sec)

    mysql> select * from t4;
    +----+------+-----+
    | id | name | age |
    +----+------+-----+
    |  1 | lla  |  10 |
    +----+------+-----+
    

Column type:
Create table Table (
field type unsigned ranked [Optional Parameters],
field types ranked [Optional Parameters],
field types ranked [Optional Parameters] # last line without commas
) charset = UTF8;
- digital
- integer
tinyint
smallint The
int
MEDIUMINT
BIGINT

    a.整数类型
    b.取值范围
    c.unsigned 加上代表不能取负数 只适用于整型
    
 -浮点型
  create table t5(
      id int auto_increment primary key,
      salary decimal(16,10),
      num float
  )charset=utf8;
    
  float:不一定精确
  decimal:非常的精确的数字(5000.23)  decimal(6,2) m是数字总个数(负号不算),d是小数点后个数。
  正好10位:
  insert into t5(salary,num) values (500023.2312345678,5000.23237834567488);
  select * from t5;
  +----+-------------------+---------+
  | id | salary            | num     |
  |  1 | 500023.2312345678 | 5000.23 |
  +----+-------------------+---------+
  1 row in set (0.00 sec)
  少于10位:(会自动补齐10位)
  insert into t5(salary,num) values (500023.231234567,5000.23237834567488);
  select * from t5;
  +----+-------------------+---------+
  | id | salary            | num     |
  |  1 | 500023.2312345678 | 5000.23 |
  |  2 | 500023.2312345670 | 5000.23 |
  +----+-------------------+---------+
  1 row in set (0.00 sec)
  多于10位:(四舍五入只留10位)
  insert into t5(salary,num) values (500023.23123456789,5000.23237834567488);
  select * from t5;
  +----+-------------------+---------+
  | id | salary            | num     |
  |  1 | 500023.2312345678 | 5000.23 |
  |  2 | 500023.2312345670 | 5000.23 |
  |  3 | 500023.2312345679 | 5000.23 |
  +----+-------------------+---------+
  1 row in set (0.00 sec)

-字符串
-char(长度):定长
create table t6(
id int unsigned auto_increment primary key,
name char(10) not null default 'lls'
)charset=utf8;
insert into t6 (name) values ('hello');
insert into t6 (name) values ('hellolkjsadjjj');
select * from t6;
+----+------------+
| id | name |
+----+------------+
| 1 | hello |
+----+------------+
| 2 | hellolkjsa |
+----+------------+
1 row in set (0.00 sec)
-varchar(长度):变长
create table t7(
id int unsigned auto_increment primary key,
name varchar(10) not null default 'lls'
)charset=utf8;
insert into t7 (name) values ('hello');
INTO T7 INSERT (name) values ( 'hellolkjsadjjj');
the SELECT * from T7;
+ ---- + ------------ +
| the above mentioned id | name |
+ ---- + - + ----------
| 1 | the Hello |
+ ---- + ------------ +
| 2 | hellolkjsa |
+ ---- + ---- -------- +
difference:
char: fixed-length, no matter how many characters are inserted, forever fixed duty specified length
scenes:
1. ID
2. phone number char (11)
3.md5 after encryption value, such as passwords char (32)
VARCHAR: variable length, the length of the string is inserted according to calculate the number of bytes occupied by, but is used to store a byte of character sizes
Note: If the inserted data is not determined size, is generally recommended to use varchar (255)

  • The date and time types
    YEAR
    YYYY (1901/2155)

DATE
YYYY-MM-DD(1000-01-01/9999-12-31)

TIME HH:MM:SS('-838:59:59'/'838:59:59')

DATETIME (***************************)

YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59    Y)

TIMESTAMP
YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某时)
例子:
create table t8(
d date,
t time,
dt datetime
);
mysql> insert into t8 values(now(),now(),now());
Query OK, 1 row affected, 1 warning (0.08 sec)

mysql> select * from t8;
+------------+----------+---------------------+
| d | t | dt |
+------------+----------+---------------------+
| 2019-10-29 | 17:06:56 | 2019-10-29 17:06:51 |
+------------+----------+---------------------+
1 row in set (0.00 sec)
枚举:列出所有的选项
create table t9(
id int auto_increment primary key,
gender enum('male','female')
)charset utf8;
insert into t9 (gender) values ('male');
Query OK, 1 row affected (0.01 sec)
insert into t9 (gender) values ('male');
Query OK, 1 row affected (0.00 sec)
insert into t9 (gender) values ('jasjdhsjncbnv');
warning ...
+----+--------+
| id | name |
+----+--------+
| 1 | male |
+----+--------+
| 2 | female |
+----+--------+
| 3 | |
+----+--------+

    改
        1. 修改表名
            ALTER TABLE 旧表名 RENAME 新表名;

            mysql> alter table t8 rename t88;
            Query OK, 0 rows affected (0.19 sec)

        2. 增加字段
            ALTER TABLE 表名
            ADD 字段名 列类型 [可选的参数],
            ADD 字段名 列类型 [可选的参数];

            mysql> alter table t5 add gender varchar(32) not null default '';

The OK Query, 0 rows affected (0.82 sec)
Records: 0 Duplicates: 0 Warnings: 0
MySQL> SELECT * from T5;
+ ---- + ------------------ - + --------- + -------- +
| the above mentioned id | salary | NUM | Gender |
+ ---- + -------------- + --------- + -------- + -----
|. 1 | 500,023.2312345678 | 500023 | |
| 2 | 500,023.2312345670 | 5000.23 | |
|. 3 | 500,023.2312345679 | 5000.23 | |
+ + ------------------- + --------- + ---- + --------
3 rows in the SET (0.00 sec)
added on top of the column is always added after the last one

            ALTER TABLE 表名
            ADD 字段名 列类型 [可选的参数] FIRST;

            mysql> alter table t5 add name3 varchar(32) not null default '' first;
        Query OK, 0 rows affected (0.83 sec)
        Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from t5;
+------+----+-------------------+---------+
| name | id | salary | num |
+------+----+-------------------+---------+
| | 1 | 500023.2312345678 | 500023 |
| | 2 | 500023.2312345670 | 5000.23 |
| | 3 | 500023.2312345679 | 5000.23 |
+------+----+-------------------+---------+
3 rows in set (0.00 sec)

            ALTER TABLE 表名
            ADD 字段名 列类型 [可选的参数] AFTER 字段名;
            mysql> alter table t5 add name4 varchar(32) not null default '' after id;
        Query OK, 0 rows affected (0.68 sec)
        Records: 0  Duplicates: 0  Warnings: 0
            mysql> select * from t5;

+------+----+-------+-------------------+---------+
| name | id | name4 | salary | num |
+------+----+-------+-------------------+---------+
| | 1 | | 500023.2312345678 | 500023 |
| | 2 | | 500023.2312345670 | 5000.23 |
| | 3 | | 500023.2312345679 | 5000.23 |
+------+----+-------+-------------------+---------+
3 rows in set (0.00 sec)

        3. 删除字段
            ALTER TABLE 表名  DROP 字段名;

mysql> alter table t5 drop name4;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from t5;
+------+----+-------------------+---------+--------+
| name | id | salary | num | gender |
+------+----+-------------------+---------+--------+
| | 1 | 500023.2312345678 | 500023 | |
| | 2 | 500023.2312345670 | 5000.23 | |
| | 3 | 500023.2312345679 | 5000.23 | |
+------+----+-------------------+---------+--------+
3 rows in set (0.00 sec)

        4. 修改字段
            ALTER TABLE 表名 MODIFY 字段名 数据类型 [完整性约束条件…];

            mysql> alter table t5 modify num char(8);

Query OK, 3 rows affected (0.07 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from t5;
+------+----+-------------------+---------+--------+
| name | id | salary | num | gender |
+------+----+-------------------+---------+--------+
| | 1 | 500023.2312345678 | 500023 | |
| | 2 | 500023.2312345670 | 5000.23 | |
| | 3 | 500023.2312345679 | 5000.23 | |
+------+----+-------------------+---------+--------+
3 rows in set (0.00 sec)

            ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…];

            mysql> alter table t6 change name name1 varchar(20) not null default '';

Query OK, 3 rows affected (0.07 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from t6;
+----+------------+
| id | name1 |
+----+------------+
| 1 | hello |
| 2 | hskjdsdcdj |
| 3 | hskjdsdcdj |
+----+------------+
3 rows in set (0.00 sec)

            mysql> alter table t6 change name1 name2;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '' at line 1

    删
        drop table 表名;  #### 线上禁用
        mysql> drop table t7;

Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+---------------+
| Tables_in_ttt |
+---------------+
| t1 |
| t10 |
| t2 |
| t3 |
| t4 |
| t5 |
| t6 |
| t88 |
| t9 |
+---------------+
9 rows in set (0.01 sec)

    复制表结构:

mysql> ## 1. View the table creation statements t88
MySQL> Show the Create the Table t88;
+ ------- + ---------------------- ------------------------------------------------
- -------------------------------------------------- ---------------------------
----------------------- + -----------------
| the Table | the Create the Table

                                    |

+-------+----------------------------------------------------------------------

----------------------------------------+
| t88 | CREATE TABLE t88 (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(10) NOT NULL DEFAULT 'lls',
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------
-------------------------------------------------------------------------------
----------------------------------------+
1 row in set (0.00 sec)

        mysql> ## 2. like
        mysql> create table t89 like t88;

Query OK, 0 rows affected (0.02 sec)

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

mysql> select * from t88;
+----+------------+
| id | name |
+----+------------+
| 1 | hskjdsdcdj |
| 2 | hskjdsdcdj |
+----+------------+
2 rows in set (0.00 sec)

Second, the operating table data lines:
by:
increasing the data:
Syntax:
INSERT INTO table name (column 1, column 2) values (values 1, 'value 2');
examples:
INSERT INTO T1 (ID, name) values (1, 'LDD');
INSERT INTO T1 (ID, name) values (. 1, 'WHH');
INSERT INTO T1 (ID, name) values (. 1, 'LDD2'), (2, 'LDD3'), (. 3, 'ldd4');
MySQL> SELECT * from T1;
+ ------ + ------- +
| ID | name |
+ ------ + ------- +
| . 1 | Duodu |
|. 1 | Duodu |
|. 1 | LDD |
|. 1 | WHH |
|. 1 | LDD2 |
| 2 | LDD3 |
|. 3 | ldd4 |
+ ------ + ------- +
in rows SET. 7 (0.00 sec)
MySQL> INSERT INTO T5 (name) SELECT from NAME1 T6;
Query the OK,. 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from t5;
+------------+----+-------------------+---------+--------+
| name | id | salary | num | gender |
+------------+----+-------------------+---------+--------+
| | 1 | 500023.2312345678 | 500023 | |
| | 2 | 500023.2312345670 | 5000.23 | |
| | 3 | 500023.2312345679 | 5000.23 | |
| hello | 4 | NULL | NULL | |
| hskjdsdcdj | 5 | NULL | NULL | |
| hskjdsdcdj | 6 | NULL | NULL | |
+------------+----+-------------------+---------+--------+
6 rows in set (0.00 sec)

    删
        delete from 表名 where 条件;
            mysql> delete from t5 where id=1;

Query OK, 1 row affected (0.01 sec)

mysql> select * from t5;
+------------+----+-------------------+---------+--------+
| name | id | salary | num | gender |
+------------+----+-------------------+---------+--------+
| | 2 | 500023.2312345670 | 5000.23 | |
| | 3 | 500023.2312345679 | 5000.23 | |
| hello | 4 | NULL | NULL | |
| hskjdsdcdj | 5 | NULL | NULL | |
| hskjdsdcdj | 6 | NULL | NULL | |
+------------+----+-------------------+---------+--------+
5 rows in set (0.00 sec)
mysql> delete from t5 where id>1;
mysql> delete from t5 where id>=1;
mysql> delete from t5 where id<1;
mysql> delete from t5 where id<=1;
mysql> delete from t5 where id>=1 and id<10;
Query OK, 1 row affected (0.06 sec)

            delete from 表名; 删除表中所有的数据
            mysql> delete from t5;

Query OK, 6 rows affected (0.00 sec)

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

            mysql> insert into t5 (salary, num) values (500023.2312345679,  5000.24);

Query OK, 1 row affected (0.00 sec)

mysql> select * from t5;
+------+----+-------------------+---------+--------+
| name | id | salary | num | gender |
+------+----+-------------------+---------+--------+
| | 8 | 500023.2312345679 | 5000.24 | |
+------+----+-------------------+---------+--------+
1 row in set (0.00 sec)

        truncate 表名; #### 没有where条件的
            mysql> truncate t5;
            Query OK, 0 rows affected (0.25 sec)

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

            mysql> insert into t5 (salary, num) values (500023.2312345679,  5000.24);

Query OK, 1 row affected (0.00 sec)

MySQL> the SELECT * from T5;
+ ------ + ---- + --------- + ------------------- + + --------
| name | the above mentioned id | salary | NUM | Gender |
+ ------ + ---- + ----------------- - + --------- + -------- +
| | 1 | 500,023.2312345679 | 5000.24 | |
+ ------ + ---- + ------ + --------- + -------- + -------------
. 1 in Row SET (0.00 sec)
differences:
after 1. delete, insert data from the time the primary key from increased 1 start, then truncate from the beginning 1
2. delete delete delete line by line, truncate: Select delete to delete truncate speed is higher than delete the

    改
        update 表名 set 列名1=新值1,列名2=新值2 where 条件;
            mysql> update t1 set name='whh' where id=1;

Query OK, 4 rows affected (0.00 sec)
Rows matched: 5 Changed: 4 Warnings: 0

mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | whh |
| 1 | whh |
| 1 | whh |
| 1 | whh |
| 1 | whh |
| 2 | ldd3 |
| 3 | ldd4 |
+------+------+
7 rows in set (0.00 sec)

            mysql> update t1 set name='ggg' where id>=1;
            mysql> update t1 set name='ggg' where id<3;
            mysql> update t1 set name='ggg' where id<=3;

    查

        语法:
            select 列1, 列2 from 表名;  (*代表查询所有的列)
            select * from 表名;  (*代表查询所有的列)
            mysql> select * from t1 where id>1;

+------+------+
| id | name |
+------+------+
| 2 | ldd3 |
| 3 | ldd4 |
+------+------+
2 rows in set (0.00 sec)
select * from t1 where id=1;
select * from t6 where id<=3;

            between..and...: 取值范围是闭区间

                mysql> select * from t1 where id between 1 and 3;

+------+------+
| id | name |
+------+------+
| 1 | whh |
| 1 | whh |
| 1 | whh |
| 1 | whh |
| 1 | whh |
| 2 | ldd3 |
| 3 | ldd4 |
+------+------+
7 rows in set (0.00 sec)

            避免重复DISTINCT
                mysql> select distinct name from t1;

+ ------ +
| name |
+ ------ +
| WHH |
| LDD3 |
| ldd4 |
+ ------ +
3 rows in the SET (0.00 sec)
through four operational queries (not a)
MySQL> SELECT name, Age 10 from T3;
+ ------ + -------- +
| name | Age
10 |
+ ------ + ------- - +
| LDD | 100 |
+ ------ + -------- +
. 1 in Row SET (0.01 sec)

                mysql> select name, age*10 as age from t3;
                +------+-----+
                | name | age |
                +------+-----+
                | ldd  | 100 |
                +------+-----+
                1 row in set (0.02 sec)

            in(80,90,100):

                mysql> select * from t1 where id in (2,3,4);

+------+------+
| id | name |
+------+------+
| 2 | ldd3 |
| 3 | ldd4 |
+------+------+
2 rows in set (0.00 sec)

like : 模糊查询
以w开头:
mysql> select * from t1 where name like 'w%';
+------+------+
| id | name |
+------+------+
| 1 | whh |
| 1 | whh |
| 1 | whh |
| 1 | whh |
| 1 | whh |
+------+------+
5 rows in set (0.00 sec)
以3结尾:
mysql> select * from t1 where name like '%3';
+------+------+
| id | name |
+------+------+
| 2 | ldd3 |
+------+------+
1 row in set (0.00 sec)

包含d的:
mysql> select * from t1 where name like '%d%';
+------+------+
| id | name |
+------+------+
| 2 | ldd3 |
| 3 | ldd4 |
+------+------+
2 rows in set (0.00 sec)

Guess you like

Origin www.cnblogs.com/lidandanaa/p/11761184.html