use database01 Database changed Database changed">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

MySQL Servidor de Bases de Datos Software Libre Ambientes Windows & Unix.

Similar presentations


Presentation on theme: "MySQL Servidor de Bases de Datos Software Libre Ambientes Windows & Unix."— Presentation transcript:

1 MySQL Servidor de Bases de Datos Software Libre Ambientes Windows & Unix

2 Activación de "MySQL” C:\mysql\bin> mysql Listar las bases de datos mysql > show databases; mysql > show databases; | Tables in database01 | | table01 | | table02 | rows in set (0.00 sec) 2 rows in set (0.00 sec)

3 Crear una BD simple y desplegar su estructura Crear una nueva BD llamada database01 mysql> create database database01; Database "database01" created. Todo lo que hace esta instrucción es crear un nuevo subdirectorio en c:\mysql\data. Todo lo que hace esta instrucción es crear un nuevo subdirectorio en c:\mysql\data. Abrir la BD mysql> use database01 Database changed Database changed

4 Crear una tabla mysql> create table table01 ( field01 integer, field02 char(10)); Query OK, 0 rows affected (0.00 sec) Encerrar toda la lista de atributos entre paréntesis. Encerrar toda la lista de atributos entre paréntesis. Comas son usadas como separadores de atributos. Comas son usadas como separadores de atributos. Todas las instrucciones de SQL son terminadas por ; Todas las instrucciones de SQL son terminadas por ;

5 Listar las tablas mysql> show tables; | Tables in database01 | | table01 | | table02 |

6 Listar los campos en una tabla mysql> show columns from table01; mysql > desc table01; | Field | Type | Null | Key | Default | Extra | | field01 | int(11) | YES | | | | | field02 | char(10) | YES | | | |

7 Poner datos en una Tabla Insertar un registro mysql> insert into table01 (field01,field02) values (1,'first'); Query OK, 1 row affected (0.00 sec) Encerrar la lista de atributos entre paréntesis. Encerrar los valores a ser insertados entre paréntesis. Comas son usadas entre cada campo y cada variable. Un espacio es usado después de la coma entre campos.

8 Listar todos los registros en una tabla mysql> select * from table01; | field01 | field02 | | 1 | first |

9 Añadiendo Campos (columnas)... un campo a la vez mysql> alter table table01 add column field03 char(20); Query OK, 1 row affected (0.04 sec) Records: 1 Duplicates: 0 Warnings: 0...más de un campo a la vez mysql> alter table table01 add column field04 date, add column field05 time; Query OK, 1 row affected (0.04 sec) Records: 1 Duplicates: 0 Warnings: 0 "add column" tiene que ser aplicada por cada columna. Comas son usadas entre cada instrucción add column. Un espacio debe ser usado después de las comas.

10 Esquema COMPANY PROJECT pnamepnumberplocationdnum DEPT_LOCATION dnumberdlocation EMPLOYEE fnameminitlnamessnbdateaddresssexsalarysuperssndno DEPARTMENT dnamednumbermgrssnmgrstartdate DEPENDENT essndependent_namesexbdaterelationship WORKS_ON essnpnohours FK

11 CREATE TABLE EMPLOYEE (FNAMEVARCHAR(10), mint char(1), lname varchar(10), SSNCHAR(9) NOT NULL, SSNCHAR(9) NOT NULL, BDATEDATE, address varchar(30), sex char(1), salary float, BDATEDATE, address varchar(30), sex char(1), salary float, SUPERSSNCHAR(9), SUPERSSNCHAR(9), DNOINTEGER DEFAULT 1, DNOINTEGER DEFAULT 1, PRIMARY KEY(SSN), PRIMARY KEY(SSN), FOREIGN KEY(DNO) REFERENCES DEPARTMENT FOREIGN KEY(DNO) REFERENCES DEPARTMENT ON DELETE SET DEFAULT ON UPDATE CASCADE, ON DELETE SET DEFAULT ON UPDATE CASCADE, FOREIGN KEY(SUPERSSN) REFERENCES EMPLOYEE FOREIGN KEY(SUPERSSN) REFERENCES EMPLOYEE ON DELETE SET NULL ON UPDATE CASCADE); ON DELETE SET NULL ON UPDATE CASCADE); CREATE TABLE DEPARTMENT CREATE TABLE DEPARTMENT (DNAMEVARCHAR(10)NOT NULL, DNUMBERINTEGER NOT NULL, DNUMBERINTEGER NOT NULL, MGRSSNCHAR(9), MGRSSNCHAR(9), MGRSTARTDATECHAR(10), MGRSTARTDATECHAR(10), PRIMARY KEY(DNUMBER), PRIMARY KEY(DNUMBER), UNIQUE(DNAME), UNIQUE(DNAME), FOREIGN KEY(MGRSSN) REFERENCES EMPLOYEE FOREIGN KEY(MGRSSN) REFERENCES EMPLOYEE ON DELETE SET DEFAULT ON UPDATE CASCADE); ON DELETE SET DEFAULT ON UPDATE CASCADE);

12 CREATE TABLE project ( pname char(15) not null, pname char(15) not null, pnumber char(5) not null, pnumber char(5) not null, plocation char(10), plocation char(10), dnum integer default 1, dnum integer default 1, primary key(pnumber), primary key(pnumber), unique(pname), unique(pname), foreign key(dnum) references department foreign key(dnum) references department on delete set default on update cascade); on delete set default on update cascade); CREATE TABLE dept_locations ( CREATE TABLE dept_locations ( dnumber integer default 1 not null, dnumber integer default 1 not null, dlocation char(10) not null, dlocation char(10) not null, primary key(dnumber, dlocation), primary key(dnumber, dlocation), foreign key(dnumber) references department foreign key(dnumber) references department on delete set default on update cascade); on delete set default on update cascade);

13 CREATE TABLE works_on( essn char(9) not null, essn char(9) not null, pno char(5) not null, pno char(5) not null, hours float, hours float, primary key(essn, pno), primary key(essn, pno), foreign key(essn) references employee foreign key(essn) references employee on delete cascade on update cascade, on delete cascade on update cascade, foreign key(pno) references project foreign key(pno) references project on delete cascade on update cascade); on delete cascade on update cascade); CREATE TABLE dependent( essn char(9) not null, essn char(9) not null, dependent_name varchar(10) not null, dependent_name varchar(10) not null, sex char(1), sex char(1), bdate date, bdate date, relationship varchar(10), relationship varchar(10), primary key(essn, dependent_name), primary key(essn, dependent_name), foreign key(essn) references employee foreign key(essn) references employee on delete cascade on update cascade ); on delete cascade on update cascade );

14 INSERT INTO project VALUES ('ProductX', '1', 'Bellaire', 5) ; INSERT INTO project VALUES ('ProductY', '2', 'Sugarland', 5); INSERT INTO project VALUES ('ProductY', '2', 'Sugarland', 5); INSERT INTO project VALUES ('ProductZ', '3', 'Houston', 5); INSERT INTO project VALUES ('ProductZ', '3', 'Houston', 5); INSERT INTO project VALUES ('Computerization', '10', 'Stafford', 4); INSERT INTO project VALUES ('Computerization', '10', 'Stafford', 4); INSERT INTO project VALUES ('Reorganization', '20', 'Houston', 1); INSERT INTO project VALUES ('Reorganization', '20', 'Houston', 1); INSERT INTO project VALUES ('Newbenefits', '30', 'Stafford', 4); INSERT INTO project VALUES ('Newbenefits', '30', 'Stafford', 4); INSERT INTO dept_locations VALUES (1, 'Houston'); INSERT INTO dept_locations VALUES (1, 'Houston'); INSERT INTO dept_locations VALUES (4, 'Stafford'); INSERT INTO dept_locations VALUES (4, 'Stafford'); INSERT INTO dept_locations VALUES (5, 'Bellaire') ; INSERT INTO dept_locations VALUES (5, 'Bellaire') ; INSERT INTO dept_locations VALUES (5, 'Sugarland'); INSERT INTO dept_locations VALUES (5, 'Sugarland'); INSERT INTO dept_locations VALUES (5, 'Houston') ; INSERT INTO dept_locations VALUES (5, 'Houston') ;

15 INSERT INTO employee VALUES ('John', 'B', 'Smith', ' ', '09-Jan- 1955', 'Houston,TX', 'M', 30000, ' ', 5); INSERT INTO employee VALUES ('Frank', 'T', 'Wong', ' ', '08-DEC- 1945', 'Houston,TX', 'M', 40000, ' ', 5) ; INSERT INTO employee VALUES ('Frank', 'T', 'Wong', ' ', '08-DEC- 1945', 'Houston,TX', 'M', 40000, ' ', 5) ; INSERT INTO employee VALUES ('Alicia', 'J', 'Zelaya', ' ', '19-JUL- 1958', 'Spring,TX', 'F', 25000, ' ', 4) ; INSERT INTO employee VALUES ('Alicia', 'J', 'Zelaya', ' ', '19-JUL- 1958', 'Spring,TX', 'F', 25000, ' ', 4) ; INSERT INTO employee VALUES ('Jennifer', 'S', 'Wallace', ' ', '20- JUN-1931', 'Bellaire,TX', 'F', 43000, ' ', 4) ; INSERT INTO employee VALUES ('Jennifer', 'S', 'Wallace', ' ', '20- JUN-1931', 'Bellaire,TX', 'F', 43000, ' ', 4) ; INSERT INTO employee VALUES ('Ramesh', 'K', 'Narayan', ' ', '15- SEP-1952', 'Humble,TX', 'M', 38000, ' ', 5); INSERT INTO employee VALUES ('Joyce', 'A', 'English', ' ', '31-JUL- 1962', 'Houston, TX', 'F', 25000, ' ', 5); INSERT INTO employee VALUES ('Joyce', 'A', 'English', ' ', '31-JUL- 1962', 'Houston, TX', 'F', 25000, ' ', 5); INSERT INTO employee VALUES ('Ahmad', 'V', 'Jabbar', ' ', '29- MAR-1959', 'Houston,TX', 'M', 25000, ' ', 4) ; INSERT INTO employee VALUES ('Ahmad', 'V', 'Jabbar', ' ', '29- MAR-1959', 'Houston,TX', 'M', 25000, ' ', 4) ; INSERT INTO employee VALUES ('James', 'E', 'Borg', ' ', '10-NOV- 1927', 'Houston,TX', 'M', 55000, null, 1); INSERT INTO employee VALUES ('James', 'E', 'Borg', ' ', '10-NOV- 1927', 'Houston,TX', 'M', 55000, null, 1); INSERT INTO department VALUES ('Research', 5, ' ', '22-MAY- 1978'); INSERT INTO department VALUES ('Research', 5, ' ', '22-MAY- 1978'); INSERT INTO department VALUES ('Administration', 4, ' ', '01-JAN- 1985'); INSERT INTO department VALUES ('Administration', 4, ' ', '01-JAN- 1985'); INSERT INTO department VALUES ('Headquarters', 1, ' ', '19-JUN- 1971') ; INSERT INTO department VALUES ('Headquarters', 1, ' ', '19-JUN- 1971') ;

16 INSERT INTO dependent VALUES (' ','Alice','F','05-APR-76','Daughter') ; INSERT INTO dependent VALUES (' ','Theodore','M','25-OCT-73','Son') ; INSERT INTO dependent VALUES (' ','Theodore','M','25-OCT-73','Son') ; INSERT INTO dependent VALUES (' ','Joy','F','03-MAY-48','Spouse') ; INSERT INTO dependent VALUES (' ','Joy','F','03-MAY-48','Spouse') ; INSERT INTO dependent VALUES (' ','Abner','M','29-FEB-32','Spouse') ; INSERT INTO dependent VALUES (' ','Abner','M','29-FEB-32','Spouse') ; INSERT INTO dependent VALUES (' ','Michael','M','01-JAN-78','Son') ; INSERT INTO dependent VALUES (' ','Michael','M','01-JAN-78','Son') ; INSERT INTO dependent VALUES (' ','Alice','F', '31-DEC-78','Daughter'); INSERT INTO dependent VALUES (' ','Elizabeth','F','05-MAY- 57','Spouse') ; INSERT INTO dependent VALUES (' ','Elizabeth','F','05-MAY- 57','Spouse') ; INSERT INTO works_on VALUES (' ', '1', 32.5) ; INSERT INTO works_on VALUES (' ', '1', 32.5) ; INSERT INTO works_on VALUES (' ', '2', 7.5) ; INSERT INTO works_on VALUES (' ', '2', 7.5) ; INSERT INTO works_on VALUES (' ', '3', 40.0) ; INSERT INTO works_on VALUES (' ', '3', 40.0) ; INSERT INTO works_on VALUES (' ', '1', 20.0) ; INSERT INTO works_on VALUES (' ', '1', 20.0) ; INSERT INTO works_on VALUES (' ', '2', 20.0) ; INSERT INTO works_on VALUES (' ', '2', 20.0) ; INSERT INTO works_on VALUES (' ', '2', 10.0) ; INSERT INTO works_on VALUES (' ', '2', 10.0) ; INSERT INTO works_on VALUES (' ', '3', 10.0) ; INSERT INTO works_on VALUES (' ', '3', 10.0) ; INSERT INTO works_on VALUES (' ', '10', 10.0) ; INSERT INTO works_on VALUES (' ', '10', 10.0) ; INSERT INTO works_on VALUES (' ', '30', 30.0) ; INSERT INTO works_on VALUES (' ', '10', 10.0) ; INSERT INTO works_on VALUES (' ', '10', 10.0) ; INSERT INTO works_on VALUES (' ', '10', 35.0) ; INSERT INTO works_on VALUES (' ', '10', 35.0) ; INSERT INTO works_on VALUES (' ', '30', 5.0) ; INSERT INTO works_on VALUES (' ', '30', 5.0) ; INSERT INTO works_on VALUES (' ', '30', 20.0) ; INSERT INTO works_on VALUES (' ', '20', 15.0) ; INSERT INTO works_on VALUES (' ', '20', 15.0) ; INSERT INTO works_on VALUES (' ', '20', null) ; INSERT INTO works_on VALUES (' ', '20', null) ;


Download ppt "MySQL Servidor de Bases de Datos Software Libre Ambientes Windows & Unix."

Similar presentations


Ads by Google