Oracle和Sqlserver增加修改删除字段约束.docx_第1页
Oracle和Sqlserver增加修改删除字段约束.docx_第2页
Oracle和Sqlserver增加修改删除字段约束.docx_第3页
Oracle和Sqlserver增加修改删除字段约束.docx_第4页
Oracle和Sqlserver增加修改删除字段约束.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Oracle 增加修改删除字段 添加字段的语法:alter table tablename add (column datatype default valuenull/not null,.);修改字段的语法:alter table tablename modify (column datatype default valuenull/not null,.);删除字段的语法:alter table tablename drop (column);添加、修改、删除多列的话,用逗号隔开。使用alter table 来增加、删除和修改一个列的例子。创建表结构:create table test1(id varchar2(20) not null);增加一个字段:alter table test1add (name varchar2(30) default 无名氏 not null);使用一个SQL语句同时添加三个字段:alter table test1add (name varchar2(30) default 无名氏 not null,age integer default 22 not null,has_money number(9,2);修改一个字段alter table test1modify (name varchar2(16) default unknown);另:比较正规的写法是:- Add/modify columnsalter table TABLE_NAME rename column FIELD_NAME to NEW_FIELD_NAME;删除一个字段alter table test1drop column name;需要注意的是如果某一列中已经存在值,如果你要修改的为比这些值还要小的列宽这样将会出现一个错误。例如前面如果我们插入一个值insert into test1values (1,我们很爱你);然后曾修改列: alter table test1modify (name varchar2(8);将会得到以下错误:ERROR 位于第 2 行:ORA-01441: 无法减小列长度, 因为一些值过大-高级用法:重命名表ALTER TABLEtable_nameRENAME TOnew_table_name;修改列的名称语法:ALTER TABLE table_name RENAME COLUMN supplier_name to sname;范例:alter table s_dept rename column age to age1;附:创建带主键的表create table student (studentid int primary key not null,studentname varchar(8),age int);1、创建表的同时创建主键约束(1)无命名create table student (studentid int primary key not null,studentname varchar(8),age int);(2)有命名create table students (studentid int ,studentname varchar(8),age int,constraint yy primary key(studentid);2、删除表中已有的主键约束(1)无命名可用 SELECT * from user_cons_columns;查找表中主键名称得student表中的主键名为SYS_C002715alter table student drop constraint SYS_C002715;(2)有命名alter table students drop constraint yy;3、向表中添加主键约束alter table student add constraint pk_student primary key(studentid);-转载自/blog/static/163683341201112655637737/版权给原创所有!Sql Server 增 删 SELECT AS 表名, AS 字段名,cast(ep.value as varchar(100) AS 字段说明 FROM sys.tables AS t INNER JOIN sys.columns AS c ON t.object_id = c.object_id LEFT JOIN sys.extended_properties AS ep ON ep.major_id = c.object_id AND ep.minor_id = c.column_id WHERE ep.class =1 and =table1 or =table2 and in (table2字段,table2字段) or in (table1字段,table1字段) 2.添加字段的名称EXEC sys.sp_addextendedproperty name=NMS_Description, value=N字段说明 , level0type=NSCHEMA,level0name=Ndbo, level1type=NTABLE,level1name=N表名, level2type=NCOLUMN, level2name=N字段名 GO 3.修改字段的名称BEGIN TRANSACTION GO DECLARE v sql_variant SET v = N说明信息 EXECUTE sys.sp_updateextendedproperty NMS_Description, v, NSCHEMA,Ndbo,NTABLE,N表名, NCOLUMN, N字段名 GO COMMIT 4.查询数据库字段信息和类型select as zdname,a.length, as zdtype from syscolumns a,systypes b,sysobjects c where a.xtype=b.xtype and a.id=c.id and = table -没有过滤系统字段信息 select ,a.length, from syscolumns a,systypes b,sysobjects c where a.xtype=b.xtype and a.id=c.id and = table AND B.NAME!=SYSNAME -过滤了系统字段信息 select ,a.length, from syscolumns a,systypes b,sysobjects c where a.xtype=b.xtype and a.id=c.id and = table and charindex(sysname,) = 0 -过滤了系统字段信息约束根据不同的需要,sql server数据库中不同的表之间有的将有所关联,下面将为您介绍sql server中如何增加约束,并删除的方法,供您参考。sql server中增加约束的语句是:-添加主键约束alter table 表名add constraint 约束名 primary key (主键)-添加唯一约束alter table 表名add constraint 约束名 unique (字段)-添加默认约束alter table 表名add constraint 约束名 default (默认内容) for 字段-添加检查check约束,要求字段只能在1到100之间alter table 表名add constraint 约束名 check (字段 between 1 and 100 )-添加外键约束(主表stuInfo和从表stuMarks建立关系,关

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论