Demo:
mysql> use school; #选择数据库school
mysql> create table class6(class_id integer(5) zerofill, class_name varchar(128), class_teacher
varchar(64) ); #创建表class6
mysql> insert into class0 values(1,'三年级六班','张老师');
mysql> select * from class0 ;
+-------+------------+---------+
| id | name | teacher |
+-------+------------+---------+
| 00001 | 三年级六班 | 张老师 |
+-------+------------+---------+
1 row in set (0.00 sec)
Demo:
mysql> use school; #选择数据库school
mysql> create table class6(id integer unsigned , name varchar(128), teacher varchar(64) ); #创建表class6
mysql> create table class7(id integer zerofill , name varchar(128), teacher varchar(64) ); #创建表class7, id类型为 int unsigned
mysql> use school; #选择数据库school
mysql> create table class8(id integer auto_increment PRIMARY KEY , name varchar(128), teacher varchar(64) ); #创建表class8, id 具有自增长属性
mysql> create table class9(id integer auto_increment UNIQUE , name varchar(128), teacher varchar(64) ); #创建表class9, id 具有自增长属性
mysql> use school; #选择数据库school
mysql> create table class10 (f1 float, do1 double, de1 decimal); #创建表class10
Demo:
mysql> create table date_example (e_date date, e_datetime datetime, e_timestamp
timestamp, e_time time, e_year year); #创建表dt_example
mysql> insert into date_example values('2020-5-9', '2020-5-9 15:01:00', '2020-05-09 15:01:00', '15:56:01', 2011);
mysql> insert into date_example values('2020_5/9', '2020:5-9 15/01-00', '2020:05/09 15-01_00', '15:56:01', '2011'); #效果同上面插入语句
mysql> select * from date_example;
+------------+---------------------+---------------------+----------+--------+
| e_date | e_datetime | e_timestamp | e_time | e_year |
+------------+---------------------+---------------------+----------+--------+
| 2020-05-09 | 2020-05-09 15:01:00 | 2020-05-09 15:01:00 | 15:56:01 | 2011 |
| 2020-05-09 | 2020-05-09 15:01:00 | 2020-05-09 15:01:00 | 15:56:01 | 2011 |
+------------+---------------------+---------------------+----------+--------+
2 rows in set (0.00 sec)
Demo:
mysql> insert into date_example values(CURDATE(), NOW(), NOW(), time(NOW()), YEAR(NOW()) );
Query OK, 1 row affected (0.10 sec)
mysql> select * from date_example;
+------------+---------------------+---------------------+----------+--------+
| e_date | e_datetime | e_timestamp | e_time | e_year |
+------------+---------------------+---------------------+----------+--------+
| 2020-05-09 | 2020-05-09 15:01:00 | 2020-05-09 15:01:00 | 15:56:01 | 2011 |
| 2020-05-09 | 2020-05-09 15:01:00 | 2020-05-09 15:01:00 | 15:56:01 | 2011 |
| 2020-06-05 | 2020-06-05 22:08:33 | 2020-06-05 22:08:33 | 22:08:33 | 2020 |
+------------+---------------------+---------------------+----------+--------+
3 rows in set (0.00 sec)
mysql> use test; #选择数据库test
mysql> create table char_example(e_char char(5), v_char varchar(5)); #创建数据库表
mysql> insert into char_example values('12345','12345'); #正常插入数据
mysql> insert into char_example values('1 2 ','1 2 '); #char类型会屏蔽后面隐藏的空格,varchar 不会
mysql>select concat('(',e_char, ')'), concat('(',v_char, ')') from char_example ; #让char 后面屏蔽的空格原型毕露
+-------------------------+-------------------------+
| concat('(',e_char, ')') | concat('(',v_char, ')') |
+-------------------------+-------------------------+
| (12345) | (12345) |
| (1 2) | (1 2 ) |
+-------------------------+-------------------------+
2 rows in set (0.01 sec)
mysql> select * from char_example;
+--------+--------+
| e_char | v_char |
+--------+--------+
| 12345 | 12345 |
| 1 2 | 1 2 |
+--------+--------+
2 rows in set (0.00 sec)
mysql> use test; #选择数据库test
mysql> create table text_example(e_text tinytext, v_char varchar(255)); #创建数据库表,e_text 可存储255个字节,v_char可存储255个字符
mysql> insert into char_example values(90个中文字符,90个中文字符); #插入失败,utfmb4 用3个字节表示一个中文汉字,会超出tinytext 保存范围
mysql> insert into char_example values(80个中文字符,100个中文字符); #插入成功
mysql> use test; #选择数据库test
mysql> create table enum_example (e_enum enum('男','女','选择保密') ); #创建表
mysql> insert into enum_example values('男'); #插入记录,必须是enum 选项中的值
mysql> insert into enum_example values(1); #插入记录可以用数值表示
mysql> select * from enum_example;
+--------+
| e_enum |
+--------+
| 男 |
| 女 |
+--------+
2 rows in set (0.00 sec)
mysql>select e_enum + 0 from enum_example; #查询enum 选项对应的整数值
+------------+
| e_enum + 0 |
+------------+
| 1 |
| 2 |
+------------+
2 rows in set (0.01 sec)
Demo:
mysql> create table enum_test(name varchar(64),sex enum('男','女') NOT NULL);
Query OK, 0 rows affected (0.09 sec)
mysql> insert into enum_test values('李白','男');
Query OK, 1 row affected (0.01 sec)
mysql> insert into enum_test values('迪丽热巴',2);
Query OK, 1 row affected (0.00 sec)
mysql> insert into enum_test (name) values('成龙'); #默认为男,即第一个参数
Query OK, 1 row affected (0.01 sec)
mysql> select * from enum_test;
+----------+-----+
| name | sex |
+----------+-----+
| 李白 | 男 |
| 迪丽热巴 | 女 |
| 成龙 | 男 |
+----------+-----+
3 rows in set (0.00 sec)
mysql> use test; #选择数据库test
mysql> create table set_example (interest set('足球','追剧','篮球','撩妹') ); #创建表
mysql> insert into set_example values( '足球,撩妹' ); #插入记录,必须是enum 选项中的值
mysql> insert into set_example values(9); #插入相应位效果等同,9 =>1001 选择1,4
mysql> insert into set_example values(5); #5 =>0101 选择1,3.
mysql> select * from set_example;
+-----------+
| interest |
+-----------+
| 足球,撩妹 |
| 足球,撩妹 |
| 足球,篮球 |
+-----------+
3 rows in set (0.00 sec)
mysql> select interest+0 from set_example; #以整数的方式查询
+------------+
| interest+0 |
+------------+
| 9 |
| 9 |
| 5 |
+------------+
3 rows in set (0.00 sec)
创建一个用户信息表,用来存储用户的如下信息:姓名,手机号码,家庭地址,个人简介,性别,年龄,身份证号,喜欢的球类。数据库表名可定义为userinfo, 同时增加一个列id 用来作为每一条记录的唯一标识,并设为主键,自加!
mysql> create table userinfo(
-> id int(11) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT'主键',
-> name varchar(64) NOT NULL COMMENT '姓名',
-> mobile char(11) DEFAULT NULL COMMENT '手机号码',
-> address varchar(128) DEFAULT NULL COMMENT '居住地址',
-> description text DEFAULT NULL COMMENT '个人简介-不知道具体的范围,不常更新用text',
-> sex enum('男','女') NOT NULL COMMENT '性别-男或女',
-> age tinyint unsigned DEFAULT 0 COMMENT '年龄',
-> idno char(18) NOT NULL COMMENT '身份证号码',
-> favoriteballs set('篮球','排球','足球','网球','乒乓球','高尔夫球') DEFAULT NULL COMMENT '喜欢的球类'
-> );
Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql> DESC USERINFO;
+---------------+------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------------------------------------------+------+-----+---------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(64) | NO | | NULL | |
| mobile | char(11) | YES | | NULL | |
| address | varchar(128) | YES | | NULL | |
| description | text | YES | | NULL | |
| sex | enum('男','女') | NO | | NULL | |
| age | tinyint unsigned | YES | | 0 | |
| idno | char(18) | NO | | NULL | |
| favoriteballs | set('篮球','排球','足球','网球','乒乓球','高尔夫球') | YES | | NULL | |
+---------------+------------------------------------------------------+------+-----+---------+----------------+
9 rows in set (0.15 sec)
mysql> show create table userinfo;
+----------+----------+----------------------------------------------------+
| Table | Create Table |
+----------+----------+----------------------------------------------------+
| userinfo | CREATE TABLE `userinfo` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(64) NOT NULL COMMENT '姓名',
`mobile` char(11) DEFAULT NULL COMMENT '手机号码',
`address` varchar(128) DEFAULT NULL COMMENT '居住地址',
`description` text COMMENT '个人简介-不知道具体的范围,不常更新用text',
`sex` enum('男','女') NOT NULL COMMENT '性别-男或女',
`age` tinyint unsigned DEFAULT '0' COMMENT '年龄',
`idno` char(18) NOT NULL COMMENT '身份证号码',
`favoriteballs` set('篮球','排球','足球','网球','乒乓球','高尔夫球') DEFAULT NULL COMMENT '喜欢的球类',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+----------------------------------------------------+
1 row in set (0.02 sec)
mysql> insert into userinfo values(1,'周润发','13548672416','香港','我是赌神!!!','男','39','45098119781001****','乒乓球,高尔夫球,篮球');
Query OK, 1 row affected (0.01 sec)
mysql> insert into userinfo (name,mobile,address,description,age,idno,favoriteballs) values(
-> '周星驰','13317681517','香港','我是赌圣!!!','41','45098119770821****','网球,足球,排球');
Query OK, 1 row affected (0.01 sec)
mysql> insert into userinfo (name,mobile,address,description,sex,age,idno,favoriteballs) values(
-> '朱茵','13317681517','香港','我是赌圣的绯闻女友!!!','女','36','45098119790516****','网球,排球');
Query OK, 1 row affected (0.01 sec)
mysql> select * from userinfo;
+----+--------+-------------+---------+--------------------------+-----+------+--------------------+----------------------+
| id | name | mobile | address | description | sex | age | idno | favoriteballs |
+----+--------+-------------+---------+--------------------------+-----+------+--------------------+----------------------+
| 1 | 周润发 | 13548672416 | 香港 | 我是赌神!!! | 男 | 39 | 45098119781001**** | 篮球,乒乓球,高尔夫球 |
| 2 | 周星驰 | 13317681517 | 香港 | 我是赌圣!!! | 男 | 41 | 45098119770821**** | 排球,足球,网球 |
| 3 | 朱茵 | 13317681517 | 香港 | 我是赌圣的绯闻女友!!! | 女 | 36 | 45098119790516**** | 排球,网球 |
+----+--------+-------------+---------+--------------------------+-----+------+--------------------+----------------------+
3 rows in set (0.00 sec)