引擎: 决定数据库存取数据的方式 => 不同的特点 => 不同的用户体验 数据类型: 规定了数据库可以存放哪些数据 约束: 限制存储数据的规则 键

Microsoft Windows [版本 10.0.17134.472]
(c) 2018 Microsoft Corporation。保留所有权利。

C:\Users\Administrator>SQL;
'SQL' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

C:\Users\Administrator>SQL
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> drop database db1;
Query OK, 6 rows affected (0.41 sec)

mysql> drop database db2;
Query OK, 1 row affected (0.19 sec)

mysql> drop database db3;
ERROR 1008 (HY000): Can't drop database 'db3'; database doesn't exist
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> use db1;
Database changed
mysql> create table t1(age int)engine=memory;
Query OK, 0 rows affected (0.08 sec)

mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1 |
+---------------+
1 row in set (0.04 sec)

mysql> quit;
Bye

C:\Users\Administrator>sql
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show tables;
ERROR 1046 (3D000): No database selected
mysql> use db1;
Database changed
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1 |
+---------------+
1 row in set (0.00 sec)

mysql> show tables;
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> create table t1(name char)engine=innodb;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 1
Current database: db1

ERROR 1050 (42S01): Table 't1' already exists
mysql> desc ti;
ERROR 1146 (42S02): Table 'db1.ti' doesn't exist
mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| age | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.02 sec)

mysql> quit;
Bye

C:\Users\Administrator>sql
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use db1;
Database changed
mysql> show create t1;
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 't1' at line 1
mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`age` int(11) DEFAULT NULL
) ENGINE=MEMORY DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------+
1 row in set (0.03 sec)

mysql> create table t2(name char(5) not null)engine=innodb charset=utf8;
Query OK, 0 rows affected (0.29 sec)

mysql> create table t3(name char(5) )engine=innodb charset=utf8;
Query OK, 0 rows affected (0.38 sec)

mysql> insert into from t1 values('zzfsn');
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 'from t1 values('zzfsn')' at line 1
mysql> insert into t1 values('zzfsn');
Query OK, 1 row affected, 1 warning (0.05 sec)

mysql> insert into t1 values('');
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> insert into t1 values(null);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values('');
Query OK, 1 row affected (0.08 sec)

mysql> insert into t2 values();
Query OK, 1 row affected, 1 warning (0.07 sec)

mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`age` int(11) DEFAULT NULL
) ENGINE=MEMORY DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table t2;
+-------+------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------+
| t2 | CREATE TABLE `t2` (
`name` char(5) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------+
1 row in set (0.05 sec)

mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| name | char(5) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.02 sec)

mysql> insert into t2 values();
Query OK, 1 row affected, 1 warning (0.08 sec)

mysql> insert into t2 name. values();
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 'name. values()' at line 1
mysql> insert into t2 name.values();
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 'name.values()' at line 1
mysql> insert into t3 name.values();
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 'name.values()' at line 1
mysql> insert into t2(name).values();
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 '.values()' at line 1
mysql> insert into t2 (name) values();
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into t3 (name) values();
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into t2 (name) values(null);
ERROR 1048 (23000): Column 'name' cannot be null
mysql> insert into t3 (name) values(null);
Query OK, 1 row affected (0.04 sec)

mysql> insert into t2 (name) values('rydtfugi');
Query OK, 1 row affected, 1 warning (0.08 sec)

mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| name | char(5) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.02 sec)

mysql> select name from t2;
+-------+
| name |
+-------+
| |
| |
| |
| rydtf |
+-------+
4 rows in set (0.12 sec)

mysql> show variables like'%sql_mode%';
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| sql_mode | NO_ENGINE_SUBSTITUTION |
+---------------+------------------------+
1 row in set (0.00 sec)

mysql> show variables like '%sql_mode%';
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| sql_mode | NO_ENGINE_SUBSTITUTION |
+---------------+------------------------+
1 row in set (0.00 sec)

