hibernate drop table if exists

将hibernate.hbm.xml中的hbm2ddl.auto属性设置为update,可防止Hibernate在运行时自动删除并重建数据库。这允许数据库结构更新而非清除。
/*==============================================================*/ /* DBMS name: Sybase SQL Anywhere 12 */ /* Created on: 2025/6/23 16:54:25 */ /*==============================================================*/ if exists(select 1 from sys.sysforeignkey where role='FK_P_INFOR_REFERENCE_HEAL') then alter table P_infor delete foreign key FK_P_INFOR_REFERENCE_HEAL end if; if exists(select 1 from sys.sysforeignkey where role='FK_P_INFOR_REFERENCE_P_SPE') then alter table P_infor delete foreign key FK_P_INFOR_REFERENCE_P_SPE end if; if exists(select 1 from sys.sysforeignkey where role='FK_VETERINA_REFERENCE_HEAL') then alter table "Veterinary_information table" delete foreign key FK_VETERINA_REFERENCE_HEAL end if; if exists(select 1 from sys.sysforeignkey where role='FK_AFTER_SA_REFERENCE_ORDER') then alter table after_sales delete foreign key FK_AFTER_SA_REFERENCE_ORDER end if; if exists(select 1 from sys.sysforeignkey where role='FK_GKID_REFERENCE_P_SPE') then alter table gkid delete foreign key FK_GKID_REFERENCE_P_SPE end if; if exists(select 1 from sys.sysforeignkey where role='FK_GKID_REFERENCE_SERVICES') then alter table gkid delete foreign key FK_GKID_REFERENCE_SERVICES end if; if exists(select 1 from sys.sysforeignkey where role='FK_GOODS_IN_REFERENCE_GKID') then alter table goods_infor delete foreign key FK_GOODS_IN_REFERENCE_GKID end if; if exists(select 1 from sys.sysforeignkey where role='FK_GOODS_IN_REFERENCE_ORDER_IT') then alter table goods_infor delete foreign key FK_GOODS_IN_REFERENCE_ORDER_IT end if; if exists(select 1 from sys.sysforeignkey where role='FK_ORDER_REFERENCE_CUS_INFO') then alter table "order" delete foreign key FK_ORDER_REFERENCE_CUS_INFO end if; if exists(select 1 from sys.sysforeignkey where role='FK_ORDER_IT_REFERENCE_ORDER') then alter table order_item delete foreign key FK_ORDER_IT_REFERENCE_ORDER end if; if exists(select 1 from sys.sysforeignkey where role='FK_PAYMENT_REFERENCE_ORDER') then alter table payment delete foreign key FK_PAYMENT_REFERENCE_ORDER end if; if exists(select 1 from sys.sysforeignkey where role='FK_PERMISSI_REFERENCE_CUS_INFO') then alter table permission_manage delete foreign key FK_PERMISSI_REFERENCE_CUS_INFO end if; if exists(select 1 from sys.sysforeignkey where role='FK_PERMISSI_REFERENCE_PEOPLE_I') then alter table permission_manage delete foreign key FK_PERMISSI_REFERENCE_PEOPLE_I end if; if exists(select 1 from sys.sysforeignkey where role='FK_PERMISSI_REFERENCE_SAL_MANA') then alter table permission_manage delete foreign key FK_PERMISSI_REFERENCE_SAL_MANA end if; if exists(select 1 from sys.sysforeignkey where role='FK_PURCHASE_REFERENCE_CONSUMAB') then alter table purchase delete foreign key FK_PURCHASE_REFERENCE_CONSUMAB end if; if exists(select 1 from sys.sysforeignkey where role='FK_PURCHASE_REFERENCE_SOURCE') then alter table purchase delete foreign key FK_PURCHASE_REFERENCE_SOURCE end if; if exists(select 1 from sys.sysforeignkey where role='FK_SERVICES_REFERENCE_SERVICES') then alter table services_info delete foreign key FK_SERVICES_REFERENCE_SERVICES end if; if exists(select 1 from sys.sysforeignkey where role='FK_SERVICES_REFERENCE_SERVICES') then alter table services_kind delete foreign key FK_SERVICES_REFERENCE_SERVICES end if; if exists(select 1 from sys.sysforeignkey where role='FK_SOURCE_REFERENCE_GOODS_IN') then alter table source delete foreign key FK_SOURCE_REFERENCE_GOODS_IN end if; if exists(select 1 from sys.sysforeignkey where role='FK_STOCK_REFERENCE_GOODS_IN') then alter table stock delete foreign key FK_STOCK_REFERENCE_GOODS_IN end if; drop table if exists Heal; drop table if exists P_infor; drop table if exists P_spe; drop table if exists "Veterinary_information table"; drop table if exists after_sales; drop table if exists consumable; drop table if exists cus_info; drop table if exists gkid; drop table if exists goods_infor; drop table if exists "order"; drop table if exists order_item; drop table if exists payment; drop table if exists people_info; drop table if exists permission_manage; drop table if exists purchase; drop table if exists sal_manage; drop table if exists services_info; drop table if exists services_kind; drop table if exists services_rules; drop table if exists source; drop table if exists stock; /*==============================================================*/ /* Table: Heal */ /*==============================================================*/ create table Heal ( p_ID integer not null, Vac text null, constraint PK_HEAL primary key clustered (p_ID) ); /*==============================================================*/ /* Table: P_infor */ /*==============================================================*/ create table P_infor ( p_ID integer not null, p_name varchar(50) null, p_gender varchar(50) null, p_date_of_birth varchar(50) null, spe_ID integer null, constraint PK_P_INFOR primary key clustered (p_ID) ); /*==============================================================*/ /* Table: P_spe */ /*==============================================================*/ create table P_spe ( spe_ID integer not null, p_species varchar(50) null, p_char varchar(50) null, constraint PK_P_SPE primary key clustered (spe_ID) ); /*==============================================================*/ /* Table: "Veterinary_information table" */ /*==============================================================*/ create table "Veterinary_information table" ( vet_ID integer not null, p_ID integer null, vet_contact varchar(50) null, vet_name varchar(50) null, vet_age integer null, constraint "PK_VETERINARY_INFORMATION TABL" primary key clustered (vet_ID) ); /*==============================================================*/ /* Table: after_sales */ /*==============================================================*/ create table after_sales ( request_id int not null, order_id varchar(50) null, type enum('0','1') null, status enum('0','1') null, constraint PK_AFTER_SALES primary key (request_id) ); /*==============================================================*/ /* Table: consumable */ /*==============================================================*/ create table consumable ( con_ID integer not null, c_name varchar(50) null, c_kind varchar(50) null, c_price decimal null, constraint PK_CONSUMABLE primary key clustered (con_ID) ); /*==============================================================*/ /* Table: cus_info */ /*==============================================================*/ create table cus_info ( cus_id integer not null, cus_name varchar(50) null, cus_gender varchar(50) null, cus_age varchar(50) null, con_info varchar(50) null, constraint PK_CUS_INFO primary key clustered (cus_id) ); /*==============================================================*/ /* Table: gkid */ /*==============================================================*/ create table gkid ( gkid integer not null, kid integer null, spe_ID integer null, gkname varchar(50) null, gkdetail long varchar null, constraint PK_GKID primary key clustered (gkid) ); /*==============================================================*/ /* Table: goods_infor */ /*==============================================================*/ create table goods_infor ( gid integer not null, gkid integer null, gname varchar(50) null, gprice integer null, gsnum integer null, constraint PK_GOODS_INFOR primary key clustered (gid) ); /*==============================================================*/ /* Table: "order" */ /*==============================================================*/ create table "order" ( order_id varchar(50) not null, "cus id" integer null, total_amount decimal(10,2) null, status enum('0','1','2','3') null, receiver_info varchar(200) null, created_at timestamp null, constraint PK_ORDER primary key (order_id) ); /*==============================================================*/ /* Table: order_item */ /*==============================================================*/ create table order_item ( gid integer not null, order_id varchar(50) null, quantity int null, gprice decimal(10,2) null, constraint PK_ORDER_ITEM primary key (gid) ); /*==============================================================*/ /* Table: payment */ /*==============================================================*/ create table payment ( payment_id int not null, order_id varchar(50) null, amount decimal(10,2) null, method enum('0','1') null, status enum('0','1') null, constraint PK_PAYMENT primary key (payment_id) ); /*==============================================================*/ /* Table: people_info */ /*==============================================================*/ create table people_info ( p_id integer not null, p_name varchar(50) null, p_gender varchar(50) null, p_age integer null, constraint PK_PEOPLE_INFO primary key clustered (p_id) ); /*==============================================================*/ /* Table: permission_manage */ /*==============================================================*/ create table permission_manage ( per_id integer not null, cus_id integer null, sal_id integer null, p_id integer null, per_name varchar(50) null, per_describe varchar(50) null, per_type varchar(50) null, constraint PK_PERMISSION_MANAGE primary key clustered (per_id) ); /*==============================================================*/ /* Table: purchase */ /*==============================================================*/ create table purchase ( p_ID integer not null, p_day varchar(50) null, con_ID integer not null, souceid integer null, sum varchar(50) null, p_price decimal null, constraint PK_PURCHASE primary key clustered (p_ID) ); /*==============================================================*/ /* Table: sal_manage */ /*==============================================================*/ create table sal_manage ( sal_id integer not null, p_id integer null, ba_sal decimal(50) null, bonus decimal(50) null, allow decimal(50) null, de_ite decimal(50) null, net_sal decimal(50) null, constraint PK_SAL_MANAGE primary key clustered (sal_id) ); /*==============================================================*/ /* Table: services_info */ /*==============================================================*/ create table services_info ( info_id integer not null, kid integer null, services_name varchar(50) null, services_detail text null, constraint PK_SERVICES_INFO primary key clustered (info_id) ); /*==============================================================*/ /* Table: services_kind */ /*==============================================================*/ create table services_kind ( kid integer not null, kname varchar(50) null, kdetial text null, rules_id integer null, constraint PK_SERVICES_KIND primary key clustered (kid) ); /*==============================================================*/ /* Table: services_rules */ /*==============================================================*/ create table services_rules ( rules_id integer not null, rules_detail text null, constraint PK_SERVICES_RULES primary key clustered (rules_id) ); /*==============================================================*/ /* Table: source */ /*==============================================================*/ create table source ( souceid integer not null, gid integer null, sourcename varchar(50) null, sourceaddress varchar(50) null, sources integer null, constraint PK_SOURCE primary key clustered (souceid) ); /*==============================================================*/ /* Table: stock */ /*==============================================================*/ create table stock ( sid integer not null, gid integer null, snum integer null, constraint PK_STOCK primary key clustered (sid) ); alter table P_infor add constraint FK_P_INFOR_REFERENCE_HEAL foreign key (p_ID) references Heal (p_ID) on update restrict on delete restrict; alter table P_infor add constraint FK_P_INFOR_REFERENCE_P_SPE foreign key (spe_ID) references P_spe (spe_ID) on update restrict on delete restrict; alter table "Veterinary_information table" add constraint FK_VETERINA_REFERENCE_HEAL foreign key (p_ID) references Heal (p_ID) on update restrict on delete restrict; alter table after_sales add constraint FK_AFTER_SA_REFERENCE_ORDER foreign key (order_id) references "order" (order_id) on update restrict on delete restrict; alter table gkid add constraint FK_GKID_REFERENCE_P_SPE foreign key (spe_ID) references P_spe (spe_ID) on update restrict on delete restrict; alter table gkid add constraint FK_GKID_REFERENCE_SERVICES foreign key (kid) references services_kind (kid) on update restrict on delete restrict; alter table goods_infor add constraint FK_GOODS_IN_REFERENCE_GKID foreign key (gkid) references gkid (gkid) on update restrict on delete restrict; alter table goods_infor add constraint FK_GOODS_IN_REFERENCE_ORDER_IT foreign key (gid) references order_item (gid) on update restrict on delete restrict; alter table "order" add constraint FK_ORDER_REFERENCE_CUS_INFO foreign key ("cus id") references cus_info (cus_id) on update restrict on delete restrict; alter table order_item add constraint FK_ORDER_IT_REFERENCE_ORDER foreign key (order_id) references "order" (order_id) on update restrict on delete restrict; alter table payment add constraint FK_PAYMENT_REFERENCE_ORDER foreign key (order_id) references "order" (order_id) on update restrict on delete restrict; alter table permission_manage add constraint FK_PERMISSI_REFERENCE_CUS_INFO foreign key (cus_id) references cus_info (cus_id) on update restrict on delete restrict; alter table permission_manage add constraint FK_PERMISSI_REFERENCE_PEOPLE_I foreign key (p_id) references people_info (p_id) on update restrict on delete restrict; alter table permission_manage add constraint FK_PERMISSI_REFERENCE_SAL_MANA foreign key (sal_id) references sal_manage (sal_id) on update restrict on delete restrict; alter table purchase add constraint FK_PURCHASE_REFERENCE_CONSUMAB foreign key (con_ID) references consumable (con_ID) on update restrict on delete restrict; alter table purchase add constraint FK_PURCHASE_REFERENCE_SOURCE foreign key (souceid) references source (souceid) on update restrict on delete restrict; alter table services_info add constraint FK_SERVICES_REFERENCE_SERVICES foreign key (kid) references services_kind (kid) on update restrict on delete restrict; alter table services_kind add constraint FK_SERVICES_REFERENCE_SERVICES foreign key (rules_id) references services_rules (rules_id) on update restrict on delete restrict; alter table source add constraint FK_SOURCE_REFERENCE_GOODS_IN foreign key (gid) references goods_infor (gid) on update restrict on delete restrict; alter table stock add constraint FK_STOCK_REFERENCE_GOODS_IN foreign key (gid) references goods_infor (gid) on update restrict on delete restrict; 跟据我给的数据库代码,用eclipse手把手教我建成实体类导包
06-24
``` -- 学号:2023123456 姓名:张三 SELECT '学号:2345313524, 姓名:吕倩倩' AS StudentInfo; -- 创建数据库 CREATE DATABASE IF NOT EXISTS db_final_project; USE db_final_project; -- 部门表 DROP TABLE IF EXISTS dept; CREATE TABLE dept( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '部门ID', name VARCHAR(50) NOT NULL COMMENT '部门名称' ) COMMENT='部门表'; INSERT INTO dept(name) VALUES ('研发部'),('市场部'),('财务部'),('销售部'),('总经办'); -- 员工表 DROP TABLE IF EXISTS emp; CREATE TABLE emp( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '员工ID', name VARCHAR(50) NOT NULL COMMENT '姓名', age TINYINT COMMENT '年龄', job VARCHAR(20) COMMENT '职位', salary INT COMMENT '薪资', entrydate DATE COMMENT '入职日期', managerid INT COMMENT '直属上司ID', dept_id INT COMMENT '部门ID', INDEX idx_emp_dept(dept_id), INDEX idx_emp_salary(salary), INDEX idx_emp_entry(entrydate) ) COMMENT='员工表'; INSERT INTO emp(name,age,job,salary,entrydate,managerid,dept_id) VALUES ('张三',45,'总经理',20000,'2010-01-01',NULL,5), ('李四',30,'项目经理',15000,'2012-05-10',1,1), ('王五',28,'开发工程师',9000,'2015-08-20',2,1), ('赵六',35,'市场总监',16000,'2011-03-15',1,2), ('孙七',50,'财务主管',18000,'2013-11-30',1,3), ('周八',26,'销售代表',8000,'2016-07-01',4,4), ('吴九',22,'实习生',4000,'2020-09-01',2,1); -- 薪资等级表 DROP TABLE IF EXISTS salgrade; CREATE TABLE salgrade( grade INT COMMENT '等级', losal INT COMMENT '最低薪资', hisal INT COMMENT '最高薪资' ) COMMENT='薪资等级表'; INSERT INTO salgrade VALUES (1,0,5000),(2,5001,10000),(3,10001,15000),(4,15001,20000); -- 题目一:复杂查询与优化 -- 1. CTE与中位数 -- 题目一:复杂查询与优化 -- 1. CTE与中位数 WITH dept_stats AS ( SELECT d.id AS dept_id, d.name AS dept_name, AVG(e.salary) AS avg_salary, ( SELECT e2.entrydate FROM ( SELECT entrydate, @rownum := @rownum + 1 AS row_num FROM emp e2, (SELECT @rownum := 0) r WHERE e2.dept_id = d.id ORDER BY entrydate ) e2 WHERE e2.row_num = ( SELECT CEILING(COUNT(*) / 2) FROM emp e3 WHERE e3.dept_id = d.id ) ) AS med_entry FROM dept d LEFT JOIN emp e ON d.id = e.dept_id GROUP BY d.id, d.name ) -- 查询薪资高于部门平均且入职日期早于中位数的员工 SELECT ds.dept_name AS '部门名称', e.name AS '员工姓名', e.salary AS '薪资', e.entrydate AS '入职日期' FROM emp e JOIN dept_stats ds ON e.dept_id = ds.dept_id WHERE e.salary > ds.avg_salary AND e.entrydate < ds.med_entry; -- 2. 性能分析与优化 -- 原始执行计划 EXPLAIN FORMAT=JSON SELECT * FROM emp WHERE dept_id = 1 AND salary > 10000; -- 优化1: 创建覆盖索引 ALTER TABLE emp ADD INDEX idx_emp_dept_salary(dept_id, salary); -- 优化2: 创建物化视图 CREATE VIEW emp_dept_stats AS SELECT d.id AS dept_id, d.name AS dept_name, COUNT(e.id) AS emp_count, AVG(e.salary) AS avg_salary, MAX(e.salary) AS max_salary, MIN(e.salary) AS min_salary FROM dept d LEFT JOIN emp e ON d.id = e.dept_id GROUP BY d.id, d.name; -- 优化后执行计划 EXPLAIN FORMAT=JSON SELECT * FROM emp WHERE dept_id = 1 AND salary > 10000; -- 题目二:窗口函数与分区分析 -- 1. 窗口函数排名 -- Student:2023123456张三 WITH dept_top3 AS ( SELECT d.name AS dept_name, e.name AS emp_name, e.salary, ROW_NUMBER() OVER (PARTITION BY e.dept_id ORDER BY e.salary DESC) AS salary_rank FROM emp e JOIN dept d ON e.dept_id = d.id ) SELECT dept_name AS '部门名称', MAX(salary) - MIN(salary) AS '薪资差距' FROM dept_top3 WHERE salary_rank <= 3 GROUP BY dept_name; -- 2. 分区表设计 -- Student:2023123456张三 -- 创建分区表 DROP TABLE IF EXISTS emp_partitioned; CREATE TABLE emp_partitioned( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '员工ID', name VARCHAR(50) NOT NULL COMMENT '姓名', age TINYINT COMMENT '年龄', job VARCHAR(20) COMMENT '职位', salary INT COMMENT '薪资', entrydate DATE COMMENT '入职日期', managerid INT COMMENT '直属上司ID', dept_id INT COMMENT '部门ID', INDEX idx_emp_dept(dept_id), INDEX idx_emp_salary(salary) ) COMMENT='员工表(分区)' PARTITION BY RANGE (YEAR(entrydate)) ( PARTITION p2020 VALUES LESS THAN (2021), PARTITION p2021 VALUES LESS THAN (2022), PARTITION p2022 VALUES LESS THAN (2023), PARTITION p2023 VALUES LESS THAN (2024), PARTITION pmax VALUES LESS THAN MAXVALUE ); -- 加载数据到分区表 INSERT INTO emp_partitioned SELECT * FROM emp; -- 验证分区裁剪 EXPLAIN SELECT * FROM emp_partitioned WHERE entrydate BETWEEN '2020-01-01' AND '2020-12-31'; ```请回答上述代码剧具体执行顺序精确到每一行是否执行或者不执行
06-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值