MySQL第一次作业

打印 上一主题 下一主题

主题 993|帖子 993|积分 2979

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x

一、库的创建

  1. mysql> show databases;
  2. +--------------------+
  3. | Database           |
  4. +--------------------+
  5. | information_schema |
  6. | mydb2_stuinfo      |
  7. | mydb3_employee     |
  8. | mydb4_product      |
  9. | mydbl_test         |
  10. | mysql              |
  11. | performance_schema |
  12. | sys                |
  13. | temp2              |
  14. +--------------------+
  15. 9 rows in set (0.00 sec)
  16. mysql> create database mydb6_product;
  17. Query OK, 1 row affected (0.01 sec)
  18. mysql> show databases;
  19. +--------------------+
  20. | Database           |
  21. +--------------------+
  22. | information_schema |
  23. | mydb2_stuinfo      |
  24. | mydb3_employee     |
  25. | mydb4_product      |
  26. | mydb6_product      |
  27. | mydbl_test         |
  28. | mysql              |
  29. | performance_schema |
  30. | sys                |
  31. | temp2              |
  32. +--------------------+
  33. 10 rows in set (0.00 sec)
复制代码

二、创建employees表  (有主键,非空,默认值的要求)


  1. mysql> create table employees(
  2.     -> id int primary key,
  3.     -> name varchar(50) not null,
  4.     -> age int,
  5.     -> gender varchar(10) not null default 'unknown',
  6.     -> salary float);
  7. Query OK, 0 rows affected (0.03 sec)
  8. mysql> desc employees;
  9. +--------+-------------+------+-----+---------+-------+
  10. | Field  | Type        | Null | Key | Default | Extra |
  11. +--------+-------------+------+-----+---------+-------+
  12. | id     | int         | NO   | PRI | NULL    |       |
  13. | name   | varchar(50) | NO   |     | NULL    |       |
  14. | age    | int         | YES  |     | NULL    |       |
  15. | gender | varchar(10) | NO   |     | unknown |       |
  16. | salary | float       | YES  |     | NULL    |       |
  17. +--------+-------------+------+-----+---------+-------+
  18. 5 rows in set (0.00 sec)
复制代码

三、创建 orders表 (有主键和非空的要求)


  1. mysql> create table orders(
  2.     -> id int primary key,
  3.     -> name varchar(100) not null,
  4.     -> price float,
  5.     -> quantity int,
  6.     -> category varchar(50));
  7. Query OK, 0 rows affected (0.03 sec)
  8. mysql> desc orders;
  9. +----------+--------------+------+-----+---------+-------+
  10. | Field    | Type         | Null | Key | Default | Extra |
  11. +----------+--------------+------+-----+---------+-------+
  12. | id       | int          | NO   | PRI | NULL    |       |
  13. | name     | varchar(100) | NO   |     | NULL    |       |
  14. | price    | float        | YES  |     | NULL    |       |
  15. | quantity | int          | YES  |     | NULL    |       |
  16. | category | varchar(50)  | YES  |     | NULL    |       |
  17. +----------+--------------+------+-----+---------+-------+
  18. 5 rows in set (0.00 sec)
复制代码

四、创建invoices表 (有主键自增长,外键关联,数值范围的要求)


  1. mysql> create table invoices(
  2.     -> number int primary key,
  3.     -> order_id int,foreign key (order_id) references orders(id),
  4.     -> in_date date,
  5.     -> total_amount float ,check(total_amount>0));
  6. Query OK, 0 rows affected (0.04 sec)
  7. mysql> alter table invoices modify number int auto_increment;
  8. Query OK, 0 rows affected (0.06 sec)
  9. Records: 0  Duplicates: 0  Warnings: 0
  10. mysql> desc invoices;
  11. +--------------+-------+------+-----+---------+----------------+
  12. | Field        | Type  | Null | Key | Default | Extra          |
  13. +--------------+-------+------+-----+---------+----------------+
  14. | number       | int   | NO   | PRI | NULL    | auto_increment |
  15. | order_id     | int   | YES  | MUL | NULL    |                |
  16. | in_date      | date  | YES  |     | NULL    |                |
  17. | total_amount | float | YES  |     | NULL    |                |
  18. +--------------+-------+------+-----+---------+----------------+
  19. 4 rows in set (0.00 sec)
复制代码











免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美丽的神话

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表