SQL Server 和 Oracle对字段的操作

来源:互联网 发布:怎样删自己的淘宝好评? 编辑:程序博客网 时间:2024/06/06 02:02
SQL Server:
        --创建主键约束
alter table [table_name] add constraint [主键约束名]  primary key clustered (列名) clustered:成群的;聚集成群的、聚合、聚集索引的创建和使用 

--创建外建约束
alter table [table_name][with nocheck] add constraint [外键约束名] foreign key (列名) references 表名(列名)
alter table stuinfo add constraint Fk_classid foreign key(classid) references classes(cid);
--创建检查约束
alter table stuinfo add constraint s_age check (age between 18 and 30);
alter table stuinfo add constraint CK_sex check(sex in('男','女'));
--添加默认值
alter table emp add constraint DF_sex default '男' for sex;
 
--删除约束
alter table users drop CK_age
--删除主键约束
alter table users drop constraint PK_uid;

--重命名表:
Exec  sp_rename  'old_table_name',  'new_table_name'
--重命名表,customers为原表名,custs为新表名     
EXEC sp_rename 'stuinfo', 'stus';
    
--重命名列: 
Exec  sp_rename  'table_name.old_name', 'new_name','column'
--重命名列名,stuinfo为表名,Id为要修改的列名,stuid为新列名 
EXEC  sp_rename  'stuinfo.Id',   'stuid',   'COLUMN'; 


--添加新列: 
Alter  table  [table_name]  add  [new_column]  varchar(33) 
--更改列的数据类型:
Alter  table  [table_name]   alter  column  [column_name]  [data_type]

--删除列:
alter  table  [table_name]  drop  column  [column_name]
--删除表:
drop  table  table_name 
Truncate table [table_name]

--删除classes中的desces列
alter table classes drop column desces;

--增加列  在classes表中增加一列desces
alter table classes add desces varchar(20) default ' ';

--修改列的属性
alter table classes alter column cname varchar(60) not null;

--创建好表后,用下面的存储过程来查看表结构
exec sp_help table_name
exec sp_help classes;

Oracle: 
    --创建主键约束
alter table [table_name] add constraint [主键约束名]  primary key(列名);
alter table sales add constraint PK_sid primary key(sid);

--创建外建约束
alter table [table_name] [with nocheck] add constraint [外键约束名] foreign key(列名) references 表名(列名)
alter table stuinfo add constraint FK_classid foreign key(cid) references classes(cid);
--创建检查约束
alter table stuinfo add constraint s_age check (age between 18 and 30);
alter table stuinfo add constraint CK_sex check(sex in('男','女'));
--添加非空约束
alter table stuinfo modify sname not null;  --modify:修改
--查看约束
select * from user_constraints [where table_name='大写表名'];
--禁用或启动约束
alter table stuInfo disable|enable constraint FK_classid;
--删除约束
alter table stuInfo drop constraint FK_classid;
--重命名表:
alter table table_name rename to new_table_name;
--重命名表,customers为原表名,custs为新表名     
alter table customers rename to custs;
    
--重命名列: 
alter table table_name rename column old_column_name to new_column_name;

--重命名列名,stuinfo为表名,Id为要修改的列名,stuid为新列名 
alter table stuinfo rename column Id to stuid;


--添加新列: 
alter table table_name add column_name data_type [default value][null/not null],...;
alter table sale add sname number(10,2) default 0.0;
--更改列的数据类型:
alter table table_name modify column_name data_type [default value][null/not null],...;
alter table sale modify sname decimal(10,8);

--删除列:
alter table table_name drop column_name;
alter table sale drop column sname;
--删除表:
drop  table  table_name;

--创建好表后,查看表结构
select * from user_tab_columns where table_name='大写表名';
0 0
原创粉丝点击