sql建立表

來源:互聯網
上載者:User

下面是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)
seleCt S.sname as 姓名 ,grade as 成績 ,C.cname as 選修課程
from SC,S,C
where S.sno=SC.sno and SC.cno=C.cno and SC.cno in(
     seleCt cno
     from C
     where cname='DB');
(8)
select TOP 1 sno as 學號,grade as 分數,cname as 課程名
from SC,C
where SC.cno=C.cno and cname='OS'
order by grade desc;
(9)
select Sname
from   S
where not exists(
      select *
      from SC
      where Sno=S.Sno and Cno=1);
(10)
select Sname
from S
where not exists(
      select *
      from C
      where not exists(
            select *
            from SC
            where Sno=S.Sno and Cno=C.Cno));

(11)
select distinct Sno
from SC,SCX
where not exists(
      select *
      from SC SCY
      where SCY.Sno=95001 and
        not exists(
            select *
            from SC SCZ
            where SCZ.Sno=SCX.Sno and SCZ.Cno=SCY.Cno));
(12)
select top 3 Cno as 課程號, Sno
from SC
where Cno=1
order by Grade desc;

 

create database stu

use stu

create table S
(
sno char(6),
sname char(10),
age int,
sex char(2),
constraint PK_S primary key (sno),
constraint CK_age check(age>=0 and age<=150)
)

create table C
(
cno char(8),
cname char(16),
credit int,
constraint PK_C primary key (cno),
constraint CK_credit check (credit>=0)
)

create table SC
(
sno char(6),
cno char (8),
constraint PK_SC primary key (sno,cno),
constraint FK_s foreign key (sno) references S(sno),
constraint FK_c foreign key (cno) references C(cno)
)

insert into S values ('001','zhang',19,'男')

insert into S values('002','li',16,'女')

select * from S

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.