下面是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,
[字段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;

(7)

sql 创建表、删除表 增加字段 删除字段操作的更多相关文章

  1. 第16课-数据库开发及ado.net-数据库SQl,创建数据库和表,增删改语句,约束,top和Distinct,聚合函数介绍

    第16课-数据库开发及ado.net 数据库SQl,创建数据库和表,增删改语句,约束,top和Distinct,聚合函数介绍 SQL语句入门(脚本.命令) SQL全名是结构化查询语言(Structur ...

  2. oracle常用SQL——创建用户、表空间、授权(12C)

    一.查询 查询用户所属 表空间 select username,default_tablespace from dba_users where username='xxx' 查询表空间情况 SELEC ...

  3. ORACLE中通过SQL语句(alter table)来增加、删除、修改字段

    1.添加字段: alter table  表名  add (字段  字段类型)  [ default  '输入默认值']  [null/not null]  ; 2.添加备注: comment on ...

  4. SQL创建数据库、表、存储过程及调用

    --如果存在数据库PRogrammerPay  就删除 if exists (select * from sysdatabases where name='programmerPay') drop d ...

  5. 2016年11月14日--SQL创建数据库、表-查、插、删、改

    --创建数据库(create database 数据库名)create database hq20161114go --使用选择数据库(use 数据库名)use hq20161114go --创建学生 ...

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

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. mysql 怎么给一个表一次增加多个字段, mysql 添加 多个 字段

    sql 语句: ALTER TABLE oversea_liveauctioneers_detail_info_2018 ADD `result` LONGTEXT, ADD `buyer_premi ...

  8. SQL - 创建一个学生表,要求有主键约束和非空约束

    CREATE TABLE [dbo].[Student] ( [ID] [int] NOT NULL, [Name] [nchar](10) NOT NULL, [Age] [int] NOT NUL ...

  9. Sqlserver2012 使用sql语句增加(或删除)表一个字段

    前言 Mark在SqlServer 2012 的数据库使用sql语句增加(或删除)一张表的一个字段. 使用Sql语句增加表的一个字段 [1]语法: alter table table_name add ...

随机推荐

  1. JAVA中如何使用SORT从大到小排

    import java.util.Arrays;import java.util.Collections;public class Test { public static void main(Str ...

  2. Selenium-java-TestNg-的运行

    package com.day.www; import org.testng.annotations.AfterClass;import org.testng.annotations.AfterMet ...

  3. Win10升级后回退后无法检测新版本的修复办法

    笔记本原来装的是Win10 10240版本,升级到14393版本后进行了回退.回退后,Win10系统再也检测不到新版本更新了.   解决办法如下: 1.打开注册表:HKEY_LOCAL_MACHINE ...

  4. 微信支付开发(1) JS API支付

    关键字:微信支付 微信支付v3 jsapi支付 统一支付 Native支付 prepay_id 作者:方倍工作室原文: http://www.cnblogs.com/txw1958/p/wxpayv3 ...

  5. jquery-leonaScroll-1.2-自定义滚动条插件

    leonaScroll-1.2.js 下载链接地址:http://share.weiyun.com/bb531dd6b1916c0023c176897182dc15 (密码:iZck)[内含压缩版] ...

  6. 动画的使用—View Animation

    View Animation定义了下面的四种动画效果: 缩放(scale).位移(translation).旋转(rotation).透明(alpha)   缩放动画: ScaleAnimation( ...

  7. transformClassesWithDexForDebug

    转自:http://blog.sina.com.cn/s/blog_6f3828770102w30b.html

  8. MyBatis的foreach标签与SUM函数同时使用

    最近在项目中遇到一个,需要根据传入的存有id的list,计算值,再起别名 <if test="channelList != null and channelList.size()> ...

  9. jQuery瀑布流简单示例

    1,以下demo是基于window的滚动  

  10. php 5.4中php-fpm 的重启、终止操作命令

    php 5.4中php-fpm 的重启.终止操作命令: 查看php运行目录命令:which php/usr/bin/php 查看php-fpm进程数:ps aux | grep -c php-fpm ...