项目三,创建和管理数据表

在继续学习之前,先创建一个库,创建表,导入数据,方便后续的继续学习。

复制以下代码直接运行,会得到一个db_shop的库,已经有相关数据了。

推荐在Navicat里创建运行

安装版,附上教程

链接:https://pan.baidu.com/s/1X2iGhnKrEE98tfdnzAn-rQ?pwd=walq 提取码:walq

/*
 Navicat Premium Data Transfer
​
 Source Server         : MySQL8.0.15
 Source Server Type    : MySQL
 Source Server Version : 80015
 Source Host           : localhost:3306
 Source Schema         : db_shop
​
 Target Server Type    : MySQL
 Target Server Version : 80015
 File Encoding         : 65001
​
 Date: 05/03/2021 21:32:55
添加了中文字符编码
*/
CREATEDATABASE db_shop DEFAULTCHARACTERSET gbk COLLATE gbk_chinese_ci;
​
USE db_shop;
​
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS =0;
​
-- ----------------------------
-- Table structure for department
-- ----------------------------
DROPTABLEIFEXISTS`department`;
CREATETABLE`department`  (
  `id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'部门编号(定义主键)',
  `dept_name`varchar(20)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOTNULLCOMMENT'部门名称(定义唯一键)',
  `dept_phone`char(13)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'部门电话',
  `dept_memo`varchar(100)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'备注',
  PRIMARYKEY(`id`)USINGBTREE,
  UNIQUEINDEX`dept_name`(`dept_name`)USINGBTREE
)ENGINE=InnoDBAUTO_INCREMENT=8CHARACTERSET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci COMMENT='部门信息'ROW_FORMAT= Dynamic;
​
-- ----------------------------
-- Records of department
-- ----------------------------
INSERTINTO`department`VALUES(1,'总经办','020','总经理');
INSERTINTO`department`VALUES(2,'销售部','020-87991234','负责销售');
INSERTINTO`department`VALUES(3,'采购部','13661256821','负责商品采购');
INSERTINTO`department`VALUES(4,'客服部','020-87993095',NULL);
INSERTINTO`department`VALUES(5,'技术部','020-87993692',NULL);
INSERTINTO`department`VALUES(6,'仓管部','020-87993691',NULL);
INSERTINTO`department`VALUES(7,'维修部','13612312625',NULL);
INSERTINTO`department`VALUES(8,'公关部','020-38128912',NULL);
​
​
-- ----------------------------
-- Table structure for supplier
-- ----------------------------
DROPTABLEIFEXISTS`supplier`;
CREATETABLE`supplier`  (
  `id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'主键',
  `supplier_name`varchar(100)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOTNULLCOMMENT'供应商名称',
  `phone`char(11)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'手机号',
  PRIMARYKEY(`id`)USINGBTREE,
  UNIQUEINDEX`supplier_name`(`supplier_name`)USINGBTREE
)ENGINE=InnoDBAUTO_INCREMENT=7CHARACTERSET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci COMMENT='供应商信息'ROW_FORMAT= Dynamic;
​
-- ----------------------------
-- Records of supplier
-- ----------------------------
INSERTINTO`supplier`VALUES(1,'北京路城南方天贸','13138490819');
INSERTINTO`supplier`VALUES(2,'农历下路广百天怡','13132312631');
INSERTINTO`supplier`VALUES(3,'怡宝','13132491935');
INSERTINTO`supplier`VALUES(4,'珠江牌','13321490728');
INSERTINTO`supplier`VALUES(5,'广州李锦记','13736490938');
INSERTINTO`supplier`VALUES(6,'农夫山泉','13135490231');
INSERTINTO`supplier`VALUES(7,'味极鲜','020-3890123');
​
​
​
-- ----------------------------
-- Table structure for goods_type
-- ----------------------------
DROPTABLEIFEXISTS`goods_type`;
CREATETABLE`goods_type`  (
  `id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'主键',
  `name`varchar(50)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOTNULLCOMMENT'商品类型名称',
  `memo`varchar(300)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'备注',
  PRIMARYKEY(`id`)USINGBTREE
)ENGINE=InnoDBAUTO_INCREMENT=3CHARACTERSET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci COMMENT='商品类型信息表'ROW_FORMAT= Dynamic;
​
-- ----------------------------
-- Records of goods_type
-- ----------------------------
INSERTINTO`goods_type`VALUES(1,'饮用水','单支出售');
INSERTINTO`goods_type`VALUES(2,'牛奶','');
INSERTINTO`goods_type`VALUES(3,'酱油','');
​
​
-- ----------------------------
-- Table structure for goods
-- ----------------------------
DROPTABLEIFEXISTS`goods`;
CREATETABLE`goods`  (
  `id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'主键',
  `goods_name`varchar(50)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOTNULLCOMMENT'商品名称',
  `supplier_id`int(11)NOTNULLCOMMENT'供应商标识',
  `goods_type`char(20)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'商品类型',
  `banner`varchar(255)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'商品图片',
  `introduce`varchar(255)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'商品介绍',
  `unit_price`decimal(11,2)UNSIGNEDNULLDEFAULT0.00COMMENT'单价',
  `amount`int(10)UNSIGNEDNULLDEFAULT0COMMENT'数量',
  `goods_memo`varchar(300)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'备注',
  PRIMARYKEY(`id`)USINGBTREE,
  INDEX`supplier_id`(`supplier_id`)USINGBTREE,
  CONSTRAINT`goods_ibfk_1`FOREIGNKEY(`supplier_id`)REFERENCES`supplier`(`id`)ONDELETERESTRICTONUPDATERESTRICT
)ENGINE=InnoDBAUTO_INCREMENT=9CHARACTERSET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci COMMENT='商品信息表'ROW_FORMAT= Dynamic;
​
-- ----------------------------
-- Records of goods
-- ----------------------------
INSERTINTO`goods`VALUES(1,'普通酱油',1,'酱油',NULL,'甜',12.30,51,'海天');
INSERTINTO`goods`VALUES(2,'顶级酱油',1,'酱油',  NULL,'鲜甜',22.30,14,'开平味极鲜');
INSERTINTO`goods`VALUES(3,'顶级生抽',5,'酱油',  NULL,'甜味',21.00,76,'李锦记');
INSERTINTO`goods`VALUES(4,'精品老抽',4,'酱油',NULL,'味道可口,盐',12.10,93,'珠江牌');
INSERTINTO`goods`VALUES(5,'哇哈哈矿泉水',2,'饮用水',NULL,'清甜',2.30,96,'哇哈哈公司');
INSERTINTO`goods`VALUES(6,'塞上牧场纯牛奶',1,'牛奶',NULL,'香甜',1.50,96,'塞上牧场');
INSERTINTO`goods`VALUES(7,'动力水',2,'饮用水',NULL,'动力源',6.50,98,'动脉');
INSERTINTO`goods`VALUES(8,'蒙牛',6,'牛奶',NULL,'有机牛奶',3.00,8,'特仑苏品牌');
INSERTINTO`goods`VALUES(9,'怡宝矿泉水',3,'饮用水',NULL,'矿泉水',2.00,95,'怡宝公司');
​
-- ----------------------------
-- Table structure for customer
-- ----------------------------
DROPTABLEIFEXISTS`customer`;
CREATETABLE`customer`  (
  `id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'主键',
  `username`varchar(32)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOTNULLCOMMENT'账号',
  `password`varchar(32)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOTNULLCOMMENT'密码',
  `customer_name`varchar(10)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOTNULLCOMMENT'顾客姓名',
  `sex`enum('F','M')CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULT'F'COMMENT'性别',
  `birthday`dateNULLDEFAULTNULLCOMMENT'生日日期',
  `hobby`set('ball','art','music')CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'兴趣',
  `consumption_amount`decimal(11,2)UNSIGNEDNULLDEFAULT0.00COMMENT'消费金额',
  `menber_balance`decimal(11,2)UNSIGNEDNULLDEFAULT0.00COMMENT'会员余额',
  `photo`varbinary(250)NULLDEFAULTNULLCOMMENT'照片',
  `email`varchar(50)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'邮箱',
  `address` json NULLCOMMENT'地址',
  PRIMARYKEY(`id`)USINGBTREE,
  UNIQUEINDEX`username`(`username`)USINGBTREE
)ENGINE=InnoDBAUTO_INCREMENT=19CHARACTERSET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci COMMENT='顾客信息表'ROW_FORMAT= Dynamic;
​
-- ----------------------------
-- Records of customer
-- ----------------------------
INSERTINTO`customer`VALUES(1,'10','10','he','F','2020-02-04','art',0.00,0.00,NULL,'','{\"city\": \"广州\", \"road\": \"天天寿路\", \"province\": \"广东省\"}');
INSERTINTO`customer`VALUES(2,'LH','123','李红','F','2000-09-01','ball,art',5000.00,0.00,NULL,NULL,'{\"city\": \"广州\", \"road\": \"黄埔\", \"room\": 16}');
INSERTINTO`customer`VALUES(3,'CHL','123','陈红玲','F','1995-06-01','art',1200.00,0.00,NULL,NULL,'{\"city\": \"潮州\", \"road\": \"朝阳街\", \"room\": \"9\"}');
INSERTINTO`customer`VALUES(13,'zhangs','123','张三','F',NULL,NULL,0.00,0.00,NULL,NULL,'{\"city\": \"广州\", \"road\": \"天河比\", \"room\": 5}');
INSERTINTO`customer`VALUES(14,'LiS','123','李四','F',NULL,NULL,0.00,0.00,NULL,NULL,'{\"city\": \"广州\", \"road\": \"林和街\", \"room\": 10}');
INSERTINTO`customer`VALUES(15,'he','123456','何彬彬','M','2021-01-05','ball,art',143.30,0.00,NULL,'xy@163.com','{\"city\": \"广州\", \"road\": \"林和\", \"province\": \"广东省\"}');
INSERTINTO`customer`VALUES(16,'he2','123456','何小','M','2021-01-06','art',0.00,0.00,NULL,'','{\"city\": \"广州\", \"road\": \"石牌\", \"province\": \"广东省\"}');
INSERTINTO`customer`VALUES(17,'hh','123456','周颜色','F','2020-09-30','ball,art',62.50,0.00,NULL,'hh123@163.com','{\"city\": \"广州\", \"road\": \"广州石牌\", \"province\": \"广东省\"}');
INSERTINTO`customer`VALUES(18,'wu','123456','王五','F','2020-12-31','ball,art',0.00,0.00,NULL,'wu123@163.com','{\"city\": \"广州\", \"road\": \"广州林和\", \"province\": \"广东省\"}');
INSERTINTO`customer`VALUES(19,'kk','123456','凯凯','F','2019-12-04','ball,art',52.00,0.00,NULL,'hh123@163.com','{\"city\": \"广州\", \"road\": \"广州林和\", \"province\": \"广东省\"}');
​
-- ----------------------------
-- Table structure for orders
-- ----------------------------
DROPTABLEIFEXISTS`orders`;
CREATETABLE`orders`  (
  `id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'主键',
  `customer_id`int(11)NOTNULLCOMMENT'顾客标识',
  `create_time`datetime(0)NOTNULLCOMMENT'订单生成时间',
  `amount`decimal(11,2)UNSIGNEDNOTNULLDEFAULT0.00COMMENT'订单应付金额',
  `paid_day`datetime(0)NULLDEFAULTNULLCOMMENT'支付时间',
  `status`int(11)NOTNULLCOMMENT'支付状态',
  `memo`varchar(100)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'备注',
  PRIMARYKEY(`id`)USINGBTREE,
  INDEX`customer_id`(`customer_id`)USINGBTREE,
  CONSTRAINT`orders_ibfk_1`FOREIGNKEY(`customer_id`)REFERENCES`customer`(`id`)ONDELETERESTRICTONUPDATERESTRICT
)ENGINE=InnoDBAUTO_INCREMENT=11CHARACTERSET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci COMMENT='订单项目表'ROW_FORMAT= Dynamic;
​
-- ----------------------------
-- Records of orders
-- ----------------------------
INSERTINTO`orders`VALUES(1,2,'2018-09-01 00:00:00',234.50,NULL,0,NULL);
INSERTINTO`orders`VALUES(2,2,'2019-06-11 00:00:00',271.50,NULL,0,NULL);
INSERTINTO`orders`VALUES(3,3,'2019-08-01 00:00:00',111.50,NULL,0,NULL);
INSERTINTO`orders`VALUES(4,3,'2019-10-01 00:00:00',61.50,NULL,0,NULL);
INSERTINTO`orders`VALUES(5,15,'2021-01-13 14:52:53',45.20,'2021-01-13 14:53:00',1,'');
INSERTINTO`orders`VALUES(6,15,'2021-01-13 15:00:09',29.20,'2021-01-13 15:00:13',1,'');
INSERTINTO`orders`VALUES(7,15,'2021-01-13 15:09:09',68.90,'2021-01-13 15:09:18',1,'');
INSERTINTO`orders`VALUES(8,17,'2021-01-13 15:24:50',29.20,'2021-01-13 15:24:54',1,'');
INSERTINTO`orders`VALUES(9,17,'2021-01-13 15:25:19',33.30,'2021-01-13 15:25:23',1,'');
INSERTINTO`orders`VALUES(10,19,'2021-01-13 18:12:28',32.70,'2021-01-13 18:12:34',1,'');
INSERTINTO`orders`VALUES(11,19,'2021-01-13 18:13:06',19.30,'2021-01-13 18:13:09',1,'');
​
-- ----------------------------
-- Table structure for item
-- ----------------------------
DROPTABLEIFEXISTS`item`;
CREATETABLE`item`  (
  `id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'主键',
  `order_id`int(11)NULLDEFAULTNULLCOMMENT'订单标识',
  `goods_id`int(11)NULLDEFAULTNULLCOMMENT'商品标识',
  `supplier_id`int(11)NULLDEFAULTNULLCOMMENT'供应商标识',
  `quantity`int(11)NULLDEFAULTNULLCOMMENT'单个订单商品数量',
  `total_price`decimal(11,2)UNSIGNEDNULLDEFAULT0.00COMMENT'单个订单商品总额',
  PRIMARYKEY(`id`)USINGBTREE,
  INDEX`goods_id`(`goods_id`)USINGBTREE,
  INDEX`supplier_id`(`supplier_id`)USINGBTREE,
  INDEX`order_id`(`order_id`)USINGBTREE,
  CONSTRAINT`item_ibfk_1`FOREIGNKEY(`goods_id`)REFERENCES`goods`(`id`)ONDELETERESTRICTONUPDATERESTRICT,
  CONSTRAINT`item_ibfk_2`FOREIGNKEY(`supplier_id`)REFERENCES`supplier`(`id`)ONDELETERESTRICTONUPDATERESTRICT,
  CONSTRAINT`item_ibfk_3`FOREIGNKEY(`order_id`)REFERENCES`orders`(`id`)ONDELETERESTRICTONUPDATERESTRICT
)ENGINE=InnoDBAUTO_INCREMENT=33CHARACTERSET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci COMMENT='订单详细表'ROW_FORMAT= Dynamic;
​
-- ----------------------------
-- Records of item
-- ----------------------------
INSERTINTO`item`VALUES(4,1,1,1,10,123.00);
INSERTINTO`item`VALUES(5,1,2,1,5,111.50);
INSERTINTO`item`VALUES(6,2,1,3,5,61.50);
INSERTINTO`item`VALUES(7,2,3,2,10,210.00);
INSERTINTO`item`VALUES(8,3,2,2,5,111.50);
INSERTINTO`item`VALUES(9,4,1,3,5,61.50);
INSERTINTO`item`VALUES(10,1,2,1,10,0.00);
INSERTINTO`item`VALUES(11,1,2,1,17,0.00);
INSERTINTO`item`VALUES(13,2,1,2,8,0.00);
INSERTINTO`item`VALUES(14,5,4,4,2,24.20);
INSERTINTO`item`VALUES(15,5,3,5,1,21.00);
INSERTINTO`item`VALUES(16,6,4,4,2,24.20);
INSERTINTO`item`VALUES(17,6,6,1,2,3.00);
INSERTINTO`item`VALUES(18,6,9,3,1,2.00);
INSERTINTO`item`VALUES(19,7,3,5,2,42.00);
INSERTINTO`item`VALUES(20,7,2,1,1,22.30);
INSERTINTO`item`VALUES(21,7,5,2,2,4.60);
INSERTINTO`item`VALUES(22,8,4,4,2,24.20);
INSERTINTO`item`VALUES(23,8,6,1,2,3.00);
INSERTINTO`item`VALUES(24,8,9,3,1,2.00);
INSERTINTO`item`VALUES(25,9,3,5,1,21.00);
INSERTINTO`item`VALUES(26,9,1,1,1,12.30);
INSERTINTO`item`VALUES(27,10,4,4,1,12.10);
INSERTINTO`item`VALUES(28,10,1,1,1,12.30);
INSERTINTO`item`VALUES(29,10,8,6,2,6.00);
INSERTINTO`item`VALUES(30,10,5,2,1,2.30);
INSERTINTO`item`VALUES(31,11,9,3,2,4.00);
INSERTINTO`item`VALUES(32,11,7,2,2,13.00);
INSERTINTO`item`VALUES(33,11,5,2,1,2.30);
​
SET FOREIGN_KEY_CHECKS =1;

运行后查看有哪些库了

SHOWDATABASES;

一、查看数据表与数据类型

1.选择mysql这个库,查看这个库有哪些表

USE mysql;
SHOWTABLES;

2.查看系统数据库mysql中的user表结构定义信息

USE mysql;
DESCuser;

Field:表示表的字段名称;

Type:表示字段值的数据类型;

Null:表示字段值是否允许空值;

Key:表示字段是否为键,比如主键,外键,唯一键;

Default:表示默认值;

Extra:表示其他额外信息,比如自增字段标识等。

3.查看mysql系统数据库的user表定义脚本

USE mysql;
SHOWCREATETABLEuser\G

4.查看MySQL数据类型

查看MySQL支持的所有数据类型名称

HELPDATA TYPES;

4.1查看INT数据类型

可以查看到类型说明和存储数值范围

HELPINT;

5.查看db_shop数据库的department表的全部记录(以下是使用Navicat软件进行运行)

USE db_shop;
SELECT*FROM department;

二、数据类型

1.数字类型

2.字符串类型

3.日期和时间类型

4.JSON类型

三、数据表和键

1.数据表

2.主键

3.唯一键

4.外键

四、关系数据完整性约束

1.域完整性

2.实体完整性

3.参照完整性

4.用户定义的完整性

五、创建数据表,建立数据表和数据完整性

1.CREATE TABLE 语句

2.设置表的主键和唯一键

3.设置外键、检查约束和默认值约束

4.设置表的存储引擎、字符集

5.使用字符串特殊类型和JSON类型

六、复制数据表

1.LIKE复制表结构

USE db_shop;
CREATETABLEIFNOTEXISTS customer_cp LIKE customer;

复制出来的表结构与原表一模一样,完整性约束也相同,但里面的数据不会被复制。可以通过下面语句来查询数据表和表结构。

SHOWTABLES;
DESC customer_cp;

2.AS复制表结构和记录

此语法可复制表结构字段定义及其检索出来的记录,但不复制主键、索引、自动编号等。

该语句相当于把检索出来的记录作为一个新表保存。

USE db_shop;
CREATETABLEIFNOTEXISTS customer_cp2 ASSELECT*FROM customer;

可以看到已经复制的新表结构与原来的一致,但没有复制到完整性约束和自增字段设置,并且AS检索记录也被复制过来。查看语句如下

SHOWTABLES;
DESC customer_CP2;
SELECT*FROM customse_cp2;

七、修改数据表

1.ALTER TABLE语句用来修改数据表的定义和数据完整性约束信息

2.添加表字段

FIRST|AFTER [字段名] 表示添加的字段需要放到表的哪一列的前面或者后面,省略则添加到表的最后,列的顺序没有关系,显示顺序可以用后面学习的检索语句来实现。

当表中已经有记录时,添加的字段必须是允许空的。

USE db_shop;
ALTERTABLE customer_cp2 ADD name1 DATENULL;

使用DESC customer_cp2;来查看增加的字段

3.修改字段类型

在customer_cp2表上修改字段birthday的类型为datetime(6)

AFTER hobby,修改后放到hobby字段的后面

USE db_shop;
ALTERTABLE customer_cp2 MODIFY birthday datetime(6)AFTER hobby;
DESC customer_cp2;

4.修改自增类型字段

说明:自增字段的初始值默认是1,如果不是1,则可以在创建时设置初始值或通过修改表自增值

ALTER TABLE customer_cp2 AUTO_INCREMENT=初始值;

USE db_shop;
ALTERTABLE customer_cp2 AUTO_INCREMENT=100501;

5.添加字段约束条件

给customer的复制表customer_cp2添加主键

USE db_shop;
ALTERTABLE customer_cp2 ADDPRIMARYKEY(id);
DESC customer_cp2;

5.1给customer的复制表customer_cp2的username添加唯一键

USE db_shop;
ALTERTABLE customer_cp2 ADDUNIQUE(username);
DESC customer_cp2;

5.2给customer的复制表customer_cp2的name1添加外键(customer_cp的id)

USE db_shop;
ALTERTABLE customer_cp2 ADDFOREIGNKEY(name1)REFERENCES customer_cp(id);
DESC customer_cp2;

设置外键的时候需要注意以下几点:(1)外键是用于两个表的数据之间建立连接,可以是一列或者多列,即一个表可以有一个或多个外键。

(2)这个表里面设置的外键必须是另外一个表的主键!

(3)外键可以不是这个表的主键,但必须和另外一个表的主键相对应(字段的类型和值必须一样)。

(4)带有主键的那张表称为父表,含外键的是子表,必须先删除外键约束才能删除父表。

(5)另一个表的主键没有空值,必须是主键,这个表的外键跟另一个表的主键数据类型要一样。

6.删除字段约束

删除customer_cp2表的主键

USE db_shop;
ALTERTABLE customer_cp2 DROPPRIMARYKEY;
DESC customer_cp2;

6.1删除customer_cp2表的唯一键

第一步:查看表定义语句,找到唯一键的索引名。

这里在终端里运行,因为navicat显示不完全,navicat不用+\G,直接使用;号

SHOWCREATETABLE customer_cp2 \G

这里可以看到唯一键,主键跟外键

第二步:使用索引名删除唯一键设置

ALTERTABLE customer_cp2 DROPKEY username;
DESC customer_cp2;

6.2删除customer_cp2表的外键

第一步:查看customer_cp2的信息

SHOWCREATETABLE customer_cp2\G

第二步:通过外键标识删除外键(删除外键标识)

ALTERTABLE customer_cp2 DROPFOREIGNKEY customer_cp2_ibfk_1;

第三步:删除外键上建立的索引(KEY)(删除外键字段的外键)

DESC customer_cp2;
ALTERTABLE customer_cp2 DROPKEY name1;
DESC customer_cp2;

7.删除表字段

删除customer_cp2表中的name1字段

USE db_shop;
ALTERTABLE customer_cp2 DROPCOLUMN name1;
DESC customer_cp2;

8.修改表的存储引擎

USE db_shop;
ALTERTABLE customer_cp2 ENGINE=MyISAM;
SHOWCREATETABLE customer_cp2\G

改回去

USE db_shop;
ALTERTABLE customer_cp2 ENGINE=InnoDB;
SHOWCREATETABLE customer_cp2\G

9.修改表的字符集

修改customer_cp2表的字符集为GBK

USE db_shop;
ALTERTABLE customer_cp2 DEFAULTCHARSET= GBK;
SHOWCREATETABLE customer_cp2\G

八、删除数据表

1.删除db_shop数据库中的表customer_cp2

USE db_shop;
DROPTABLE customer_cp2;

#方法二:DROP TABLE IF EXISTS customer_cp2;

九、任务实施

1.完成db_shopping数据库的部门表、职员表的创建

/*
 Navicat Premium Data Transfer
​
 Source Server         : 33
 Source Server Type    : MySQL
 Source Server Version : 80028
 Source Host           : localhost:3306
 Source Schema         : db_shopping
​
 Target Server Type    : MySQL
 Target Server Version : 80028
 File Encoding         : 65001
​
 Date: 24/04/2022 09:22:41
*/
​
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS =0;
​
-- ----------------------------
-- Table structure for department
-- ----------------------------
DROPTABLEIFEXISTS`department`;
CREATETABLE`department`  (
  `id`int(0)NOTNULLAUTO_INCREMENTCOMMENT'部门编号(定义主键)',
  `dept_name`varchar(20)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOTNULLCOMMENT'部门名称(定义唯一键)',
  `dept_phone`char(13)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'部门电话',
  `dept_memo`varchar(100)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'备注',
  PRIMARYKEY(`id`)USINGBTREE,
  UNIQUEINDEX`dept_name`(`dept_name`)USINGBTREE
)ENGINE=InnoDBCHARACTERSET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci COMMENT='部门信息'ROW_FORMAT= Dynamic;
​
-- ----------------------------
-- Records of department
-- ----------------------------
​
-- ----------------------------
-- Table structure for staffer
-- ----------------------------
DROPTABLEIFEXISTS`staffer`;
CREATETABLE`staffer`  (
  `id`int(0)NOTNULLAUTO_INCREMENTCOMMENT'职员编号',
  `staff_name`varchar(10)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOTNULLCOMMENT'职员姓名',
  `dept_id`int(0)NOTNULLCOMMENT'部门编号',
  `sex`enum('F','M')CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULT'F'COMMENT'性别',
  `birthday`dateNULLDEFAULTNULLCOMMENT'生日日期',
  `phone`char(11)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'电话',
  `salary`decimal(8,2)UNSIGNEDNULLCOMMENT'薪水',
  `staff_memo`varchar(100)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'职员备注',
  PRIMARYKEY(`id`)USINGBTREE,
  UNIQUEINDEX`staff_name`(`staff_name`)USINGBTREE,
  INDEX`dept_id`(`dept_id`)USINGBTREE,
  CONSTRAINT`staffer_ibfk_1`FOREIGNKEY(`dept_id`)REFERENCES`department`(`id`)ONDELETERESTRICTONUPDATERESTRICT
)ENGINE=InnoDBCHARACTERSET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci COMMENT='职员信息'ROW_FORMAT= Dynamic;
​
-- ----------------------------
-- Records of staffer
-- ----------------------------
​
SET FOREIGN_KEY_CHECKS =1;

2.查看db_shop数据库当前的所有表

SHOWTABLES;

3.查看部门表department的表结构

DESC department;

4.复制department表结构,命名为department_bak

CREATETABLE department_bak LIKE department;

5.查看department_bak表结构

DESC department_bak;

十、课后练习

supplier表

goods表

orders表

item表

/***
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 */

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
--------语法 --建立视图 --if exists(select * from sysobjects where name='视图名') -- drop view 视图名 --go --create view 视图名 --as --select 字段名 from 表名 [条件] --go --主外健约束语句没有执行 use T90ERP go --***********人力资源 --部门表:Depet if exists(select * from sysobjects where name='Depet') drop table Depet go create table Depet ( dept_id Int primary key identity(1,1) not null, --部门编号 主键,自增 dept_name Varchar(20) not null --部门名称 ) ----约束 --alter table Depet add constraint UQ_dept_name unique (dept_name) go --职位表:Post if exists(select * from sysobjects where name='Post') drop table Post go create table Post ( Post_id Int primary key identity(1,1) not null, --职位编号 主键 自增 Post_name Varchar(50) not null, --职位名称 唯一 Post_money Money not null, --职位工资 Dept_id int not null --部门编号 外 Int 级联删除 ) ----约束 --alter table post add constraint UQ_post_name unique (post_name) alter table post add constraint FK_post_deptId foreign key(post_deptId) references depet(dept_id) go --员工信息表:Employee if exists(select * from sysobjects where name='Employee') drop table Employee go create table Employee ( Emp_id Int primary key identity(1,1) not null, --员工信息编号 主键,自增 Emp_number Varchar(50) not null, --员工工号 唯一 Emp_postId Int not null, --职位编号 外键 级联删除 Emp_hire Datetime not null, --录用时间 Emp_state Bit not null, --状态 默认1 (1在职/0离职) ) ----约束 --alter table Employee add constraint UQ_emp_number unique (emp_number) alter table Employee add constraint FK_emp_postId foreign key (emp_postId) references post(post_id) --alter table Employee add constraint DF_emp_state default(1) for emp_state go --简历表:Resume if exists(select * from sysobjects where name='Resume') drop table Resume go create table Resume ( Res_id Int primary key identity(1,1) not null, --职员信息ID 主键 非空自增 Int Emp_id Int not null, --职员ID 外键 Res_name Varchar(50) not null, --真实姓名 Res_englishname Varchar(50) null, --英文名 空 Res_idcard Varchar(19) not null, --身份证号 唯一索引 只有18位数字或18位数字加X Res_sex bit not null, --性别 默认1 男 只有男和女两种 Res_bornDate datetime not null, --出生年月 Res_nativeplace varchar(50) not null, --籍贯 Res_nation Varchar(50) not null, --民族 默认汉族 Res_health text null, --健康状况 空 默认健康 Res_diploma Varchar(50) not null, --学历 Res_address Varchar(50) null, --联系地址 空,默认地址不详 Res_Tel Varchar(50) not null, --电话 只能是11为数字 Res_photo image null --照片 空 ) ----约束 alter table Resume add constraint FK_res_empid foreign key (res_empid) references Employee(emp_id) --alter table Resume add constraint UQ_res_idcard unique (res_idcard) --alter table Resume add constraint CK_res_idcard check (res_idcard like '[1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9] --[1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9]' or res_idcard like '[1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9] --[1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9]X') --alter table Resume add constraint DF_res_sex default (1) for res_sex --alter table Resume add constraint CK_res_sex check(res_sex=1 or res_sex=0) --alter table Resume add constraint DF_res_nation default ('汉族') for res_nation --alter table Resume add constraint DF_res_health default ('健康') for res_health --alter table Resume add constraint DF_res_health defatult(1) for res_health --alter table Resume add constraint DF_res_address default ('地址不详') for res_address --alter table Resume add constraint CK_res_tel check(len(res_tel)=11) go --考勤类型表:CheckType if exists(select * from sysobjects where name='CheckType') drop table CheckType create table CheckType ( Checkt_id int primary key identity(1,1) not null, --考勤类型 主键 自增 Int Checkt_name varchar(50) not null --考勤名称(干什么) Varchar(50) ) go --考勤表:Check if exists(select * from sysobjects where name='CheckInfo') drop table CheckInfo create table CheckInfo ( Check_id int primary key identity(1,1) not null, --考勤id 主键 自增 Int Emp_id int not null, --员工id 外键 Int Check_hire datetime not null, --考勤开始时间 开始时间必须 Datetime Check_end datetime not null, --考勤结束时间 要在结束时间之前 Datatime Checkt_id int not null, --考勤类型 外键 Int Check_gtime int not null, --工休天数 Int check_time int not null --扣薪天数 Int ) ----约束 alter table CheckInfo add constraint FK_check_empId foreign key (check_empId) references Employee(emp_id) alter table CheckInfo add constraint FK_check_checktId foreign key (check_empId) references checkt(checkt_id) --alter table CheckInfo add constraint CK_check_hire check(check_hirecheck_hire) go --培训管理:Train if exists(select * from sysobjects where name='Train') drop table Train create table Train ( Train_id int primary key identity(1,1) not null, --培训编号 主键 自增 Int Train_time datetime not null, --培训日期 Datetime Train_address varchar(200) not null, --地址 Varchar(200) Train_content text not null, --内容 text Emp_id int not null, --职员编号 Int Train_teacher varchar(20) not null --讲师 Varchar(20) ) go --奖罚记录类型:PrizeAmerceType if exists(select * from sysobjects where name='PrizeAmerceType') drop table PrizeAmerceType create table PrizeAmerceType ( Prizet_id int primary key identity(1,1) not null, --奖罚记录类型id 主键 自增 Int Prizet_name varchar(50) not null, --奖罚记录名称 Varchar(50) Prizet_money money not null --奖罚金额 Money ) go --奖罚记录表:PrizeAmerceRecord if exists(select * from sysobjects where name='PrizeAmerceRecord') drop table PrizeAmerceRecord create table PrizeAmerceRecord ( Prize_id int primary key identity(1,1) not null, --奖罚记录id 主键 自增 Int Emp_id int not null, --员工id 外键 Int Prize_time datetime not null, --时间 Datetime Prizet_id int not null, --引用奖罚类型id 外键 Int Prize_desc text null, --描述 空,默认没有描述 text ) ----约束 --alter table PrizeAmerceRecord add constraint FK_prize_empid foreign key(prize_empId) references Employee(emp_id) --alter table PrizeAmerceRecord add constraint FK_prize_prizetid foreign key(prize_prizetId) references PrizeAmerceType(prizet_id) alter table PrizeAmerceRecord add constraint DF_prize_desc default('没有描述') for prize_desc go --档案:Record if exists(select * from sysobjects where name='Record') drop table Record create table Record ( Re_id int primary key identity(1,1) not null, --档案id 主键 自增 Int Re_code varchar(50) not null, --档案代码 日期+随即数生成 Varchar(50) Emp_id int not null, --员工id 外键 Int Prize_id int not null --奖罚记录id 外键 Int ) ----约束 --alter table Record add constraint FK_re_empId foreign key (re_empId) references Employee(emp_id) go --薪资表:SalaryInfo if exists(select * from sysobjects where name='SalaryInfo') drop table SalaryInfo create table SalaryInfo ( Sal_id int primary key identity(1,1) not null, --薪水表ID 主键 自增 Int Emp_id int not null, --职员ID 外键 Int Sal_bonus money not null, --奖金 Money Sal_deduct numeric(18,2) not null, --扣除 Numerica(18,2) Sal_tax money not null, --扣税 Money 默认为0 Sal_sum money not null, --总薪水 Money Sal_date smalldatetime not null --发放日期 smalldatetime ) ----约束 --alter table SalaryInfo add constraint FK_sal_empid foreign key (sal_empId) references Employee(emp_id) go --***********采购管理 --供应商级别:LevelInfo if exists(select * from sysobjects where name='LevelInfo') drop table LevelInfo create table LevelInfo ( Level_id int primary key identity(1,1) not null, --级别编号 主 自增 Int Level_name varchar(10) not null --级别名称 Varchar(10) ) go --供应商:Victualer if exists(select * from sysobjects where name='Victualer') drop table Victualer create table Victualer ( Victu_id int primary key identity(1,1) not null, --供应商编号 主 自增 Int Victu_name varchar(100) not null, --名称 Varchar(100) Level_id int not null, --级别编号 外,LevelInfo表 Int Victu_people varchar(20) not null, --联系人 Varchar(20) Victu_telephone varchar(11) not null, --联系电话 Varvhar(11),必须是11位 Victu_email varchar(50) null, --邮件 空,必须符合邮件格式 Varchar(50) Victu_address text not null, --联系地址 Text 默认地址不详 Victu_remark text null --备注 空,默认没有备注 Text ) ----约束 --alter table Victualer add constraint FK_Victu_levelId foreign key(Victu_levelId) references LevelInfo(Level_id) alter table Victualer add constraint CK_Victu_telephone check(len(victu_telephone)=11) alter table Victualer add constraint CK_Victu_email check(victu_email like '%@%.%') alter table Victualer add constraint DF_Victu_remark default ('没有描述') for victu_remark alter table Victualer add constraint DF_Victu_address default ('地址不详') for victu_address go --商品类别:Sort if exists(select * from sysobjects where name='Sort') drop table Sort create table Sort ( Sort_id int primary key identity(1,1) not null, --类别编号 主 自增 Int Sort_name varchar(50) not null --类别名称 Varchar(50) ) go --商品规格表:Spec if exists(select * from sysobjects where name='Spec') drop table Spec create table Spec ( Spec_id int primary key identity(1,1) not null, --规格编号 主 自增 Int Spec_name varchar(50) not null --规格名称 Varchar(50) ) go --商品表:Product if exists(select * from sysobjects where name='Product') drop table Product create table Product ( Pro_id int primary key identity(1,1) not null, --商品编号 主 自增 Int Pro_code varchar(20) not null, --商品代码 按日期+随机数生成,唯一 Varchar(20) Pro_name varchar(50) not null, --商品名称 Varchar(50) Sort_id int not null, --类别编号 外,Sort表 Int Spec_id int not null, --规格编号 外,Spec表 Int Pro_count int not null, --数量 Int Pro_inPrice money not null, --进货价 Money Pro_outPrice money not null, --销售价 Money Victu_id int not null, --供应商编号 外,Victualer表 Int 级联删除 Pro_remark text null --备注 空,默认没有备注 Text ) ----约束 go --询价单:AskPrice if exists(select * from sysobjects where name='AskPrice') drop table AskPrice create table AskPrice ( Ask_id int primary key identity(1,1) not null, --询价单编号 主 自增 Int Victu_id int not null, --供应商编号 外,Victualer表 Int Pro_id int not null, --商品编号 外,Product表 Int ask_price money not null, --报价 Money Ask_time datetime not null --添加时间 Datetime ) ----约束 go --采购单: BuyBill if exists(select * from sysobjects where name='BuyBill') drop table BuyBill create table BuyBill ( Buybill_id int primary key identity(1,1) not null, --采购单编号 主 自增 Int Buybill_num varchar(20) not null, --采购单据号 按日期+随机数生成,唯一 varchar(20) Emp_id int not null, --采购员编号 外,职员表 Int Buybill_time datetime not null, --采购时间 采购时间必须在交货时间之前 Datetime Buybill_delitime datetime not null, --交货时间 Datetime Buybill_remark text not null, --合同备注 Text Buybill_Isexam bit not null --库管是否审批 bit ) ----约束 go --采购明细表: BuyList if exists(select * from sysobjects where name='BuyList') drop table BuyList create table BuyList ( Buylist_id int primary key identity(1,1) not null, --采购明细编号 主 自增 Int Buybill_id int not null, --采购单编号 外,Buybill表 Int Pro_id int not null, --商品编号 外,Prduct表 Int Buylist_Count int not null, --采购数量 Int Buylist_price money not null, --采购价 Money Victu_id int not null, --供应商编号 外,Vitcualer表 Int Dsub_id int not null --采购单商品仓库编号 外,DepotSubarea表 Int ) ----约束 go --采购付款单:PayBill if exists(select * from sysobjects where name='PayBill') drop table PayBill create table PayBill ( Pay_id int primary key identity(1,1) not null, --付款单编号 主 自增 Int Buybill_id int not null, --采购单编号 外,Buybill表 Int Pay_oncoming money not null, --此次付款 如果应付款分为几次付 Money Pay_deal money not null, --应付款 则应付款应该要保持一致 Money Emp_id int not null, --财务部审批人编号 外,职员表 Int Pay_Isexam bit not null, --财务部是否审批 bit Pay_remark text null --备注 空,默认没有备注 text ) ----约束 go --采购退货单:MoveBill if exists(select * from sysobjects where name='MoveBill') drop table MoveBill create table MoveBill ( Move_id int primary key identity(1,1) not null, --退货单编号 主 自增 Int Buybill_id int not null, --采购单编号 外,Buybill表 Int Move_time datetime not null, --退单日期 退单日期必须是在采购日期之后 Datetime Dsub_id int not null, --库管审批人编号 外, Int Move_Isexam bit not null, --库管是否审批 Bit Move_remark text null --备注 空,默认没有备注 Text ) ----约束 go --***********仓库管理 --库区表:DepotSubarea if exists(select * from sysobjects where name='DepotSubarea') drop table DepotSubarea create table DepotSubarea ( Dsub_id int primary key identity(1,1) not null, --库区id 主键 自增 Int Dsub_name varchar(10) not null,--库区名称 Varchar(10) Dsub_type varchar(10) not null--仓库类别 Varchar(10) ) go --移动类型表:Transfer if exists(select * from sysobjects where name='Transfer') drop table Transfer create table Transfer ( Tran_id int primary key identity(1,1) not null, --移动类型id 主键 自增 Int Tran_name varchar(20) not null --移动类型名称 Varchar(20) ) go --入库明细表:PutInfo if exists(select * from sysobjects where name='PutInfo') drop table PutInfo create table PutInfo ( Put_id Int primary key identity(1,1) not null, --入库明细id 主键 自增 Put_code Varchar(20) not null, --入库单代码 日期+随即数生成 Buybill_id Int not null, --采购单编号 外键 Put_time Datetime not null, --入库时间 Put_people varchar(20) not null, --入库人 Dsub_id Int not null, --库区 外键 级联删除 Tran_id Int not null --移动类型 外键 ) ----约束 go --库存表:Stock if exists(select * from sysobjects where name='Stock') drop table Stock create table Stock ( Stock_id Int primary key identity(1,1) not null, --库存编号 主键 自增 Dsub_id Int not null,            --库区id 外键 Pro_id Int not null,              --商品id 外键 Stock_number Int not null             --商品数量 ) ----约束 go --出库明细表:OutInfo if exists(select * from sysobjects where name='OutInfo') drop table OutInfo create table OutInfo ( Out_id Int primary key identity(1,1) not null, --出库明细id 主键 自增 Out_code Varchar(20) not null, --出库单据号 唯一 Out_time Datetime not null, --出库时间 Out_llr Varchar(20) not null, --领料人 Out_flr Varchar(20) not null, --发料人 Out_tranId Int not null, --移动类型 Out_dsubId Int not null --库区 ) ----约束 go --补仓管理:RepairDepot if exists(select * from sysobjects where name='RepairDepot') drop table RepairDepot create table RepairDepot ( Repa_id Int primary key identity(1,1) not null, --补仓id 主键 自增 Pro_id Int not null, --商品id 外键 Repa_number Int not null, --补仓数量 Repa_dsubId Int not null, --库区表 外键 Repa_remark text null --备注 空,默认没有备注 ) ----约束 go --***********销售管理 --客户级别表(CustLevel) if exists(select * from sysobjects where name='CustLevel') drop table CustLevel create table CustLevel ( Cl_id Int primary key identity(1,1) not null,   --编号 主,自增 Cl_name Varchar(10) not null,           --级别名称 Cl_discount float not null            --折扣 ) --约束 go --客户信息表(customer) if exists(select * from sysobjects where name='customer') drop table customer create table customer ( C_id int primary key identity(1,1) not null,   --编号 主,自动增长 C_number Varchar(10) not null,          --客户代号 C_name Varchar(20) not null,            --客户名称 C_linkman Varchar(20) not null,        --联系人 C_phone Varchar(11) not null,          --联系电话 C_address Text null,              --公司地址 空,默认地址不详 Cl_id Int not null,              --级别编号 外 C_remark text null,               --备注信息 默认没有备注 空 ) --约束 go --订单表(orders) if exists(select * from sysobjects where name='orders') drop table orders create table orders ( O_id int primary key identity(1,1) not null,  --编号 主,自增 O_number Varchar(20) not null,         --订单代码 日期+随即数生成 O_timestart datetime not null,        --下单日期 下单时间必须在交货时间之前 O_timestop Datetime not null,         --交货日期 O_money Money not null,             --下单金额 C_id Int not null,             --客户编号 外 级联删除 Emp_id int not null              --员工编号 外 ) --约束 go --订单明细表(OrderDetails) if exists(select * from sysobjects where name='OrderDetails') drop table OrderDetails create table OrderDetails ( Od_id Int primary key identity(1,1) not null,   --编号 主,自增 O_id int not null,                --订单编号 外 Pro_id int not null,                --商品编号 外 Od_price Money not null,               --单件金额 Od_accounts Int not null               --单件数量 ) --约束 go --销售单表(Sells) if exists(select * from sysobjects where name='Sells') drop table Sells create table Sells ( Sell_id int primary key identity(1,1) not null,    --编号 主,自增 O_id int not null,                 --订单编号 外 Sell_timestart datetime not null,           --销售日期 下单时间必须在交货时间之前 Sell_timestop Datetime not null,            --交货日期 Sell_money Money not null,              --销售金额 C_id Int not null,              --客户编号 外 Emp_id int not null,               --员工编号 外 Sell_remark text null                   --备注 空 ) --约束 go --销售单明细表(SellDetails) if exists(select * from sysobjects where name='SellDetails') drop table SellDetails create table SellDetails ( Selld_id int primary key identity(1,1) not null,   --编号 主,自增 O_id int not null,               --订单编号 Pro_id int not null,              --商品编号 Selld_price Money not null,             --单件金额 Selld_accounts Int not null              --单件数量 ) --约束 go --***********财务管理 --财务科目表:FinaSub if exists(select * from sysobjects where name='FinaSub') drop table FinaSub create table FinaSub ( Fina_id Int primary key identity(1,1) not null, --科目编号 主,自增 Fina_name Varchar(50) not null, --科目名称 Fina_accounts Varchar(50) not null, --银行账号 随机数生成 Fina_people Varchar(50) not null, --联系人 Fina_telephone Varchar(11) not null, --联系电话 Fina_mode Varchar(10) not null, --是借或贷 Fina_play Varchar(10) not null, --借贷方式(现金、发票) Fian_money money not null --金额 ) go --发票信息表:Invoice if exists(select * from sysobjects where name='Invoice') drop table Invoice create table Invoice ( Invo_id Int primary key identity(1,1) not null, --发票编号 主,自增 Invo_code Varchar(50) not null, --发票单据号 日期+随机数生成,唯一 Invo_type Varchar(10) not null, --发票类型 Invo_money money not null, --金额 Invo_use Varchar(50) not null, --发票用途 Invo_datetime Datetime not null, --发票日期 Emp_id int not null --财务员编号 外,职员表 ) ----约束 go --固定资产表:FixedAssets if exists(select * from sysobjects where name='FixedAssets') drop table FixedAssets create table FixedAssets ( Fix_id Int primary key identity(1,1) not null, --资产编号 主,自增 Fix_name Varchar(100) not null, --资产名称 Fix_money Money not null, --可汇兑金额 Fix_datetime Datetime not null, --添加时间 Fix_remark Text null, --备注 空,默认没有备注 ) ----约束 go --财务员统计视图 --***********权限管理 --用户表:UserInfo if exists(select * from sysobjects where name='UserInfo') drop table UserInfo create table UserInfo ( u_id int primary key identity(1,1) not null, --用户编号,主,自增 u_name varchar(20) not null, --用户名,即登录名 u_pass varchar(10) not null, --登录密码 u_time datetime not null --登录时间 ) --insert into UserInfo values('admin','admin','2008-08-05') --insert into UserInfo values('yqh','yqh','2008-08-05') --insert into UserInfo values('gogo','gogo','2008-08-05') --insert into UserInfo values('wangwang','wangwang','2008-08-05') select * from UserInfo go --角色表:RolesInfo if exists(select * from sysobjects where name='RolesInfo') drop table RolesInfo create table RolesInfo ( r_id int primary key identity(1,1) not null, --角色编号,主,自增 r_name varchar(20) not null, --角色名称,即职位名称,唯一 r_desc text null --角色描述,空,默认没有描述 ) alter table RolesInfo add constraint UQ_r_name unique (r_name) alter table RolesInfo add constraint DF_r_desc default ('没有描述') for r_desc --insert into RolesInfo values('系统管理员','可以有任何操作') --insert into RolesInfo values('总经理','最高管理者') --insert into RolesInfo values('部门经理','管理者') --insert into RolesInfo values('普通员工',default) select * from RolesInfo go --用户和角色中间表(因为是、一对多的关系):UserRolesCenter if exists(select * from sysobjects where name='UserRolesCenter') drop table UserRolesCenter create table UserRolesCenter ( c_id int primary key identity(1,1) not null, --中间表编号,主,自增 u_id int not null, --外,用户编号,修改和删除规则都是层叠 r_id int not null --外,角色编号,修改和删除规则都是层叠 ) alter table UserRolesCenter add constraint FK_u_id foreign key (u_id) references UserInfo(u_id) alter table UserRolesCenter add constraint FK_r_id foreign key (r_id) references RolesInfo(r_id) --insert into UserRolesCenter values(1,1) --insert into UserRolesCenter values(1,2) --insert into UserRolesCenter values(2,2) --insert into UserRolesCenter values(3,3) --insert into UserRolesCenter values(4,4) select * from UserRolesCenter --delete from UserRolesCenter where c_id=5 --查询视图 select * from UserRolesView --查询角色1中已有的用户,利用视图 select * from UserRolesView where r_id=1 --查询角色1中没有的用户,利用子查询,用户表中的所有用户减去角色中已有的用户 Select * from UserInfo where u_id not in (select u_id from UserRolesView where r_id=1) go --菜单表:MenuInfo if exists(select * from sysobjects where name='MenuInfo') drop table MenuInfo create table MenuInfo ( M_id Int primary key identity(1,1) not null,--菜单编号 主,自增 M_name Varchar(50) not null,--菜单名称 M_url Varchar(50) null,--菜单链接 空 M_parentId int not null,--父菜单编号 ) --约束 --insert into MenuInfo values('系统权限管理','',0) --insert into MenuInfo values('人力资源管理','',0) --insert into MenuInfo values('采购管理','',0) --insert into MenuInfo values('仓库管理','',0) --insert into MenuInfo values('销售管理','',0) --insert into MenuInfo values('财务管理','',0) select * from MenuInfo go --4.角色和菜单中间表即权限表:PowerInfo if exists(select * from sysobjects where name='PowerInfo') drop table PowerInfo create table PowerInfo ( P_id Int primary key identity(1,1) not null,--权限编号 主 R_id Int not null,--角色编号 外 ,修改和删除规则都是层叠 M_id int not null,--菜单编号 外 ,修改和删除规则都是层叠 ) --约束 alter table PowerInfo add constraint FK_pr_id foreign key (r_id) references RolesInfo(r_id) alter table PowerInfo add constraint FK_m_id foreign key (m_id) references MenuInfo(m_id) --insert into PowerInfo values(1,1) --insert into PowerInfo values(1,2) --insert into PowerInfo values(2,2) --insert into PowerInfo values(3,3) --insert into PowerInfo values(4,4) select * from PowerInfo --查询视图 select * from RolesMenuView --查询角色1中已有的功能,利用视图 select * from RolesMenuView where r_id=1 --查询角色1中没有的功能,利用子查询,菜单表中的所有功能减去角色中已有的功能 Select * from MenuInfo where m_id not in (select m_id from RolesMenuView where r_id=1) go

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸷鸟之不群

你的鼓励将是我最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值