+ All Categories
Home > Documents > MySQL Servidor de Bases de Datos Software Libre Ambientes Windows & Unix.

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

Date post: 16-Dec-2015
Category:
Upload: gabriel-curtis
View: 216 times
Download: 0 times
Share this document with a friend
16
MySQL MySQL Servidor de Bases de Servidor de Bases de Datos Datos Software Libre Software Libre Ambientes Windows & Unix Ambientes Windows & Unix
Transcript

MySQLMySQL

Servidor de Bases de DatosServidor de Bases de Datos

Software LibreSoftware Libre

Ambientes Windows & UnixAmbientes Windows & Unix

Activación de "MySQL”Activación de "MySQL”

C:\mysql\bin> C:\mysql\bin> mysql mysql

Listar las bases de datosListar las bases de datos mysql > show databases; mysql > show databases; +---------------------------+

| Tables in database01 | +------------------------------+ | table01 |

| table02 | +-----------------------+

2 rows in set (0.00 sec)2 rows in set (0.00 sec)

Crear una BD simple y desplegar Crear una BD simple y desplegar su estructurasu estructura

Crear una nueva BD llamada database01Crear una nueva BD llamada database01mysql> mysql> create database database01;create database database01; Database "database01" created.Database "database01" created. Todo lo que hace esta instrucción es crear un nuevo Todo lo que hace esta instrucción es crear un nuevo

subdirectorio en c:\mysql\data .subdirectorio en c:\mysql\data .

Abrir la BDAbrir la BDmysql> mysql> use database01use database01 Database changedDatabase changed

Crear una tablaCrear una tabla

mysql> mysql> create table table01 (create table table01 (field01 integer, field02 field01 integer, field02 char(10));char(10));

Query OK, 0 rows affected (0.00 sec) 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 ;

Listar las tablasListar las tablas

mysql> show tables;

+-------------------------+ | Tables in database01 | +-------------------------+ | table01 | | table02 | +-----------------------------------+

Listar los campos en una tablaListar 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 | | | | +---------+----------+-------+------+--------+--------+

Poner datos en una TablaPoner datos en una Tabla

Insertar un registromysql> 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.

Listar todos los registros en una Listar todos los registros en una tablatabla

mysql> select * from table01;

+---------+---------+ | field01 | field02 |+---------+---------+ | 1 | first | +---------+---------+

Añadiendo CamposAñadiendo Campos(columnas)(columnas)

. . . un campo a la vezmysql> 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.

EsquemaEsquemaCOMPANYCOMPANY

PROJECT

pname pnumber plocation dnum

DEPT_LOCATION

dnumber dlocation

EMPLOYEE

fname minit lname ssn bdate address sex salary superssn dno

DEPARTMENT

dname dnumber mgrssn mgrstartdate

DEPENDENT

essn dependent_name sex bdate relationship

WORKS_ONessn pno hours

FK

FK

FK

FK

FK

FK FK

FK

CREATE TABLE EMPLOYEECREATE TABLE EMPLOYEE(FNAME(FNAME VARCHAR(10), mint char(1), lname varchar(10),VARCHAR(10), mint char(1), lname varchar(10), SSNSSN CHAR(9) NOT NULL,CHAR(9) NOT NULL,

BDATEBDATE DATE, address varchar(30), sex char(1), salary float,DATE, address varchar(30), sex char(1), salary float, SUPERSSNSUPERSSN CHAR(9),CHAR(9),

DNODNO INTEGER DEFAULT 1,INTEGER DEFAULT 1, PRIMARY KEY(SSN),PRIMARY KEY(SSN), FOREIGN KEY(DNO) REFERENCES DEPARTMENTFOREIGN KEY(DNO) REFERENCES DEPARTMENT ON DELETE SET DEFAULT ON UPDATE CASCADE,ON DELETE SET DEFAULT ON UPDATE CASCADE, FOREIGN KEY(SUPERSSN) REFERENCES EMPLOYEEFOREIGN KEY(SUPERSSN) REFERENCES EMPLOYEE ON DELETE SET NULL ON UPDATE CASCADE);ON DELETE SET NULL ON UPDATE CASCADE);

CREATE TABLE DEPARTMENTCREATE TABLE DEPARTMENT(DNAME(DNAME VARCHAR(10)VARCHAR(10) NOT NULL,NOT NULL, DNUMBERDNUMBER INTEGER INTEGER NOT NULL,NOT NULL, MGRSSNMGRSSN CHAR(9),CHAR(9), MGRSTARTDATEMGRSTARTDATE CHAR(10) ,CHAR(10) ,

PRIMARY KEY(DNUMBER),PRIMARY KEY(DNUMBER), UNIQUE(DNAME),UNIQUE(DNAME), FOREIGN KEY(MGRSSN) REFERENCES EMPLOYEEFOREIGN KEY(MGRSSN) REFERENCES EMPLOYEE ON DELETE SET DEFAULT ON UPDATE CASCADE);ON DELETE SET DEFAULT ON UPDATE CASCADE);

CREATE TABLE project (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 departmentforeign key(dnum) references department

on delete set default on update cascadeon 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 departmentforeign key(dnumber) references department on delete set default on update cascadeon delete set default on update cascade); );

CREATE TABLE works_on(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 employeeforeign key(essn) references employee

on delete cascade on update cascade,on delete cascade on update cascade, foreign key(pno) references projectforeign key(pno) references project

on delete cascade on update cascadeon delete cascade on update cascade););

CREATE TABLE dependent(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 employeeforeign key(essn) references employee

on delete cascade on update cascadeon delete cascade on update cascade ););

INSERT INTO project VALUES ('ProductX', '1', 'Bellaire', 5) ;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', INSERT INTO project VALUES ('Computerization', '10', 'Stafford', 4); 'Stafford', 4); INSERT INTO project VALUES ('Reorganization', '20', INSERT INTO project VALUES ('Reorganization', '20', 'Houston', 1); 'Houston', 1); INSERT INTO project VALUES ('Newbenefits', '30', 'Stafford', INSERT INTO project VALUES ('Newbenefits', '30', 'Stafford', 4); 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') ;

INSERT INTO employee VALUES ('John', 'B', 'Smith', '123456789', '09-Jan-INSERT INTO employee VALUES ('John', 'B', 'Smith', '123456789', '09-Jan-1955', 'Houston,TX', 'M', 30000,1955', 'Houston,TX', 'M', 30000, '333445555', 5);'333445555', 5); INSERT INTO employee VALUES ('Frank', 'T', 'Wong', '333445555', '08-DEC-INSERT INTO employee VALUES ('Frank', 'T', 'Wong', '333445555', '08-DEC-1945', 'Houston,TX', 'M', 40000, '888665555', 5) ;1945', 'Houston,TX', 'M', 40000, '888665555', 5) ; INSERT INTO employee VALUES ('Alicia', 'J', 'Zelaya', '999887777', '19-JUL-INSERT INTO employee VALUES ('Alicia', 'J', 'Zelaya', '999887777', '19-JUL-1958', 'Spring,TX', 'F', 25000, '987654321', 4) ;1958', 'Spring,TX', 'F', 25000, '987654321', 4) ; INSERT INTO employee VALUES ('Jennifer', 'S', 'Wallace', '987654321', '20-INSERT INTO employee VALUES ('Jennifer', 'S', 'Wallace', '987654321', '20-JUN-1931', 'Bellaire,TX', 'F', 43000, '888665555', 4) ; JUN-1931', 'Bellaire,TX', 'F', 43000, '888665555', 4) ; INSERT INTO employee VALUES ('Ramesh', 'K', 'Narayan', '666884444', '15-INSERT INTO employee VALUES ('Ramesh', 'K', 'Narayan', '666884444', '15-SEP-1952', 'Humble,TX', 'M', 38000, '333445555', 5); SEP-1952', 'Humble,TX', 'M', 38000, '333445555', 5); INSERT INTO employee VALUES ('Joyce', 'A', 'English', '453453453', '31-JUL-INSERT INTO employee VALUES ('Joyce', 'A', 'English', '453453453', '31-JUL-1962', 'Houston, TX', 'F', 25000, '333445555', 5); 1962', 'Houston, TX', 'F', 25000, '333445555', 5); INSERT INTO employee VALUES ('Ahmad', 'V', 'Jabbar', '987987987', '29-INSERT INTO employee VALUES ('Ahmad', 'V', 'Jabbar', '987987987', '29-MAR-1959', 'Houston,TX', 'M', 25000, '987654321', 4) ;MAR-1959', 'Houston,TX', 'M', 25000, '987654321', 4) ; INSERT INTO employee VALUES ('James', 'E', 'Borg', '888665555', '10-NOV-INSERT INTO employee VALUES ('James', 'E', 'Borg', '888665555', '10-NOV-1927', 'Houston,TX', 'M', 55000, null, 1);1927', 'Houston,TX', 'M', 55000, null, 1);

