محاضرات sql
TRANSCRIPT
sql Software Engineering Teaching Through Game Design server
sql server management studio sql server management studio :Object exploer registered server summary
sql server management studio . : registered server .
sql server management studioobject exploer : : database Security Server object Replication Management Notification services
sql server management studio : database :- 1 -: system database sql server .
-master ) (. SQL Server .
-model
-tempdb
2 -database snapshots 3- : :- database diagram- : . tables- : . views- : .
synonyms :. programmability : .sql server server broker :5002 sql server .
storage- : . security- : .
: Security :- :Logins . :Server Roles . credentials :
: Server object : backup devices : . Endpoint : Link servers : . management studio Triggers :sql server .
: Replication : ( local publication ) (local subscriptions )
: Management sql server: maintenance plans : sql server :sql .server Activity moniter : . Data mail :
: Notification services : summary object .explorer
: :management studio database . object explorer new database . ok .
: : t-sql t-sql create database :- 1 test :
USE master GO -- Drop the database if it already exists IF EXISTS ) SELECT name FROM sys.databases WHERE name = N'test1' ( DROP DATABASE test1 GO CREATE DATABASE test1 GO
execute 5 f query executed successfully .
: :management studio database object explorer delete .ok
: : t-sql t-sql drop database :- 1 test : 1drop database test
. . : sql server
Integer: From (-9,223,372,036,854,775,808) To (9,223,372,036,854,775,807). int: From (-2,147,483,648) To (2,147,483,647) smallint: from (-32,768) To (32,767) tinyint: from 0 To 255 bit : (1,0) money : From (-922,337,203,685,477.5808) To(+922,337,203,685,477.5807) smallmoney : From -214,748.3648 To 214,748.3647 datetime : From January 1, 1753 To December 31 9999 smalldatetime : From January 1, 1900 To June 6, 2079
.
char : a maximum length of 8,000 characters varchar : maximum of 8,000 characters text : maximum length of (2,147,483,647) characters Nchar : maximum length of 4,000 characters
.
ntext: a maximum length of (1,073,741,823) characters binary: a maximum length of 8,000 bytes. varbinary: Variable-length binary data with a maximum length of 8,000 bytes. image: Variable: -length binary data with a maximum length of (2,147,483,647) bytes
: :management studio database . object explorer . tables .new table null .
open table . : customers :
Column name Id Name Phone Address City state
Data type int (Varchar)50 (Varchar)50 (Varchar)50 (Varchar)50 (Varchar)50
Allow nulls
* * * *
t-sql : use ccc CREATE TABLE [dbo].[custmer]) [id ] [int] NOT NULL, [name] [varchar])50( NULL, [address] [varchar])50( NULL, ( GO
:- :- 1- 2- insert into . 3- values . : :-
Create Tableuse studentDB insert into [T_Sem])name_Sem( values)sem 4( GO : use studentDB insert into [T_Sem] values)sem 4 '( GO
Edit the Table: :management studio modify -
: -:t-sql alter add drop . 1- : state
use studentDB alter table T_Sem add state1 varchar)50( GO state1 2- : use studentDB alter table T_Sem drop column state1 GO
Delete the Table: :management studio . . delete . ok -
: -:t-sql drop table : use studentDB drop table T_Sem GO
Exp company -: -customer Column name Id Name Address City state Data type int (Varchar)50 (Varchar)50 (Varchar)50 (Varchar)50 * * * Allow nulls
Exp-orderColumn name Id date Customer-id Data type int datetime int * Allow nulls
Exp-orderlineColumn name Id Order-id Prodcut-id quantity Data type int int int int * Allow nulls
Exp-productColumn name Id Discrption Finish On-hand Unit-price Data type int (Varchar)50 (Varchar)50 int Money * Allow nulls
constraint . :- 1- unique 2- not null 3- default 4- check 5- primary key 6- foreign key
constraint :- unique :- sport ( customer) :
constraintuse sport CREATE TABLE [dbo].[custmer]) [id ] [int] NOT NULL, [name] [varchar])50( NULL, [address] [varchar])50( NULL, constraint uk_cons1 unique )name(, ( GOName of constraint Type of constraint field
constraint: - not null . :- default (0) :-
constraint ( )items (0) :-Column name Item-id quantity Data type int int Allow nulls
constraint use sport CREATE TABLE [dbo].[item]) [item_id ] [int] NOT NULL,[quantity] [int] constraint df_cons2 default)0(
( GO
constraint: -:check (1 ) item ( )qya_s ( )int :-
constraint use sport CREATE TABLE [dbo].[item1]) [item_id ] [int] NOT NULL, [quintity] [int] , [qya_s] [int] , constraint ck_cons3 check )qya_s>0( ( GO
constraint:- primary key :-
constraint use sport CREATE TABLE [dbo].[item2]) [item_id ] [int] NOT NULL, [quintity] [int] default)0(, [qya_s] [int] , constraint pk_cons4 primary key )item_id(, ( GO
constraint : foreign key . :-
constraint ( )order Column name order-id item_id Data type int int Allow nulls
( )item_id (2 )item ()order
constraint use sport CREATE TABLE [dbo].[order]) [orde_id ] [int] NOT NULL, [item_id] [int] ,constraint fk_cons5 foreign key )item_id( references dbo.item2,
( GO
- ALTER 1- : use sport ]ALTER TABLE [item 2DROP CONSTRAINT df_cons GO
2- : use sport ALTER TABLE itemadd CONSTRAINT ck_cons11 check )quantity>=0(
GO
cascade :- on delete cascade- 1 : . on update cascade- 2 : .
use company ALTER TABLE [order]add CONSTRAINT ck_cons1101 foreign key)customerid(references dbo.customer on delete cascade
GO
: use company ALTER TABLE [order]add CONSTRAINT ck_cons1101 foreignkey )customerid( references dbo.customer on update cascade
GO
. :company
Customer Id Name Address City state prodcut Id description finish Unite price On-hand Order Id Date Customer-id
Orderline Id Order-id prodcut-id Quantiy
database diagram .new database diagram .
index : . . .
index : . .
index : : 1- ()clustered index 2- ()non clustered index 3- ()unique index 4- ()non unique index
index1- ( :)clustered index . 2- ()non clustered index .
index: (01 )test (1_ )table ( )name ( )name :
01use test create nonclustered index cind1 on (table_1)name go
index3- ()unique index . : ( )address :- 01use test (create unique index cind11 on table_1)address go
index4- ()non unique index . : ( )city :- 01use test (create index cind12 on table_1)city Go
.
index :- (.) drop : 21cind 01use test 21drop index table_1.cind Go
views (.)select (:)select :- (Select )expression( from )table name : ( )products ( ) company :-
views Column name Produc_id Name Descrip Unitprice quantity Data type int (Varchar)50 (Varchar)50 Money int * Allow nulls
views :- 1- . 2- . :- 1 use company select * from T_Product 2- use company select ID_Product,Name_Product, Descrip from T_Product
views (-:)arithmetic expressions ( )select :- 1- 2- 3- 4- 5-
views . : (01). use company 01+select Name_Product,Quentity, price from T_Product
views : :- use company 01+select name, Quentity,12*price from T_Product : :-
use company ( 01+select name, Quentity,12*)price from T_Product
views ()null ( )null ( )null ( )null . : ( )null (5)
views :- (+). : ( )firstname lastname . ( ) select (Select firstname+lastname from )table name
views ( -:)literals (.)select : ( )select select 'the frist name is:'+ fristname+'the last name is: '+lastname from )table (name
views (-:)distinct ( )select . : ( ) firsname ( )select ( )distinct ( )select .
views ) products( -: ) company( Column name Produc_id Name Descript Unitprice quantity Data type int (Varchar)50 (Varchar)50 Money int * Allow nulls
views (-:)where ... ( )where ( )from . : 03.
viewsuse company select Name_Product ,descrip from T_Product 03=where Quantity :- * .
views comparison operators
< =< > => = - ><
views :--
1- : between and ( )between ()and : 03 06. use company select price ,descrip from T_Product 06 where Quantity between 30 and
views2- -: in . : (03 32 05) use company select price ,descrip from T_Product (05,32,03)where Quantity in
views3- : like :- (%): _ : ] [ : . ] ^[:
views : :- (.)r
SELECT Name,unitprice,Descrip FROM product %where name like 'r
-
SELECT
()th %Name FROM product where name like '%th ()g %Name FROM product where name like '_g .
-
-
-
SELECT
-
views -
-: .) e( ) a( SELECT Name,unitprice,Descrip FROM product where name like '[a-e]% SELECT Name,unitprice,Descrip FROM product where name like '[^a-e]% ) g,t,a( Name,unitprice,Descrip FROM product where name like-
-
-
SELECT
'[gta]%'
views4- :is null ( )null (=) . : . select name,unitprice ,descrip from product where quantity is null (=) ( )is
views -:logical operators (.)and-or-not 1- ( :)and . : 02 51. use company select name,unitprice ,descrip from product 51=where quantity >20 and unitprice
views2- ( :)or . : 02 51. use company select name,unitprice ,descrip from product 51=where quantity >20 or unitprice
views3- ( :)not . : (810251) use company select name,unitprice ,descrip from product (81,02,51) where quantity not in
views ( :)order by . : . use company select name,unitprice ,descrip from product order by descrip ( ) desc . order by descrip desc
views* (-: )join :- . : :- 1- (:)equijoin . : :
views )depart( Column name Depnum Depname Data type Int (Varchar)50 Allow nulls
)employ( Column name Empnum empname Depnum Data type Int (Varchar)50 int Allow nulls
views . 1use test select depname,empname from depart,employ where depart.depnum=employ.depnum 2 .
views2- ( :)non equijoin . : ( )sale ( )salegarde . .Column name Garde losal hisal Data type int int int Allow nulls
views use test1 select gard,empname,losal,hisal,sale from garde,employ where employ.sale between garde.losal and garde.hisal
views3- ( :)inner join ()equijoin . :- 1use test select depname,empname from depart inner join employ on depart.depnum=employ.depnum
views4- ( :)cross join . : . 1use test select depname,empname from depart cross join employ
views5- (:)union . : . 1use test 'select depname,'employ from depart union 'select empname ,'depart from employ
views6- ( -:)except ( ). : ( )products ( (producid( )prodname ))nuitprice 04 - 07 02 05.
views use company select name,unitprice from product where unitprice between 40 and 70 except select name,unitprice from product where unitprice between 20 and 50
views7- ( :)intersect ( ). ( )intersect ( )except .
views use company select name,unitprice from product where unitprice between 40 and 70 intersect select name,unitprice from product where unitprice between 20 and 50
views : select . : (.)create view : ( )employ ( )empid( )fristname(-)lastname(- )titel 004.
views use test1 GO create view view12 AS select firstname,lastname,titel from employes where sale=400
views . ( )view ()alter : 002 . - (. )drop
views use test1 GO alter view view12 AS select firstname,lastname,titel from employes where sale=200 drop view view12
()stored procedure : ()t-sql (.)server ( )server . : :- 1- : 2- : 4- ( :)clr () visual studio.net
()stored procedure :- ( )create : use company go 1create proc dbo.ex as * select from dbo.custmer 'where name='ali go (1 ) ex ( )custmer ( )name (.)ali
()stored procedure : ()exec : 1exec ex ()insert : 1insert into custmer 1exec ex (1 )ex (1 )custmer .
()stored procedure : ( )alter : use company go 1alter proc dbo.ex as select id,address from dbo.custmer 'where name='ali Go .
()stored procedure : ( )drop :- use company go 1drop proc dbo.ex go (1 )ex (.)company
()stored procedure ()input paramete :- . (1-1-3002). :( (1 )sell ())name-quantity-sellstardate
()stored procedureuse company go 11create proc dbo.ex as select name,quantity 1from dbo.sell '3002/1/1'