MySQL实训1

Posted YI瑾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL实训1相关的知识,希望对你有一定的参考价值。

#cmd中打开mysql
mysql -u root -p

1.创建数据库,名称为cdadb;(如果已有,则省略)

create database cdadb;

2.创建数据表customer(客户)、desposite(存款)、bank(银行),表结构如下:
customer的表结构:

属性名称类型与长度中文含义备注
c_idchar(6)客户标识主键,非空
namevarchar(30)客户姓名非空
locationVarchar(30)工作地点
salarydecimal(8,2)工资
#创建表
mysql> create table customer(
    -> c_id char(6) primary key not null,
    -> name varchar(30) not null,
    -> location varchar(30),
    -> salary decimal(8,2));

#显示表结构   
mysql> desc customer;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| c_id     | char(6)      | NO   | PRI | NULL    |       |
| name     | varchar(30)  | NO   |     | NULL    |       |
| location | varchar(30)  | YES  |     | NULL    |       |
| salary   | decimal(8,2) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+

bank的表结构:

属性名称类型与长度中文含义备注
b_idchar(5)银行标识主键,非空
bank_namechar(30)银行名次非空
#创建表
mysql> create table bank(
    -> b_id char(5) primary key not null,
    -> bank_name char(30) not null
    -> );
    
#显示表结构   
mysql> desc bank;
+-----------+----------+------+-----+---------+-------+
| Field     | Type     | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| b_id      | char(5)  | NO   | PRI | NULL    |       |
| bank_name | char(30) | NO   |     | NULL    |       |
+-----------+----------+------+-----+---------+-------+

desposite的表结构:

属性名称类型与长度中文含义备注
d_idint存款流水号主键,非空,自增
c_idchar(6)客户标识外键,关联customer表的c_id
b_idchar(5)银行标识外键,关联bank表的b_id
dep _datedate存入日期
dep_typechar(1)存款期限1,3,5分别代表1年期、3年期和5年期
amountdecimal(8,2)存款金额
mysql> create table desposite(
    ->  d_id int primary key not null auto_increment,
    -> c_id char(6),
    -> b_id char(5),
    -> dep_date date,
    ->  dep_type char(1),
    -> amount decimal(8,2),
    -> constraint de_c_id foreign key(c_id) references customer(c_id),
    -> constraint de_b_id foreign key(b_id) references bank(b_id));

mysql> desc desposite;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| d_id     | int(11)      | NO   | PRI | NULL    | auto_increment |
| c_id     | char(6)      | YES  | MUL | NULL    |                |
| b_id     | char(5)      | YES  | MUL | NULL    |                |
| dep_date | date         | YES  |     | NULL    |                |
| dep_type | char(1)      | YES  |     | NULL    |                |
| amount   | decimal(8,2) | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

总结:
1、创建表

create table 表名(
    字段名1 数据类型 约束,
    字段名2 数据类型 约束,
	...
	字段名n 数据类型 约束
    );

2、展示表结构

desc 表名;

3、外键约束

(constraint 外键约束名) foreign key(本表中的某个字段) references 与本表约束表的表名(与本表约束表的字段)

3.录入数据如下:customer的数据如下,注意最后一条记录用你的学号和你的姓名代替。

c_idnamelocationsalary
101001孙杨广州1234
101002郭海南京3526
101003卢江苏州6892
101004郭惠济南3492
你的学号你的姓名北京6324
mysql> insert into customer(c_id,name,location,salary)
    -> values('101001','孙杨','广州',1234),
    -> ('101002','郭海','南京',3526),
    -> ('101004','郭慧','济南',3492),
    -> ('你的学号','你的名字','北京',6324);

mysql> select * from customer;
+----------+----------+----------+---------+
| c_id     | name     | location | salary  |
+----------+----------+----------+---------+
| 101001   | 孙杨     | 广州     | 1234.00 |
| 101002   | 郭海     | 南京     | 3526.00 |
| 101004   | 郭慧     | 济南     | 3492.00 |
| 你的学号 | 你的名字 | 北京     | 6324.00 |
+----------+----------+----------+---------+

bank的数据如下:

b_idbank_name
B0001工商银行
B0002建设银行
B0003中国银行
B0004农业银行

mysql> insert into bank(b_id,bank_name)
    -> values ('B0001','工商银行'),
    -> ('B0002','建设银行'),
    -> ('B0003','中国银行'),
    -> ('B0004','建设银行');

mysql> select * from bank;
+-------+-----------+
| b_id  | bank_name |
+-------+-----------+
| B0001 | 工商银行  |
| B0002 | 建设银行  |
| B0003 | 中国银行  |
| B0004 | 建设银行  |
+-------+-----------+

desposite的数据如下:

d_idc_idb_iddep_datedep_typeamount
1101001B00012011-04-05342526
2101002B00032012-07-15566500
3101003B00022010-11-24142366
4101004B00042008-03-31162362
5101001B00032002-02-07356346
6101002B00012004-09-233353626
7101003B00042003-12-14536236
8101004B00022007-04-21526267
9101001B00022011-02-111435456
10101002B00042012-05-131234626
11101003B00032001-01-24526243
12101004B00012009-08-23345671
insert into desposite(d_id,c_id,b_id,dep_date,dep_type,amount)
    -> values (1,'101001','B0001','2011-04-05','3',42526),
    -> (2,'101002','B0003','2012-07-15','5',66500),
    -> (3,'101003','B0002','2010-11-24','1',42366),
    -> (4,'101004','B0004','2008-03-31','1',62362),
    -> (5,'101001','B0003','2002-02-07','3',56346),
    -> (6,'101002','B0001','2004-09-23','3',353626),
    -> (7,'101003','B0004','2003-12-14','5',36236),
    -> (8,'101004','B0002','2007-04-21','5',26267),
    -> (9,'101001','B0002','2011-02-11','1',435456),
    -> (10,'101002','B0004','2012-05-13','1',234626),
    -> (11,'101003','B0003','2001-01-24','5',26243),
    -> (12,'101004','B0001','2009-08-23','3',45671);

mysql> select * from desposite;
+------+--------+-------+------------+----------+-----------+
| d_id | c_id   | b_id  | dep_date   | dep_type | amount    |
+------+--------+-------+------------+----------+-----------+
|    1 | 101001 | B0001 | 2011-04-05 | 3        |  42526.00 |
|    2 | 101002 | B0003 | 2012-07-以上是关于MySQL实训1的主要内容,如果未能解决你的问题,请参考以下文章

头歌MySQL数据库实训答案2022

MySQL实训1

记一次Mysql魔鬼实训

MySQL实训3

MySQL实训2

javaweb实训第四天上午——MySQL基础