Difference between revisions of "6238 Databaser Agenda/SQLCreateInsertSelect"

From Teknologisk videncenter
Jump to: navigation, search
(Create)
(Data types)
Line 45: Line 45:
 
==Data types==
 
==Data types==
 
CHARACTER(n) Character string. Fixed-length n
 
CHARACTER(n) Character string. Fixed-length n
 +
 
VARCHAR(n) Character string. Variable length. Maximum length n
 
VARCHAR(n) Character string. Variable length. Maximum length n
 +
 
BOOLEAN         Stores TRUE or FALSE values
 
BOOLEAN         Stores TRUE or FALSE values
 +
 
INTEGER         Integer numerical (no decimal). Precision 10
 
INTEGER         Integer numerical (no decimal). Precision 10
 +
 
FLOAT         Approximate numerical, mantissa precision 16
 
FLOAT         Approximate numerical, mantissa precision 16
 +
 
DATETIME Stores year, month, day,hour, minute, and second values
 
DATETIME Stores year, month, day,hour, minute, and second values
  

Revision as of 14:25, 30 October 2015

6238 Databaser Agenda SQLCreateInsertSelect

SQL

6238 Databaser Agenda SQLCreateInsertSelect1.gif

SQL vs T-SQL

SQL er et declarativt sprog. Det betyder at vi beskriver hvad vi vil have, men ikke hvordan det skal ske.

C#, Java etc. Er procedurale sprog hvor vi beskriver step for step hvad der skal ske.

T-SQL er SQL med procedurale add ons (Stored procedures).

Saelger database

6238 Databaser Agenda SQLCreateInsertSelect2.png

6238 Databaser Agenda SQLCreateInsertSelect5.png 6238 Databaser Agenda SQLCreateInsertSelect7.png

6238 Databaser Agenda SQLCreateInsertSelect4.png 6238 Databaser Agenda SQLCreateInsertSelect3.png 6238 Databaser Agenda SQLCreateInsertSelect6.png

Create

Herunder oprettes tabellerne Saelger og TelefonNr. Bemærk hvordan TelfonNr får tildelt en FOREIGN KEY til Saelger.

CREATE TABLE Saelger
(
	Navn 	NVARCHAR(20)	NOT NULL,
	StartDato	DATETIME	NOT NULL,
	Email	NVARCHAR(20)	NOT NULL,
	MedarbNr	INT	NOT NULL,
	PRIMARY KEY(MedarbNr)
);

CREATE TABLE TelefonNr
(
	Nr 	NVARCHAR(8) 	NOT NULL,
	Saelger	INT 	NOT NULL,
	PRIMARY KEY(Nr),
	FOREIGN KEY(Saelger) REFERENCES Saelger(MedarbNr)
);

Data types

CHARACTER(n) Character string. Fixed-length n

VARCHAR(n) Character string. Variable length. Maximum length n

BOOLEAN Stores TRUE or FALSE values

INTEGER Integer numerical (no decimal). Precision 10

FLOAT Approximate numerical, mantissa precision 16

DATETIME Stores year, month, day,hour, minute, and second values

Insert

INSERT INTO Saelger(Navn,StartDato,Email,MedarbNr) 
	VALUES ('Anders','20010501','anders@mail.dk',1);

Update

UPDATE Saelger SET Navn=Anders WHERE MedarbejderNr=1;

Delete

DELETE FROM Saelger WHERE MedarbejderNr=1;

Select

/* Vælg alt fra tebellen Saelger */
SELECT * FROM Saelger;

/* Vælt alt fra tabellen Saelger hvor navnet er Anders */
SELECT * FROM Saelger WHERE Navn = 'Anders';

/* Vælg MedarbNr fra Saelger hvor navnet er Anders */
SELECT MedarbNr FROM Saelger WHERE Navn = 'Anders';