mysql> set global sql_mode='strict_trans_tables';
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t2 name values ('ryugiutyguh');
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 'name values ('ryugiutyguh')' at line 1
mysql> insert into t2 (name) values ('ryugiutyguh');
Query OK, 1 row affected, 1 warning (0.07 sec)

mysql> quit
Bye

C:\Users\Administrator>sql
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> insert into t2 (name) values ('ryyugiuhiougiutyguh');
ERROR 1046 (3D000): No database selected
mysql> use db1;
Database changed
mysql> insert into t2 (name) values ('ryyugiuhiougiutyguh');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> creat table t4;
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 'creat table t4' at line 1
mysql> create table t4(age int);
Query OK, 0 rows affected (0.28 sec)

mysql> insert into t4 values (5787);
Query OK, 1 row affected (0.07 sec)

mysql> creat table t5 (age int(4) zerofill);
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 'creat table t5 (age int(4) zerofill)' at line 1
mysql> creat table t5 (age int(4)) zerofill;
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 'creat table t5 (age int(4)) zerofill' at line 1
mysql> create table t5 (age int(4) zerofill);
Query OK, 0 rows affected (0.39 sec)

mysql> insert into t1 values (3546735467);
ERROR 1264 (22003): Out of range value for column 'age' at row 1
mysql> insert into t5 values (3546735467);
Query OK, 1 row affected (0.08 sec)

