Vous êtes sur la page 1sur 4

UNICA

FACULTAD DE INGENIERIA DE SISTEMAS


ESCUELA DE INGENIERIA DE SISTEMAS

Curso : DISEÑO E IMPLEMENTACIÓN DE BASE DE DATOS

Catedrático : Paco Márquez Urbina.

LABORATORIO Nº7
1.-------------------------------Insert-----------------------------------

Insert into Customers (CustomerID ,CompanyName ,ContactName


,ContactTitle ,[Address] ,City ,Region ,PostalCode ,Country ,Phone, Fax)
values
('ABCD5','Compañia, S.A.','Juan Perez','Ing','7av 3-3 Zona 5'
,'Guatemala','Guatemala','01005', 'Guatemala','(502)-5435-5454'
,'(502)-5435-6676'),
('ABCD6','OtraCompañia, S.A.','Ana Perez','Lic','7av 3-3 Zona 10'
,'Guatemala','Guatemala','01010', 'Guatemala','(502)-5435-5454'
,'(502)-5435-6676')

2.-------------------Insertar datos a partir de un select------------------------


Insert into customers
Select Concat(substring(Replace(companyName,' ',''),1,4),'9') as codigo
, CompanyName ,ContactName ,ContactTitle ,[Address] ,City
,Region ,PostalCode ,Country ,Phone, Fax
from suppliers

3.---Creando un procedimiento almacenado para posteriormente insertar datos------


CREATE Procedure DatosProveedor
as
Select Concat(substring(Replace(companyName,' ',''),1,4),'9') as codigo
, CompanyName ,ContactName ,ContactTitle ,[Address] ,City
,Region ,PostalCode ,Country ,Phone, Fax
from suppliers

4.------Insertar datos a partir del procedimiento anterior creado--------------


Insert into Customers
Execute DatosProveedor

5.-----------------Crear a partir de los datos de un select otra tabla-----------


select c.customerid, c.companyname, o.orderid, o.orderdate
INTO ORDENESNUEVAS
from customers as c inner join orders as o
on c.CustomerID=o.customerid

Select * from ORDENESNUEVAS


6.- -----------------------------Borrar datos------------------------------------
select * from ORDENESNUEVAS

--------------------BORRA SIN PASAR POR EL LOG DE TRANSACCIONES (CRIMEN PERFECTO)


TRUNCATE TABLE ORDENESNUEVAS

7.-------------------Borrar el objeto-----------------------------------
DROP TABLE ORDENESNUEVAS

8.----------------------Crear una tabla basica para usar default----------


Create table Test
(codigo int identity(1,1) not null primary key,
nombre varchar(100),
estadocivil varchar(100) default ('Soltero')
)

Insert into Test( nombre, estadocivil)


values ('Juan Piao', DEFAULT)

9.------------------BORRADO NORMAL CON DELETE------------------------------------


DELETE FROM CUSTOMERS WHERE CUSTOMERID IN
(Select concat(substring(Replace(companyName,' ',''),1,4),'9') as codigo
from suppliers)

-------------

SELECT C.CUSTOMERID, C.COMPANYNAME, O.ORDERID, O.ORDERDATE


FROM CUSTOMERS AS C INNER JOIN ORDERS AS O ON C.CustomerID=O.CustomerID
WHERE O.OrderID IS NULL

10.-------------ELIMINAR DATOS DE UNA TABLA CON RESPECTO A OTRA TABLA


--ELIMINAR LOS CLIENTES QUE NO HAN REALIZADO UNA ORDEN
Delete from C
from customers as c left join orders as o
on c.CustomerID=o.customerid where
o.orderid is null

11.--------------------UPDATE
UPDATE CUSTOMERS
SET CompanyName=Companyname+',USA'
where country='USA'

12.--------------------ACTUALIZAR
--TODOS LOS PRECIOS DE LOS PRODUCTOS DE ESTADOS UNIDOS
SELECT S.COMPANYNAME
, S.COUNTRY, P.PRODUCTNAME, P.UnitPrice
FROM
SUPPLIERS AS S INNER JOIN
PRODUCTS AS P ON P.SupplierID=S.SupplierID
WHERE S.COUNTRY='USA'

13.--------------------SUBE EL 5% A TODOS LOS PRODUCTOS DE USA


UPDATE P SET P.UnitPrice=P.UnitPrice * 1.05
FROM
SUPPLIERS AS S INNER JOIN
PRODUCTS AS P ON P.SupplierID=S.SupplierID
WHERE S.COUNTRY='USA'
14.----------------------------Merge-----------------------------------------
Select *
into ClientesA
from Customers

Select *
into ClientesB
from Customers

Delete clientesA where companyname like '[a-d]%'

Update clientesA Set companyname='Nombre-Eliminado' where country='USA' or


Country='France'

select * from ClientesA where country='USA' or Country='France'

Merge Into ClientesA As A Using clientesb B


on A.Customerid=B.Customerid
When matched then
UPDATE SET A.Companyname = B.Companyname, A.ContactName=B.ContactName,
A.ContactTitle= B.ContactTitle, A.Country=B.Country
When not matched then
INSERT (CustomerId, CompanyName, Contactname, ContactTitle, Country)
VALUES (B.CustomerId, B.CompanyName, B.ContactName, B.ContactTitle, B.Country)
WHEN NOT matched BY SOURCE THEN
DELETE;

select * from ClientesA where country='USA' or Country='France'

15. ---------------------------Identity--------------------------
DBCC CHECKIDENT (suppliers, RESEED, 200);
GO
Set identity_insert suppliers on

Select max(supplierid) from suppliers

insert into suppliers (companyname, contactname, country)


values ('Empresa prueba','Juanito Bazooka','Guatemala')

16.---------------------------Uso de Sequence--------------------
Create sequence numerador
as int
start with 5
increment by 5
go

Create table TablaEjemplo2


(codigo int primary key default (next value for numerador) ,
nombre varchar(100)
)
Insert into TablaEjemplo2 (codigo, nombre) values
(Default, 'Hugo'),
(Default, 'Paco'),
(Default , 'Luis')
select * from TablaEjemplo2

drop table TablaEjemplo2


drop table test

Vous aimerez peut-être aussi