SqlServer2012-创建表、删除表 增加字段 删除字段操作

新建表:
create table [表名]
(
[自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,
[字段1] nVarChar(50) default \‘默认值\‘ null ,
[字段2] ntext null ,
[字段3] datetime,
[字段4] money null ,
[字段5] int default 0,
[字段6] Decimal (12,4) default 0,
[字段7] image null ,
)

删除表:
Drop table [表名]

插入数据:
INSERT INTO [表名] (字段1,字段2) VALUES (100,\‘51WINDOWS.NET\‘)

删除数据:
DELETE FROM [表名] WHERE [字段名]>100

更新数据:
UPDATE [表名] SET [字段1] = 200,[字段2] = \‘51WINDOWS.NET\‘ WHERE [字段三] = \‘HAIWA\‘

新增字段:
ALTER TABLE [表名] ADD [字段名] NVARCHAR (50) NULL

删除字段:
ALTER TABLE [表名] DROP COLUMN [字段名]

修改字段:
ALTER TABLE [表名] ALTER COLUMN [字段名] NVARCHAR (50) NULL

重命名表:(Access 重命名表,请参考文章:在Access数据库中重命名表)
sp_rename \‘表名\‘, \‘新表名\‘, \‘OBJECT\‘

新建约束:
ALTER TABLE [表名] ADD CONSTRAINT 约束名 CHECK ([约束字段] <= \‘2000-1-1\‘)

删除约束:
ALTER TABLE [表名] DROP CONSTRAINT 约束名

新建默认值
ALTER TABLE [表名] ADD CONSTRAINT 默认值名 DEFAULT \‘51WINDOWS.NET\‘ FOR [字段名]

删除默认值
ALTER TABLE [表名] DROP CONSTRAINT 默认值名

删除Sql Server 中的日志,减小数据库文件大小
dump transaction 数据库名 with no_log
backup log 数据库名 with no_log
dbcc shrinkdatabase(数据库名)
exec sp_dboption \‘数据库名\‘, \‘autoshrink\‘, \‘true\‘

\\\‘添加字段通用函数
Sub AddColumn(TableName,ColumnName,ColumnType)
Conn.Execute(\"Alter Table \"&TableName&\" Add \"&ColumnName&\" \"&ColumnType&\"\")
End Sub

\\\‘更改字段通用函数
Sub ModColumn(TableName,ColumnName,ColumnType)
Conn.Execute(\"Alter Table \"&TableName&\" Alter Column \"&ColumnName&\" \"&ColumnType&\"\")
End Sub

\\\‘检查表是否存在

sql=\"select count(*) as dida from sysobjects where id = object_id(N\‘[所有者].[表名]\‘) and OBJECTPROPERTY(id, N\‘IsUserTable\‘) = 1\"

set rs=conn.execute(sql)

response.write rs(\"dida\")\‘返回一个数值,0代表没有,1代表存在

判断表的存在:
select * from sysobjects where id = object_id(N\‘[dbo].[tablename]\‘) and OBJECTPROPERTY(id, N\‘IsUserTable\‘) = 1

某个表的结构
select * from syscolumns where id = object_id(N\‘[dbo].[你的表名]\‘) and OBJECTPROPERTY(id, N\‘IsUserTable\‘) = 1

create table student(
Sno int not null primary key,
Sname char(10)not null,
Ssex bit not null,
Sage tinyint not null,
Sdept char(20) not null)

create table course(
Cno int not null primary key,
Cname char(20)not null,
Cpno int not null,
Ccredit tinyint not null)

create table sc(
Sno int not null,
Cno int not null,
Grade tinyint not null
foreign key(Sno)references student(Sno)
foreign key(Cno)references course(Cno)
)

(1)
seleCt top 1 S.sno,sname
from SC,S
where Cno=‘C2‘ and SC.sno=S.sno
order by grade desC;

(2)
seleCt sname,age
from Student,SC
where SC.sno not in(
seleCt SC.sno
from SC
where Cno=‘C2‘ )and SC.sno=S.sno;
(3)
seleCt sno, avg(grade) as average
from SC
group by sno
having(avg(grade)>80);
(3)法二
seleCt sno, avg(grade) ‘ average‘
from SC
group by sno
having(avg(grade)>80);

(4)
delete from SC 
where SC.sno in(
   seleCt sno
   from S
   where sname=‘S5‘);
(5)
seleCt sname
from S
where sdept=‘英语‘and sex=‘男‘;
(6)
seleCt SC.sno,avg(grade) as average
from S,SC
where S.sno=SC.sno
group by SC.sno;

原文地址:https://www.cnblogs.com/javier520/p/10943435.html

时间: 2024-08-27 12:40:31

SqlServer2012-创建表、删除表 增加字段 删除字段操作的相关文章

触发器实现多表之间的增加、删除及更新

常见的触发器有三种:分别应用于Insert,Update,Delete事件 1.数据同步增加: 如有两张表:A表和B表,创建触发器使当A表插入数据后B表也同步插入数据.其中B表插入数据的字段需要同A表中的字段相对应. 1 create trigger 触发器名称 2 on A表 3 after insert 4 as 5 begin insert into B表(B表字段1,B表字段2,B表字段3) 6 select A表字段1,A表字段2,A表字段3 7 from inserted 8 end

C#创建、读写、增加、删除XML操作

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;using System.Collections; namespace CommandXML{    public class cmdXML    { /// <summary>        /// 创建XML文件        /// &

linux命令4 find、三个时间属性、硬链接和软连接、用户名和密码文件、增加和删除用户组、usermod、su、sudo

find搜索命令 其他搜索命令如下: [[email protected] 333]# which ls alias ls='ls --color=auto' /bin/ls [[email protected] 333]# echo $PATH /usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/usr/local/jdk1.6.0_23/b

sql 创建表、删除表 增加字段 删除字段操作

[转]sql 创建表.删除表 增加字段 删除字段操作 下面是Sql Server 和 Access 操作数据库结构的常用Sql,希望对你有所帮助. 新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default \'默认值\' null ,[字段2] ntext null ,[字段3] datetime,[字段4] money null ,[字段5] int default 0,[

oracle删除表字段和oracle表增加字段

这篇文章主要介绍了oracle表增加字段.删除表字段修改表字段的使用方法,大家参考使用吧 添加字段的语法:alter table tablename add (column datatype [default value][null/not null],….); 修改字段的语法:alter table tablename modify (column datatype [default value][null/not null],….); 删除字段的语法:alter table tablenam

Django 数据库ORM操作 - 单表的创建,增加,删除,更改和查询

Django里面,管理数据库和sqlarchemy类似,也是通过orm框架来实现的.所有的数据库的建立,都是在model.py里面通过类来实现的. 首先看看如何创建一个单表: a. 先定义一个类,继承models.Model, 然后根据需求定义参数,这些参数的类型和变量后面会进一步阐述 models.py from django.db import models class UserInfo(models.Model):     username = models.CharField(max_l

oracle 增加修改删除表字段,添加修改表、以及表中字段的备注

添加字段的语法:alter table tablename add (column datatype [default value][null/not null],-.); 修改字段的语法:alter table tablename modify (column datatype [default value][null/not null],-.); 删除字段的语法:alter table tablename drop (column); 添加.修改.删除多列的话,用逗号隔开. 举例: 使用al

SQL增加,删除,更改表中字段

1. 向表中添加新的字段 alter  table  table_name  add  column_name  varchar2(20) not null 2. 删除表中的一个字段 delete table table_name column column_name 3. 修改表中的一个字段名 alter table table_name rename column oldname to newname 4. 添加主键约束   alter table 表名   add constraint 约

MySQL:创建、修改和删除表

其实对很多人来说对于SQL语句已经忘了很多,或者说是不懂很多,因为有数据库图形操作软件,方便了大家,但是我们不能忘记最根本的东西,特别是一些细节上的东西,可能你用惯了Hibernate,不用写SQL语句,但是不是任何项目都要用到大框架的,如果不用,那你是不是就不会操作数据库了呢,所以我们最好还是熟悉一点好,对我们以后找工作和工作都有帮助. 在说创建.修改和删除表前,我们还是要进行一个操作的简单说明: 1.登陆数据库系统 在命令行中登陆MySQL数据库管理系统,输入一下内容: mysql -h l