sqlserver oracle mysql数据库
最近做项目从前台转到后台,于是开始研究一下这些数据库,现在先把这几天看的写一点,稍后在做总结,方便自己和他人日后查看 ? ? 什么是规划?规划就是与数据库某个用户相关联的数据库对象集合不同的表访问规划 时必须带上表明,而同一个表则不用。user1访问自己的table ? ?则table,user1访问user2的table2时 ? 则user2.table2 ? ?。不同的规划可以具有名称相同的表。其次就是字段和行了,这个没有什么好说的,注意字段就是列。 ? ?create table则是创建表,例如create table employ_tbl(emp_id char(9) notnull,emp_name varchar(40) not null,emp_zip integer(5) null,emp_pager integer(10) null ); 上述代码同时适用于SQLserver,oracle,MySQL, 注意列的默认属性是null,所以在创建的时候不必明确设置,但notnull必须明确指定。 ? alter table命令,对其进行修改,可以添加列,删除列,修改定义,和添加去除约束,例如 alter table的命令标准 alter tabletable_name [modify] [column_name] [datatype|null not null] ? ? ? [restrict|cascade] ? ? ? ?[drop] [constraint constraint_name] ? ?[add] [column] column definition ? ? MySQL提供serial方法为表生成真正的唯一值 create table test_incerement(id ?serial,?test_name verchar(20)); ? SQLserver中可以使用identity类型 ?例如 crate table test_incerement(id int identity(1,1) notnull,? ?test_name varchar(20)) ? oracle 则 insert into?test_incerement(test_name) values ('fred'),('joe'),('mike'),('ted'); ? ? 即? ID test_name 1 ?fred 2 ?joe 3 ?mike 4 ?ted 从现有表创建另一个表:crate table ?new_table_name as select [* | column1,coulmn2 ] from ?table_name [ where ] ?但是SQLserver 却不一样 ?select [ * | column1,column2] into new_table_name from table_name [ where ] 接下来基于这个查询创建名为products_tmp 表 ? create table?products_tmp as select ?* ?from?products_tb1; sqlserver 使用 select * into?products_tmp ?from?products_tb1; ? table created; 注意从现有表创建新表,新表与原始表具有一样的属性。 删除表 ?: ?drop table table_name [restrict| | cascade] ? ? ? ? ? ? ? ? ? ? ? ? ? ? drop table?products_tmp ; 完整性约束 ? 数据的准确性和一致性 :主键约束 primary key,唯一约束,外键约束 ? 下面说说外键约束:是确保表与表之间引用完整性的主要机制。 例如: ? ?create table?employ_pay_tbl(emp_id char(9) notnull,?position?varchar(15) notnull,?date_hire date null,) ?constraint ?emp_id_fk ?foreign key (emp_id) references?employ_tbl (emp_id); 这被称为“父子表”,父亲是employ_tbl,子表是employ_pay_tbl。为了在子表里插入一个emp_id,首先要在父表的emp_id里存入,类似父表里删除一个emp_id的值,子表里相应的emp_id值必须全部删除,这就是引用完整性概念。 使用alter table命令向表里添加外键: alter table?employ_pay_tbl ?add constraint ?id_fk ?foreign ?key ?(emp_id) ?references ?employ_tbl ?(emp_id) ? 检查约束:检查(CHK)约束用于检查输入到特定字段的数据的有效性。。。。例如oracle中: ?create ?table?employ_check_tst (emp_id char(9) notnull,emp_pager integer(10) null,?primary ?key ?(emp_id),?constraint chk_emp_zip check (emp_zip='46234')); ? ??emp_zip设置检查约束,确保输入到这个表里的Zip代码都是‘46234’。 如果想利用检查约束来确保Zip代码属于某个值得列表,: ?constraint ?chk_emp_zip check ?(emp_zip ?in ?('46234','46227','46745')); 去除约束: alter table employees drop constraint employees_pk; ? ? ? ? ? table altered; 而MySQL则?alter table employees drop ?primary key; ? ? 好了先写到这里,改天学习了,再来分享,做笔记! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |