天天看點

sqlserver中的一些簡單的sql語句

建立新表 -- create table t_tb(a int,b char(4),c varchr(10),d money,e int)

使用已有表字段建立新表----create table t_tbone select a,b,c from t_tb  //這樣建立的表,會把原來表中的資料複制過來,需要自己手動删除掉舊資料。

删除表----drop table t_tb

插入資料---insert into t_table(a,b,c,d,e) values(1,'2','3',4.0000,5) --插入資料時,一定要對準字段,并且注意資料類型,否則會報錯。

把一張表中的資料插入到另外一張表----insert into t_tb(a,b,c) select a,b,c from t_tbone //這裡可以指定特定的字段插入,也可以全表插入。也要注意字段對齊,和字段類型比對。

删除表中資料---delete from t_tb //全删除     delete  from t_tb where b='2' //加條件删除資料

更新表資料---update t_tb set a=2 b=3 //全部更新資料    update t_tb set a=3 where b=3 // 通過條件部分更新資料

使用者權限的配置設定   grant 權限 to 角色   權限例如 :select ,insert,delete  角色例如:mary,andy等資料庫中的管理者  收回權限--revoke 權限 from 角色  權限是:create,insert等 角色是:mary,andy等資料庫管理者

判斷資料庫中的表是否已經建立的語句:

if  not exists  (select   *   from   dbo.sysobjects   where   id   =   object_id(N'zzcz.dbo.sto_price')   and   OBJECTPROPERTY(id,   N'IsUserTable')=   1)    create   table  zzcz.dbo.sto_price(a int,b int) //如果不存在需要的表,則建立它。

清除資料庫中的日志:dump tran 資料庫表 with no_log

在程式中運作存儲過程時,一定在每一句的語句後面加一個空格。

在查詢分析器編寫sql語句和存儲過程,按F5執行,如果有多條語句,可以先選擇特定條語句,再執行。