一些工作和學習中經(jīng)常用到的sql語句

字號:


    一些常用地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 ,
    )
    新建表:
    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 [表名]
    插入數(shù)據(jù):
    insert into [表名] (字段1,字段2) values (100,'51windows.net')
    刪除數(shù)據(jù):
    delete from [表名] where [字段名]>100
    更新數(shù)據(jù):
    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數(shù)據(jù)庫中重命名表)
    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 默認值名