mysql> insert into t1 values (3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t5 values (3);
Query OK, 1 row affected (0.07 sec)

mysql> desc t5;
+-------+--------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------+-------+
| age | int(4) unsigned zerofill | YES | | NULL | |
+-------+--------------------------+------+-----+---------+-------+
1 row in set (0.02 sec)

mysql> select*from t5;
+------------+
| age |
+------------+
| 3546735467 |
| 0003 |
+------------+
2 rows in set (0.00 sec)

mysql> create table t6(age float);
Query OK, 0 rows affected (0.27 sec)

mysql> insert into t6 values(0.246354335555555555533333333333333);
Query OK, 1 row affected (0.08 sec)

mysql> select age from t6;
+----------+
| age |
+----------+
| 0.246354 |
+----------+
1 row in set (0.00 sec)

mysql> create table t7(age decimal);
Query OK, 0 rows affected (0.27 sec)

mysql> insert into t7 age values (1.22336564575674342223546);
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 'age values (1.22336564575674342223546)' at line 1
mysql> insert into t7 (age) values (1.22336564575674342223546);
Query OK, 1 row affected, 1 warning (0.07 sec)

mysql> select age from t7;
+------+
| age |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> select*from t7;
+------+
| age |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table t8(age decimal(65,30));
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 'decimal(65,30))' at line 1
mysql> create table t8(age decimal(65,30));
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 'decimal(65,30))' at line 1
mysql> create table t8(age decimal(65,30));
Query OK, 0 rows affected (0.25 sec)

mysql> select age from t8;
Empty set (0.00 sec)

mysql> insert into t8 values(7.46235744666666666);
Query OK, 1 row affected (0.07 sec)

mysql> select age from t8;
+----------------------------------+
| age |
+----------------------------------+
| 7.462357446666666660000000000000 |
+----------------------------------+
1 row in set (0.00 sec)

mysql> create table t9(x char(5),y varchar(5));
Query OK, 0 rows affected (0.25 sec)

mysql> insert into t9 (x,y)values('ftu','ytu');
Query OK, 1 row affected (0.07 sec)

mysql> select *from t9;
+------+------+
| x | y |
+------+------+
| ftu | ytu |
+------+------+
1 row in set (0.00 sec)

mysql> select char_length(x),char_length(y) from t9;
+----------------+----------------+
| char_length(x) | char_length(y) |
+----------------+----------------+
| 3 | 3 |
+----------------+----------------+
1 row in set (0.09 sec)

mysql> select global sql_mode='strict_tables,PAD_CHAR_TO_FULL_LENGTH';
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 '='strict_tables,PAD_CHAR_TO_FULL_LENGTH'' at line 1
mysql> set global sql_mode='strict_tables,PAD_CHAR_TO_FULL_LENGTH';
ERROR 1231 (42000): Variable 'sql_mode' can't be set to the value of 'strict_tables'
mysql> set global sql_mode='strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH';
Query OK, 0 rows affected (0.00 sec)

mysql> select char_length(x),char_length(y) from t9;
+----------------+----------------+
| char_length(x) | char_length(y) |
+----------------+----------------+
| 3 | 3 |
+----------------+----------------+
1 row in set (0.00 sec)

mysql> quit;
Bye

C:\Users\Administrator>sql;
'sql' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

C:\Users\Administrator>sql
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select char_length(x),char_length(y) from t9;
ERROR 1046 (3D000): No database selected
mysql> use db1;
Database changed
mysql> select char_length(x),char_length(y) from t9;
+----------------+----------------+
| char_length(x) | char_length(y) |
+----------------+----------------+
| 5 | 3 |
+----------------+----------------+
1 row in set (0.00 sec)

mysql> create table t10(name enum('zzf','sn')not null default 'zf',hobbies set('basketball','pingpang','baseball');
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
mysql> create table t10(name enum('zzf','sn')not null default 'zf',hobbies set('basketball','pingpang','baseball'));
ERROR 1067 (42000): Invalid default value for 'name'
mysql> create table t10(name enum('zzf','sn')not null default 'zzf',hobbies set('basketball','pingpang','baseball'));
Query OK, 0 rows affected (0.29 sec)

mysql> create table t11(my_datetime datetime, my_time time);
Query OK, 0 rows affected (0.27 sec)

mysql> insert into t11 values();
Query OK, 1 row affected (0.88 sec)

mysql> select*from t11;
+-------------+---------+
| my_datetime | my_time |
+-------------+---------+
| NULL | NULL |
+-------------+---------+
1 row in set (0.00 sec)

mysql> alter table t11 mondify my_time timestamp;
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 'mondify my_time timestamp' at line 1
mysql> alter table t11 mondify my_time timestamp;
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 'mondify my_time timestamp' at line 1
mysql> alter table t11 change my_time my_tim timestamp;
Query OK, 1 row affected (0.87 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> insert into t11(null,null);
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 'null,null)' at line 1
mysql> insert into t11 values(null,null);
Query OK, 1 row affected (0.08 sec)

mysql> select*from t11;
+-------------+---------------------+
| my_datetime | my_tim |
+-------------+---------------------+
| NULL | 2019-01-08 19:06:22 |
| NULL | 2019-01-08 19:08:17 |
+-------------+---------------------+
2 rows in set (0.00 sec)

mysql> insert into t11 values();
Query OK, 1 row affected (0.08 sec)

mysql> select *from t11;
+-------------+---------------------+
| my_datetime | my_tim |
+-------------+---------------------+
| NULL | 2019-01-08 19:06:22 |
| NULL | 2019-01-08 19:08:17 |
| NULL | 2019-01-08 19:09:21 |
+-------------+---------------------+
3 rows in set (0.00 sec)

mysql> create table t12(ip char(16), port int,unique(ip,port));
Query OK, 0 rows affected (0.36 sec)

mysql> insert into table t12 values('192.168.12.168',4646),('192.168.12.168',3563);
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 'table t12 values('192.168.12.168',4646),('192.168.12.168',3563)' at line 1
mysql> insert into table t12 values('192.168.12.168',4646);
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 'table t12 values('192.168.12.168',4646)' at line 1
mysql> insert into table t12 (ip,port) values('192.168.12.168',4646),('192.168.12.168',3563);
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 'table t12 (ip,port) values('192.168.12.168',4646),('192.168.12.168',3563)' at line 1
mysql> insert into table t12 (ip,port) values('192.168.12.168',4646);
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 'table t12 (ip,port) values('192.168.12.168',4646)' at line 1
mysql> insert into table t12 values('192.168.12.168',4646),('192.168.12.168',3563);
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 'table t12 values('192.168.12.168',4646),('192.168.12.168',3563)' at line 1
mysql> insert into t12 values('192.168.12.168',4646),('192.168.12.168',3563);
Query OK, 2 rows affected (0.06 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select*from t12;
+------------------+------+
| ip | port |
+------------------+------+
| 192.168.12.168 | 3563 |
| 192.168.12.168 | 4646 |
+------------------+------+
2 rows in set (0.03 sec)

mysql> create table t13(ip char(16),port int,unique port);
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
mysql> create table t13(ip char(16),port int,unique(port));
Query OK, 0 rows affected (0.32 sec)

mysql> insert into t13 values('192',56),('314',56);
ERROR 1062 (23000): Duplicate entry '56' for key 'port'
mysql> insert into t13 values('192',56),('314',57);
Query OK, 2 rows affected (0.08 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> create table t14(ip char(16),port int primary key(ip,port));
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 '(ip,port))' at line 1
mysql> create table t14(ip char(16),port int, primary key(ip,port));
Query OK, 0 rows affected (0.34 sec)

mysql> select *from t13;
+------------------+------+
| ip | port |
+------------------+------+
| 192 | 56 |
| 314 | 57 |
+------------------+------+
2 rows in set (0.00 sec)

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

mysql> insert into t14 values('192',56),('314',56);
Query OK, 2 rows affected (0.06 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select *from t14;
+------------------+------+
| ip | port |
+------------------+------+
| 192 | 56 |
| 314 | 56 |
+------------------+------+
2 rows in set (0.00 sec)

mysql> desc t14;
+-------+----------+------+-----+------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+------------------+-------+
| ip | char(16) | NO | PRI | | |
| port | int(11) | NO | PRI | 0 | |
+-------+----------+------+-----+------------------+-------+
2 rows in set (0.02 sec)

mysql> desc t13;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| ip | char(16) | YES | | NULL | |
| port | int(11) | YES | UNI | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.02 sec)

mysql> create table t15(ip char(16),ip int,primary key(ip));
ERROR 1060 (42S21): Duplicate column name 'ip'
mysql> create table t15(ip char(16),ip int,primary key ip)
-> ;
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
mysql> create table t15 (ip char(16),ip int,primary key(port);)
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
-> create table t15 (ip char(16),ip int,primary key(port));
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 ')
create table t15 (ip char(16),ip int,primary key(port))' at line 1
mysql> create table t15 (ip char(16),port int,primary key(port));
Query OK, 0 rows affected (0.29 sec)

mysql> inser into t15 values('6779',798),('6779',798);
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 'inser into t15 values('6779',798),('6779',798)' at line 1
mysql> insert into t15 values('6779',798),('6779',798);
ERROR 1062 (23000): Duplicate entry '798' for key 'PRIMARY'
mysql> insert into t15 values('6779',798),('6779',79);
Query OK, 2 rows affected (0.08 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from t14;
+------------------+------+
| ip | port |
+------------------+------+
| 192 | 56 |
| 314 | 56 |
+------------------+------+
2 rows in set (0.00 sec)

mysql> desc t14;
+-------+----------+------+-----+------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+------------------+-------+
| ip | char(16) | NO | PRI | | |
| port | int(11) | NO | PRI | 0 | |
+-------+----------+------+-----+------------------+-------+
2 rows in set (0.02 sec)

mysql> desc t15;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| ip | char(16) | YES | | NULL | |
| port | int(11) | NO | PRI | 0 | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.02 sec)

mysql> create table t16(id int,port int primary key auto_increment);
Query OK, 0 rows affected (0.34 sec)

mysql> insert into t16 values();
Query OK, 1 row affected (0.08 sec)

mysql> insert into t16 values();
Query OK, 1 row affected (0.08 sec)

mysql> insert into t16 values();
Query OK, 1 row affected (0.12 sec)

mysql> desc t16;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(11) | YES | | NULL | |
| port | int(11) | NO | PRI | NULL | auto_increment |
+-------+---------+------+-----+---------+----------------+
2 rows in set (0.02 sec)

mysql> select * from t16;
+------+------+
| id | port |
+------+------+
| NULL | 1 |
| NULL | 2 |
| NULL | 3 |
+------+------+
3 rows in set (0.00 sec)

mysql>

 

 

 

引擎  数据库存取数据方式建表时使用,

show engines;

完整的表结构

create  table 表名 (字段名 类型  (宽度)约束条件)engine =innodb   charset=utf8;

innodb

myisam 效率高不支持外键

blackhole 黑洞数据放入就丢失

memory  重启数据删除

 

 

sql_mode

数据库模式

no_engine_substitution 非安全性 默认

strict_trans_tables  安全模式

show variables like '%sql_mode%' 查看数据库模式 %模糊匹配

set global  sql_mode=‘strict_trance_tables'’ 设置安全模式,需要重新连接 quit;

 

 

 

数据类型

整形  浮点型   字符型  枚举  集合 时间

整形

tinyint  1

smallint  2

mediumint  3字节

int    4

bigint   8

 

 

非安全模式下超出限制也可以存,会丢失一部分;安全模式超出报错

有zerofill 不足用0填充

 

浮点型

float 4字节(255,30)

double 8字节(255,30)

decimal M(65,30)

会丢失精度

 

 

字符型

char 定长

vchar 不定长

超出设置位数非安全模式下超出限制也可以存,会丢失一部分;安全模式超出报错

数据接近时用定长省空间,反之不定长省空间

 

set global sql_mode="strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH

select char_length(x), char_length(y) from 表名

之后 不定长的空间省

char: 一定按规定的宽度存放数据, 以规定宽度读取数据, 通常更占空间

varchar: 首先根据数据长度计算所需宽度, 并在数据开始以数据头方式将宽度信息保存起来, 是一个计算耗时过程, 取先读取宽度信息,以宽度信息为依准读取数据, 通常节省空间

 

时间类型

year:yyyy(1901/2155)

date:yyyy-MM-dd(1000-01-01/9999-12-31)

time:HH:mm:ss

datetime:yyyy-MM-dd HH:mm:ss(1000-01-01 00:00:00/9999-12-31 23:59:59)

timestamp:yyyy-MM-dd HH:mm:ss(1970-01-01 00:00:00/2038-01-19 11:14:08)

time 这种 多加数据丢失,不够报错

 

时间戳设置null 的话默认为元时间,

datetime:时间范围,不依赖当前时区,8字节,可以为null

timestamp:时间范围,依赖当前时区,4字节,有默认值CURRENT_TIMESTAMP

 

枚举与集合

enum:单选

set:多选

枚举单选集合多选

在枚举后定义约束not null  default ‘zf'’枚举必须有值不能为null 如果为()为default 的值

 

约束 键

primary 主键 唯一标识 不设置默认第一个 唯一 不为空 未标识自动创建隐藏字段

foreign key:外键

unique:唯一性数据, 该条字段的值需要保证唯一,不能重复

auto_increment:自增,只能加给key字段辅助修饰

 

not null:不为空

default:默认值 

unsigned:无符号

zerofill:0填充

 

如果联合两个字段,两个字段全相同才相同,否则为不同

 

1.表默认都有主键, 且只能拥有一个主键字段(单列主键 | 联合主键)

# 2.没有设置主键的表, 数据库系统会自上而下将第一个规定为unique not null字段自动提升为primary key主键

# 3.如果整个表都没有unique not null字段且没有primary key字段, 系统会默认创建一个隐藏字段作为主键

# 4.通常必须手动指定表的主键, 一般用id字段, 且id字段一般类型为int, 因为int类型可以auto_increment

create table t21(id int primary key auto_increment); # 自增要结合key,不赋值插入,数据会自动自增, 且自增的结果一直被记录保留

联合主键分开显示pri 联合唯一 key栏合并

posted on 2019-01-08 19:36  paulgeo  阅读(340)  评论(0编辑  收藏  举报