INSERT INTO department VALUES ('Research', 5, '333445555', '22-MAY-INSERT INTO department VALUES ('Research', 5, '333445555', '22-MAY-1978'); 1978'); INSERT INTO department VALUES ('Administration', 4, '987654321', '01-JAN-INSERT INTO department VALUES ('Administration', 4, '987654321', '01-JAN-1985'); 1985'); INSERT INTO department VALUES ('Headquarters', 1, '888665555', '19-JUN-INSERT INTO department VALUES ('Headquarters', 1, '888665555', '19-JUN-1971') ;1971') ;

INSERT INTO dependent VALUES ('333445555','Alice','F','05-APR-76','Daughter') ;INSERT INTO dependent VALUES ('333445555','Alice','F','05-APR-76','Daughter') ; INSERT INTO dependent VALUES ('333445555','Theodore','M','25-OCT-73','Son') ;INSERT INTO dependent VALUES ('333445555','Theodore','M','25-OCT-73','Son') ; INSERT INTO dependent VALUES ('333445555','Joy','F','03-MAY-48','Spouse') ;INSERT INTO dependent VALUES ('333445555','Joy','F','03-MAY-48','Spouse') ; INSERT INTO dependent VALUES ('987654321','Abner','M','29-FEB-32','Spouse') ;INSERT INTO dependent VALUES ('987654321','Abner','M','29-FEB-32','Spouse') ; INSERT INTO dependent VALUES ('123456789','Michael','M','01-JAN-78','Son') ;INSERT INTO dependent VALUES ('123456789','Michael','M','01-JAN-78','Son') ;INSERT INTO dependent VALUES ('123456789','Alice','F', '31-DEC-78','Daughter');INSERT INTO dependent VALUES ('123456789','Alice','F', '31-DEC-78','Daughter'); INSERT INTO dependent VALUES ('123456789','Elizabeth','F','05-MAY-57','Spouse') INSERT INTO dependent VALUES ('123456789','Elizabeth','F','05-MAY-57','Spouse') ;;

INSERT INTO works_on VALUES ('123456789', '1', 32.5) ;INSERT INTO works_on VALUES ('123456789', '1', 32.5) ; INSERT INTO works_on VALUES ('123456789', '2', 7.5) ;INSERT INTO works_on VALUES ('123456789', '2', 7.5) ; INSERT INTO works_on VALUES ('666884444', '3', 40.0) ;INSERT INTO works_on VALUES ('666884444', '3', 40.0) ; INSERT INTO works_on VALUES ('453453453', '1', 20.0) ;INSERT INTO works_on VALUES ('453453453', '1', 20.0) ; INSERT INTO works_on VALUES ('453453453', '2', 20.0) ;INSERT INTO works_on VALUES ('453453453', '2', 20.0) ; INSERT INTO works_on VALUES ('333445555', '2', 10.0) ;INSERT INTO works_on VALUES ('333445555', '2', 10.0) ; INSERT INTO works_on VALUES ('333445555', '3', 10.0) ;INSERT INTO works_on VALUES ('333445555', '3', 10.0) ; INSERT INTO works_on VALUES ('333445555', '10', 10.0) ;INSERT INTO works_on VALUES ('333445555', '10', 10.0) ;INSERT INTO works_on VALUES ('999887777', '30', 30.0) ;INSERT INTO works_on VALUES ('999887777', '30', 30.0) ; INSERT INTO works_on VALUES ('999887777', '10', 10.0) ;INSERT INTO works_on VALUES ('999887777', '10', 10.0) ; INSERT INTO works_on VALUES ('987987987', '10', 35.0) ;INSERT INTO works_on VALUES ('987987987', '10', 35.0) ; INSERT INTO works_on VALUES ('987987987', '30', 5.0) ;INSERT INTO works_on VALUES ('987987987', '30', 5.0) ;INSERT INTO works_on VALUES ('987654321', '30', 20.0) ;INSERT INTO works_on VALUES ('987654321', '30', 20.0) ; INSERT INTO works_on VALUES ('987654321', '20', 15.0) ;INSERT INTO works_on VALUES ('987654321', '20', 15.0) ; INSERT INTO works_on VALUES ('888665555', '20', null) ;INSERT INTO works_on VALUES ('888665555', '20', null) ;


Recommended