Categoria De Tipos De Datos Sql Server
Los tipos de datos de SQL Server se organizan en las siguientes categorías:
Numéricos exactos | Cadenas de caracteres Unicode |
Numéricos aproximados | Cadenas binarias |
Fecha y hora | Otros tipos de datos |
Cadenas de caracteres | |
En SQL Server, según las características de almacenamiento, algunos tipos de datos están designados como pertenecientes alos siguientes grupos:
* Tipos de datos de valores grandes: varchar(max), nvarchar(max) y varbinary(max)
* Tipos de datos de objetos grandes: text, ntext, image, varchar(max), nvarchar(max), varbinary(max) y xml
Numéricos exactos
bigint | numeric |
bit | smallint |
decimal | smallmoney |
int | tinyint |
money | |
Numéricos aproximados
float | real |
Fecha y hora
date| datetimeoffset |
datetime2 | smalldatetime |
datetime | time |
Cadenas de caracteres
char | varchar |
text | |
Cadenas de caracteres Unicode
nchar | nvarchar |
ntext | |
Cadenas binarias
binary | varbinary |
image | |
Otros tipos de datos
cursor | timestamp | table |
hierarchyid | uniqueidentifier | |
sql_variant | xml | |
| |
Tipo de datos | Intervalo| Almacenamiento |
bigint | De -2^63 (-9.223.372.036.854.775.808) a 2^63-1 (9.223.372.036.854.775.807) | 8 bytes |
int | De -2^31 (-2.147.483.648) a 2^31-1 (2.147.483.647) | 4 bytes |
smallint | De -2^15 (-32.768) a 2^15-1 (32.767) | 2 bytes |
tinyint | De 0 a 255 | 1 byte |
Tipo de datos | Intervalo | Almacenamiento |
float | De - 1,79E+308 a -2,23E-308, 0 y de 2,23E-308 a1,79E+308 | Depende del valor de n. |
real | De - 3,40E + 38 a -1,18E - 38, 0 y de 1,18E - 38 a 3,40E + 38 | 4 Bytes |
Precisión | Bytes de almacenamiento |
1 - 9 | 5 |
10-19 | 9 |
20-28 | 13 |
29-38 | 17 |
-------------------------------------------------------
02.--- Practico constrains
03.-------------------------------------------------------
04.
05.USE abeldb
06.GO
07. 08.-------------------------------------------------------
09.--- Creamos la tablas con las que vamos a hacer pruebas
10.-------------------------------------------------------
11.IF OBJECT_ID(N'prueba.facturas') is not null
12.drop table prueba.facturas
13.
14.CREATE TABLE prueba.facturas
15.(
16.factID char(13) not null
17.,cliID Int not null18.,factFecha DATE not null
19.,importe MONEY not null
20.)
21.
22.
23.IF OBJECT_ID(N'prueba.clientes') is not null
24.drop table prueba.clientes
25.
26.CREATE TABLE prueba.clientes
27.(
28.cliID INT not null
29.,cliFechaAlta DATE not null
30.)
31.
32.
33.GO
34.-------------------------------------------------------35.--- Creamos los constrains
36.-------------------------------------------------------
37.
38.--- 1) Contrainst del tipo PK
39.
40.ALTER TABLE prueba.clientes
41.ADD CONSTRAINT pk_clientes PRIMARY KEY CLUSTERED( cliID )
42.GO
43.
44.ALTER TABLE prueba.facturas
45.ADD CONSTRAINT pk_facturas PRIMARY KEY CLUSTERED( factID )
46.GO
47.
48.--- 2) Contrainst del tipo FK49.ALTER TABLE prueba.facturas
50.ADD CONSTRAINT fk_facturasClientes FOREIGN KEY ( cliID )
51.REFERENCES prueba.clientes ( cliID )
52.GO
53.
54.--- 3) Contrainst del tipo default
55.
56.ALTER TABLE prueba.clientes
57.ADD CONSTRAINT df_fechaAlta DEFAULT (GETDATE()) FOR cliFechaAlta
58.GO
59.
60.--- 4) Contrainst del tipo check
61.ALTER TABLE prueba.facturas
62.ADD CONSTRAINT ck_importe CHECK (importe > 0 )
63.GO
64.
65.ALTER TABLE prueba.facturas
66.ADD CONSTRAINT ck_nro_factura CHECK
67.( factID like '[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' )
68.GO
A. Usar restricciones PRIMARY KEY
En el siguiente ejemplo se muestra la definición de columna para una restricción PRIMARY KEY con un índice clúster en la columna BusinessEntityID de la tabla Employee (lo...
Regístrate para leer el documento completo.