引擎: 决定数据库存取数据的方式 = 不同的特点 = 不同的用户体验 数据类型: 规定了数据库可以存放哪些数据 约束: 限制存储数据的规则 键...
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欄合并
轉載于:https://www.cnblogs.com/wrqysrt/p/10241010.html
總結
以上是生活随笔為你收集整理的引擎: 决定数据库存取数据的方式 = 不同的特点 = 不同的用户体验 数据类型: 规定了数据库可以存放哪些数据 约束: 限制存储数据的规则 键...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 红牛饮料有何区别,哪种更适合自己喝
- 下一篇: 面团里加入酵母和黄油放进冰箱发酵可不可以