第07讲 mysql的应用案例

来源:互联网 发布:拆分系统源码下载 编辑:程序博客网 时间:2024/06/05 09:46

综合案例:

员工表(employee)

字段 属性 id 整形 name 字符型 sex 字符型或bit型 birthday 日期型 job 字符型 Salary 小数型 resume 大文本型

假如在操作数据库的时候忘了在那个库
那么

mysql> show tables;+-----------------+| Tables_in_db100 |+-----------------+| test100         || test200         || test300         || test400         || test600         || test700         || test800         |+-----------------+7 rows in set (0.00 sec)

如上很明显可以看到。 tables in db100;

创建一个员工表 employee

mysql> create table employee(    -> id int unsigned,    -> name varchar(100),    -> sex char(1),    -> birthday date,    -> job varchar(30),    -> salary decimal (10,2),    -> jieshao text)character set utf8 engine MyISAM;Query OK, 0 rows affected (0.03 sec)

如上是不加默认值的,

mysql> create table employee(    -> id int unsigned,    -> name varchar(100) not null default '',    -> sex char(1) not nul default '',    -> brithday date,    -> job varchar(30) not null default'',    -> salary decimal (10,2) not null default '',    -> jieshao text)character set utf8 engine MyISAM;ERROR 1064 (42000): You have an error in your SQL syntax; check the manual thatcorresponds to your MySQL server version for the right syntax to use near 'nulefault '',brithday date,job varchar(30) not null default'',salary decima' at line 4

如上带上默认值就出问题为啥。

删了刚才创建的库之后重新创建下成功了 应该是没有注意空格啥的:

mysql> drop table employee;Query OK, 0 rows affected (0.00 sec)mysql> create table employee (    -> id int unsigned,    -> name varchar(100) not null default '',    -> sex char(1) not null default '',    -> brithday date,    -> job varchar(30) not null default '',    -> salary decimal(10,2) not null default 0,    -> jieshao text)character set utf8 engine MyISAM;Query OK, 0 rows affected (0.00 sec)

如上成功创建

说明:
- 1、当我们在int 等整形增加了 unsigned后,就是一个无符号数
- 2、我们在创建char 或者 varchar时候默认给一个”
- 3、如果我们使用到了小数,并且要求精度高的话建议使用decimal

阅读全文
0 0