Sql Server基本文操作

5509 ワード

1.データベースフォーマットの作成:create databaseデータベース名on primary(...)log on(...)
create database testdb01
on primary(
name=testdb01,
filename='E:\testdb\testdb01.mdf',
size=5,
maxsize=100,
filegrowth=5          --             
)
log on(
name=testdb01_log,
filename='E:\testdb\testdb01_log.ldf',
size=5,
maxsize=100,
filegrowth=5
)

--    :         testdb   ,              。

2.データベースフォーマットの削除:drop databaseデータベース名
3.データベース名のフォーマットの変更:alter database旧データベース名modify name=新データベース名
alter database testdb02
modify name=testdb01

4.データベース・ファイルのプロパティ・フォーマットの変更:alter databaseデータベース名modify file(
…)
alter database testdb01
modify file(
name=testdb01,        --        ,            
size=11,
maxsize=100,
filegrowth=10
)

5.データベース・プロパティ・フォーマットの表示:exec sp_helpdbデータベース名
exec sp_helpdb testdb01

6.表形式の作成:create table表名(列名データ型制約タイプが空かどうか、列明データ型が空かどうか)
create table stu(
stuID int primary key not null,
name varchar(10) not null,
age int null
)

    :         

7.削除表形式:drop table表名
drop table stu

8.–列タイプフォーマットの変更:alter tableテーブル名alter column列名データ型
alter table stu_infor
alter column name varchar(20)

9.列が空のフォーマットであるかどうかを変更する:alter tableテーブル名alter columnフィールド名データ型が空であるか(not nullまたはnull)
alter table stu_infor
alter column age float not null

10.フィールドフォーマットの追加:alter tableテーブル名addフィールド名データ型が空かどうか(not nullまたはnull)
alter table stu_infor
add grade varchar(10) not null

11.プライマリ・キー制約フォーマットの追加:alter tableテーブル名add constraint制約名primary key(プライマリ・キーを追加するフィールド名)
alter table stu_infor
add constraint KI primary key (stuID)     --   stuID    ,       ,     KI


12.外部キー制約フォーマットの追加:alter tableテーブル名add constraint外部キー名foreign key(外部キーを追加するフィールド名)references依存テーブル名(フィールド名)
--      
alter table stu
add constraint fk foreign key (age) references stu_infor(ID) 
  stu   age     fk    stu_infor    

13.フィールド名フォーマットの変更:exec sp_表名フィールド名','新しい名前','column'
exec sp_rename 'stu_infor.stuID','ID','column'
-- stu_infor   stu_ID           ID

14.betweenクエリーの範囲内のレコード
select *from stu_infor
where age between '20' and '23'
--    :between           ,      


select *from stu_infor
where name between '  'and '  '

--      65 75     
select *from stu_infor
where grade not between '65' and '75'

--      
select getdate()

15.deleteレコード
--     3    
delete stu_infor
where ID=3

16.where精密制限
--where    
update stu_infor 
set name='  '
where ID=1

17.ファジイクエリlike
select *from stu_infor       --             
where name like ' %'


select *from stu_infor       --             
where name like '% '

18.dinstinct
--           
select distinct grade from stu_infor

19.topクエリー・テーブルの前の行のデータ・フォーマット:select topデータ・フィールドfromテーブル名
--          
select top 2 * from stu_infor

20.update表のデータフォーマットの変更:update表名setフィールド=入力値whereフィールド=値
--         
update stu_infor
set name='  '
where ID=4

21.insertレコードフォーマットをテーブルに挿入する:insert intoテーブル名(フィールド)values(値)
--         
insert into stu_infor(ID,name,age,grade)
values(01,'  ',20,60)


--         
insert into stu_infor(ID,name,age,grade)
values(02,'  ',21,65),
(03,'   ',22,70),
(04,'   ',23,75)

--    :         ,             。

22.テーブル内の外部キー制約フォーマットの削除:alter tableテーブル名drop constraint制約名
--        fk
alter table stu drop constraint fk

23.クエリー・テーブルの一部のフィールド・フォーマット:selectフィールド、フィールド、フィールドfromテーブル名
--         
select ID,name from stu_infor

24.サブクエリinの使用方法:select*fromテーブル名whereフィールドin(値セット)
--   in   ,         ,        
select *from stu_infor
where ID in (1,2,3)
--      ID 1,2,3   


--           ,        
select * from stu_infor
where name in ('  ','  ','  ')


--      ID  1,2,3,4,5   
select * from stu_infor
where ID not in (1,2,3,4,5)

25.as表/列に名前のフォーマットを付ける:select*from表名as名前
フォーマット:selectフィールドas別名フィールドas別名フィールドas別名フィールドas別名fromテーブル名
----     
select * from stu_infor as A



--     
select ID as   ,
name as    ,
grade as   
from stu_infor

--    :                    ,           
-- :
select *from (select *from stu_infor where age between 21 and 25) as A


26.existsサブクエリ
27.インデックスindexインデックスの目的:クエリー効率の向上インデックスは2種類に分けられる:集計インデックス:実際の物理記憶順序と論理順序が一致し、1つのテーブルに1つの集計インデックスしか存在しない非集計インデックス:実際の物理記憶順序と論理順序が一致せず、1つのテーブルには多くの非集計インデックス一意インデックス、ビューインデックス、全文インデックス、xml
例えば、張三はある表の1位にランクされています.年齢順に並べば、張三も1位にランクされています.これが集約インデックスです.
インデックスを追加すると、追加のストレージ容量が増加し、増加、削除、変更、検索の効率が低下します.
集計インデックスフォーマットの作成:create clustered indexインデックス名onテーブル名(フィールド)
---   ID          
create clustered index ixhaha on stu_infor(ID) 

削除インデックスフォーマット:drop indexテーブル名.フィールド
--    
drop index stu_infor.ixhaha

非集計インデックスフォーマットの作成:create nonclustered indexインデックス名onテーブル名(フィールド)
--       
create nonclustered index xixi on stu_infor(name)