This is about knowledge of Database
-- Sam 17:03 25/09/2014

how to change the root user password?
  1. close service and create a txt file with a command inside, then restart the mysql with the file. (it didn't work in my environment)
  2. directly change in dbeaver: select user node, click the root user, then change passwod directly!
-- Sam 17:45 21/04/2021
我用过的SQL:
  1. create table employees (first_name varchar(20), family_name varchar(20), birthday date, born varchar(30), worktime int, salary int);
  2. insert into employees values('dog');
  3. insert into employees (family_name, first_name, born, birthday) values('luis', 'tom', 'montreal', '2013-07-07');
  4. alter table employees add column department bigint;
  5. alter table employees add foreign key(department) references department(departmentId)
  6. alter table department add primary key(departmentId);
  7. alter table employees add constraint pk_c primary key(employeeId)
  8. alter table sectionpromo add unique key(name);
  9. alter table department rename column dipartment_name to department_name;
  10. update employees set employeeId = 1, birthday = '1978-04-25' where first_name = 'wei'
  11. delete from employees where employeeId is null;                                                        //注意,不能是=null, 必须用is。
  12. select employees.employeeId, employees.first_name, location.location from employees left [outer] join location on employees.employeeId = location.employeeId order by location desc;   //注意,左连接就是从左面连接,右连接就是从右面接入,左边的全显示,右面的只显示复合条件的。 Innerjoin则只显示共有的,如果有一对多或者多对一,则予以复制。
  13. select employees.employeeId, employees.first_name, location.location from employees right join location on employees.employeeId = location.employeeId;
  14. select employees.employeeId, employees.first_name, location.location from employees inner join location on employees.employeeId = location.employeeId;
  15. select first_name from employees where employeeid = (select employeeid from locations where location = 'montreal')
  16. select * from employees where employeeId in (select employeeId from locations)  //跟inner Join类似,但不会像Inner join 显示重复行。any 和all要结合运算符使用。例如:
    1. 多行子查询使用ANY操作符号例子:查询有一门以上的成绩高于Kaka的任何一门成绩的学生的名字:
      select stName from Student where stId in(select distinct stId from score where score >any(select score from score where stId=(select stId from Student where stName='Kaka')))
  17. select distinct employeeid from locations;
  18. SELECT first_name FROM employees WHERE family_name LIKE 'ha_' or family_name like 'j%'
  19. select first_name from employees where family_name like 'ha_' or family_name like '_i[^d-f]%';  //不知怎么[在psQL上通过不了]
  20. select * from employees where first_name like 'tab%%' escape 'b'  //我把名字改成了ta%o, 这里的b可以用(几乎)任意字符替代
  21. select first_name from employees where (family_name, employeeId) in (select family_name, max(employeeId) from employees where worktime = 12 group by family_name)
  22. 选出所有薪水比其经理高的员工:select * from employees as e1 join employees as e2 on e1.reportto = e2.employeeid where e1.salary > (select salary from employees where employeeId = e1.reportto);
  23. 运算符:max()   min()    count()    avg()
    <>    <    >    <=    >=
    AND   OR   NOT   AND
    IS  NULL    IS NOT NULL;     //NULL等于NULL,但null和其他值相比,结果为unknown
  24. left join, A left join B, 那么A显示全部记录,B显示符合条件的记录。

-- Sam 00:26 14/05/2021
我用过的JPA 注解:
  1. @Id
  2. @GeneratedValue(strategy=GenerationType.AUTO)
  3. @Column(name= "xxx")
  4. @NotNull
  5. @ManyToOne//(of this guy)
  6. @OneToMany//(of this guy)
  7. @Temporal(TempralType.TIMESTAMP)
  8. @DataTimeFormat(style="M-")
  9. @Transient
  10. @Index(name="xxx")
-- Sam 16:54 14/09/2014
Database datatype
  1. char/varchar(n)/text
  2. integer(int)
  3. time/date/timestamp
char varchar or text?
  1. char is for main key and fixed length fields, it can wast space, but faster for index.
  2. varchar (normally varchar(n) is for normal field. because it can save space. but not good for index. sqlserver n<8000,mysql n<65535 通常制约于行长度。
  3. text is for variable length big text. 最长可到2G。
-- Sam 16:14 14/09/2014
数据库设计技巧
  1. 需求分析阶段 and conceptual design
    1. 理解需求,询问未来变化。
    2. 了解企业业务可以节约大量时间。
    3.  重视输入输出。检查现有的报表、查询和视图(输出)以决定必要的表和字段。例如客户需要一个报表按照邮政编码排序,你要保证单独的邮编字段而不要把邮编糅进地址里。
    4. 创建数据字典和ER 图表, ER图对表明表之间关系很有用,数据字典则说明了每个字段的用途以及任何可能存在的别名。对SQL 表达式的文档化来说这是完全必要的。
    5. 数据库各种对象的命名必须规范。
  2. 表和字段的设计(logical design)
    1. 消除数据库中的数据冗余 (3NF), be aware that, for special case, we allow some data redundancy to improve performance.
    2.  数据驱动:采用数据驱动而非硬编码的方式,许多策略变更和维护都会方便得多,大大增强系统的灵活性和扩展性。
      举例,假如用户界面要访问外部数据源(文件、XML 文档、其他数据库等),不妨把相应的连接和路径信息存储在用户界面支持表里。还有,如果用户界面执行工作流之类的任务(发送邮件、打印信笺、修改记录状态等),那么产生工作流的数据也可以存放在数据库里。角色权限管理也可以通过数据驱动来完成。事实上,如果过程是数据驱动的,你就可以把相当大的责任推给用户,由用户来维护自己的工作流过程。
    3. 考虑各种变化:西方人女性结婚后从夫姓,“类别”"职位“这种列可令建一个表(一对多)并在那张表里还可以增加开始,结束时间。
    4. 每个表都可以加的三个字段:createdTime, creator, version. (因为hibernate.cfg.xml中可以设置optimistic-lock="version")
    5. 地址经常给多个字段:address_line1, address_line2, phone number 和email address经常给单独的表。
    6. 选择数字类型和文本类型尽量充足,否则将来数据库增长就要导致重构数据库了。
    7. 加个删除标记,删除一行是危险的,要小心的维护索引完整性。
  3. 选择键和索引
    1. 键的选择
      1. 关联的字段用外键,且外键总是关联唯一的键字段(??)
      2. 所有的键唯一
      3. 避免使用复合键
      4. 使用系统生成的主键
      5. 不可使用用户可编辑的字段做主键
      6. 把可选键进一步用做主键,可以拥有建立强大索引的能力(??)
    2. 索引的选择
      1. 不要索引大型字段(有很多字符),这样作会让索引占用太多的存储空间。
      2. 不要索引常用的小型表,如果经常插入删除,就更不要简索引了,因为表的扫描用时会小于更新索引的用时。
      3. 很多数据库自动索引主键,但是我们经常需要手工为外键建立索引。
      4. 逻辑主键使用唯一的成组索引,对系统键(作为存储过程)采用唯一的非成组索引,对任何外键列采用非成组索引。考虑数据库的空间有多大,表如何进行访问,还有这些访问是否主要用作读写 (???)
  4. 数据完整性设计(数据库逻辑设计)
    1. 完整性实现机制
      1. 实体完整性:主键
      2. 参照完整性:
        1. 父表中删除数据:级联删除;受限删除;置空值
        2. 父表中插入数据:受限插入;递归插入
        3. 父表中更新数据:级联更新;受限更新;置空值
        4. DBMS对参照完整性可以有两种方法实现:外键实现机制(约束规则)和触发器实现机制
      3. 用户定义完整性:NOT NULL;CHECK;触发器
    2. 用约束而非商务规则强制数据完整性
      采用数据库系统实现数据的完整性。这不但包括通过标准化实现的完整性而且还包括数据的功能性。在写数据的时候还可以增加触发器来保证数据的正确性。不要依赖于商务层保证数据完整性;它不能保证表之间(外键)的完整性所以不能强加于其他完整性规则之上。
    3.  强制指示完整性
      在有害数据进入数据库之前将其剔除。激活数据库系统的指示完整性特性。这样可以保持数据的清洁而能迫使开发人员投入更多的时间处理错误条件。
    4. 使用查找控制数据完整性
      控制数据完整性的最佳方式就是限制用户的选择。只要有可能都应该提供给用户一个清晰的价值列表供其选择。这样将减少键入代码的错误和误解同时提供数据的一致性。某些公共数据特别适合查找:国家代码、状态代码等。
    5.  采用视图
      为了在数据库和应用程序代码之间提供另一层抽象,可以为应用程序建立专门的视图而不必非要应用程序直接访问数据表。这样做还等于在处理数据库变更时给你提供了更多的自由。
  5.  其他设计技巧
    1.  避免使用触发器 (调试程序时造成干扰。如确实需要用,最好集中文档化)。
    2. 使用排序的英语,或编码+英语,不要只用编码命名,导致读不懂。
    3. 用一个表保存常用信息(对付客户抱怨用)
      让一个表专门存放一般数据库信息非常有用。在这个表里存放数据库当前版本、最近检查/修复(对Access)、关联设计文档的名称、客户等信息。这样可以实现一种简单机制跟踪数据库,当客户抱怨他们的数据库没有达到希望的要求而与你联系时,这样做对非客户机/服务器环境特别有用。
    4. 用一个表记录版本信息,和更改记录
    5. 写注释,写ReadMe。对所有的快捷方式、命名规范、限制和函数。
    6. 测试、测试、反复测试 让用户进行测试并且同用户一道保证选择的数据类型满足商业要求
    7. 开发期间的检查设计
      在开发期间检查数据库设计的常用技术是通过其所支持的应用程序原型检查数据库。换句话说,针对每一种最终表达数据的原型应用,保证你检查了数据模型并且查看如何取出数据。
-- Sam 18:21 15/09/2014
数据库设计的基本步骤
  1. .需求分析阶段(基础,是最困难、最耗费时间的一步)
  2. Conceptual Database Design (ER-Diagram)(关键,通过对需求综合、归纳与抽象,形成形成独立于机器,smdb产品的概念模型)
  3. Logical Database Design (Tables, Normalization etc)将E-R图转换成具体的数据库产品支持的数据模型,再根据用户处理的要求、安全性的考虑,在基本表的基础上再建立必要的视图(View), 形成数据的外模式
  4. Physical Database design (Table Indexing, Clustering etc)  (为逻辑数据模型选取最适合应用环境的物理结构(包括存储结构和存取方法), 根据DBMS特点和处理的需要,进行物理存储安排,建立索引,形成数据库内模式
  5. 数据库实施阶段 运用工具及宿主语言,根据设计结果建立数据库,编制与调试应用程序,组织数据入库,并进行试运行
  6. 数据库运行和维护阶段 在数据库系统运行过程中必须不断地对其进行评价、调整与修改

-- Sam 10:03 14/09/2014
Hibernate 的缓存
  1. 一级缓存:
    1. 当调用Session的save()、update()、saveOrUpdate()、get()或load(),以及调用查询接口的 list()、iterate()或filter()方法时,若相关对象尚不在一级缓存,则调入。 待清理缓存[evict(obj),clear],hibernate根据缓存内容的变化来更新数据库.
    2. ----always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, means wont process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.
    3. use flush and clear to avoid outofmemory issue.
  2. 二级缓存:
    1. when do the select * from table where.... hibernate save the result into second cache, then when use do query base on ID, hibernate will search the first cache, then the second level cache,, then the database, if the database is changed, hibernate will change the second level cache.
    2. 二级缓存对条件查询没有帮助。只提速针对id的查询。
    3. 不太会变的数据适合放入二级缓存。比较会变得和敏感的不适合放入缓存。
    4. 可以指定具体类是否使用二级缓存。
    5. <ehcache> 
        <!-- 当二级缓存溢出时,对象保存的临时磁盘路径 -->
              <diskStore path="java.io.tmpdir"/>
              <defaultCache
                      maxElementsInMemory="10000"
                      eternal="false"
                      timeToIdleSeconds="120"
                      timeToLiveSeconds="120"
                      overflowToDisk="true" 当缓存溢出时,对象是否保存到磁盘上.保存的磁盘路径由<diskStore>中的path指定.
                      />
      </ehcache>
-- Sam 05:35 14/09/2014
矮马,啥时数据库设计范式?
  1. 第一饭:不能有重复的列  (基本要求,不满足就不是关系数据库)
  2. 第二饭:每个表要有主键    (组合主键行不行?)
  3. The third step of normalizing the database: One Fact in One Place, contains no content which exist in other table except the foreign key.
-- Sam 05:02 14/09/2014
ACID
the properties that guarantees database's transaction is executed reliably. it refers to Atomicity, Consistency, Isolation and Durability
-- Sam 10:33 13/09/2014
Hibernate's six core interface:
  1. Session                   :   do the CRUD of the persistent objects
  2. SessionFactory      :   initialize hibernate and be the proxy of data source, and generate session.
  3. Transaction             :  an abstraction of real transaction, the real transaction can be a UserTransaction in JPA, a JDBC transaction or even a corba transaction.
  4. Query                        :  执行HQL或SQL的实例的接口,经常用来绑定查询参数,限制查询结果条数。
  5. Criteria                     :  与Query接口类似,允许创建并执行面向对象的标准化查询, 也是轻量级的,不能在Session之外使用
  6. Configuration          :  对Hibernate 进行配置,以及对它进行启动
-- Sam 12:46 13/09/2014
How to improve performance of using Hibernate.
  1. there are more than 10 different ways to generate the main key in hibernate. uuid.hex is faster than self increasing, because for self increasing, every time you insert a new record, it need to read and write database several times. 读目前最大值,读递增跨度,写新的最大值,然后才是写new记录
  2. if don't need to display on page, can save the limitation like @manyToOne, just a foreign key is OK.
  3. Contract表中有个ManyToOne fiele: Employee, use this way to save one time of query:
    1. Employee employee = new Employee();
    2. employee.setOId(oid);
    3. ContractPO.setEmployee(employee);  //用个只有1个属性的新new的Employee对象冒充下,效果一样。
  4. Open the show_sql Flag in hibernate.cfg.xml, so we can check the sql generated in control, (如果查询次数和数据量有关,则视作一个潜在的大bug)
  5. Adjust the hibernate.jdbc.fetch_size and hibernate.jdbc.batch_size
  6. make it update only the modified fields. @org.hibernate.annotations.Entity(dynamicUpdate=true,dynamicInsert=true)
  7. set the cach of hibernate.
  8. use lazyload (while, for jpa, @OneToMany is bydefault lazyload true, @ManyToOne is bydefault lazyload false. but some one said hibernate will change this behavior, need to check in future.
  9. move some initialization into startup. (like what?)
  10. normally 选择version方式作为Hibernate乐观锁.
  11. manage the session in ThreadLocal mode to avoid the data sharing error between thread.
-- Sam 16:54 13/09/2014

Please click here to login and add comments! || automatically refresh content every seconds