1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #建库
create databases school charset utf8;
#建表
CREATE TABLE stu( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '学号', # NOT NULL PRIMARY KEY:主键非空 AUTO_INCREMENT:自动递增的唯一编号来标识记录。 sname VARCHAR(255) NOT NULL COMMENT '姓名', sage TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '年龄', #DEFAULT 0:表示默认值为0,即当插入数据时如未指定该列的值,则该列的值就自动写入默认值。 sgender ENUM('m','f','n') NOT NULL DEFAULT 'n' COMMENT '性别' , sfz CHAR(18) NOT NULL UNIQUE COMMENT '身份证', intime TIMESTAMP NOT NULL DEFAULT NOW() COMMENT '入学时间' ) ENGINE=INNODB CHARSET=utf8 COMMENT '学生表'; #ENGINE=INNODB:存储引擎使用INNODB
#插入表数据
alter table stu add tel varchar(20) not null unique COMMENT '电话';
#查看已有表的所有字段
mysql> show full columns from stu; + | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment | + | id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | 学号 | | sname | varchar(255) | utf8_general_ci | NO | | NULL | | select,insert,update,references | 姓名 | | sage | tinyint(3) unsigned | NULL | NO | | 0 | | select,insert,update,references | 年龄 | | sgender | enum('m','f','n') | utf8_general_ci | NO | | n | | select,insert,update,references | 性别 | | sfz | char(18) | utf8_general_ci | NO | UNI | NULL | | select,insert,update,references | 身份证 | | intime | timestamp | NULL | NO | | CURRENT_TIMESTAMP | | select,insert,update,references | 入学时间 | | tel | varchar(20) | utf8_general_ci | NO | UNI | NULL | | select,insert,update,references | 电话 | + 7 rows in set (0.00 sec)
#增加数据
mysql> insert into stu(sname,intime,tel,sfz) values('zs1',now(),'110','1'); Query OK, 1 row affected (0.01 sec)
mysql> insert into stu(sname,intime,tel,sfz) values('zs1',now(),'120','2'); Query OK, 1 row affected (0.00 sec)
mysql> insert into stu(sname,intime,tel,sfz) values('zs1',now(),'130','3'); Query OK, 1 row affected (0.00 sec)
#查看数据 mysql> select * from stu; + | id | sname | sage | sgender | sfz | intime | tel | + | 1 | zs1 | 0 | n | 1 | 2021-09-26 14:07:35 | 110 | | 2 | zs1 | 0 | n | 2 | 2021-09-26 14:07:42 | 120 | | 3 | zs1 | 0 | n | 3 | 2021-09-26 14:07:48 | 130 | + 3 rows in set (0.00 sec)
#查看执行计划(没有此数据为NULL) mysql> desc select * from stu where tel='160'; + | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + | 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | no matching row in const table | + 1 row in set, 1 warning (0.00 sec)
|