Professional Documents
Culture Documents
| sex
| char(1)
| YES |
| NULL
|
|
| salary
| decimal(10,2) | YES |
| NULL
|
|
| super_ssn | varchar(9)
| YES |
| NULL
|
|
| dno
| int(11)
| YES |
| NULL
|
|
+-----------+---------------+------+-----+---------+-------+
10 rows in set (0.02 sec)
mysql> create table department(dname VARCHAR(15) NOT NULL,dnumber INT,mgr_ssn VA
RCHAR(9) NOT NULL,mgr_startdate DATE,primary key (dnumber));
Query OK, 0 rows affected (2.18 sec)
mysql> describe department;
+---------------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| dname
| varchar(15) | NO |
| NULL
|
|
| dnumber
| int(11)
| NO | PRI | 0
|
|
| mgr_ssn
| varchar(9) | NO |
| NULL
|
|
| mgr_startdate | date
| YES |
| NULL
|
|
+---------------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> create table dept_locations(dnumber INT NOT NULL,dlocation VARCHAR(20) NO
T NULL,primary key (dnumber,dlocation));
Query OK, 0 rows affected (0.38 sec)
mysql> describe dept_location;
ERROR 1146 (42S02): Table 'dewi_pl.dept_location' doesn't exist
mysql> describe dept_locations;
+-----------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dnumber | int(11)
| NO | PRI | NULL
|
|
| dlocation | varchar(20) | NO | PRI | NULL
|
|
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
mysql> create table project(pname VARCHAR(16) NOT NULL,pnumber INT NOT NULL,ploc
ation VARCHAR(20),dnum INT,primary key (pnumber));
Query OK, 0 rows affected (0.06 sec)
mysql> describe project;
+-----------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| pname
| varchar(16) | NO |
| NULL
|
|
| pnumber | int(11)
| NO | PRI | NULL
|
|
| plocation | varchar(20) | YES |
| NULL
|
|
| dnum
| int(11)
| YES |
| NULL
|
|
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)
mysql> create table work_on(essn VARCHAR(9) NOT NULL,pno INT NOT NULL,hours TIME
,primary key (essn,pno));
Query OK, 0 rows affected (0.00 sec)
mysql> describe work_on;
+-------+------------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
mysql> alter table employee MODIFY dno INT,ADD FOREIGN KEY(dno) REFERENCES depar
tment(dnumber);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe employee;
+-----------+---------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| fname
| varchar(15) | NO |
| NULL
|
|
| minit
| char(1)
| YES |
| NULL
|
|
| lname
| varchar(15) | NO |
| NULL
|
|
| ssn
| varchar(9)
| NO | PRI | NULL
|
|
| bdate
| date
| YES |
| NULL
|
|
| address | varchar(30) | YES |
| NULL
|
|
| sex
| char(1)
| YES |
| NULL
|
|
| salary
| decimal(10,2) | YES |
| NULL
|
|
| super_ssn | varchar(9)
| YES | MUL | NULL
|
|
| dno
| int(11)
| YES | MUL | NULL
|
|
+-----------+---------------+------+-----+---------+-------+
10 rows in set (0.01 sec)
mysql> alter table dept_location MODIFY dnumber INT NOT NULL,ADD FOREIGN KEY(dnu
mber) REFERENCES department(dnumber);
ERROR 1146 (42S02): Table 'dewi_pl.dept_location' doesn't exist
mysql> alter table dept_locations MODIFY dnumber INT NOT NULL,ADD FOREIGN KEY(dn
umber) REFERENCES department(dnumber);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe dept_locations;
+-----------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dnumber | int(11)
| NO | PRI | NULL
|
|
| dlocation | varchar(20) | NO | PRI | NULL
|
|
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> alter table project MODIFY dnum INT,add foreign key(dnum) REFERENCES depa
rtment(dnumber);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe project;
+-----------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| pname
| varchar(16) | NO |
| NULL
|
|
| pnumber | int(11)
| NO | PRI | NULL
|
|
| plocation | varchar(20) | YES |
| NULL
|
|
| dnum
| int(11)
| YES | MUL | NULL
|
|
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> insert into employee(fname,minit,lname,ssn,bdate,address,sex,salary,super
_ssn,dno) VALUES('John','B','Smith','123456789','1965-01-09','731 Fondren, Houst
on, TX','M',30000,'333445555',5);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f
ails (`dewi_pl`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`super_ssn
| 987987987 | 30 | 00:00:05 |
| 999887777 | 10 | 00:00:10 |
| 999887777 | 30 | 00:00:30 |
+-----------+-----+----------+
16 rows in set (0.00 sec)
mysql> insert into work_on(essn,pno,hours) values('888665555',20,);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near ')' at
line 1
mysql> insert into dependent(essn,dependent_name,sex,bdate,relationship) values(
'333445555','Alice','F','1986-04-05','Daughter');
Query OK, 1 row affected (0.08 sec)
mysql> insert into dependent(essn,dependent_name,sex,bdate,relationship) values(
'333445555','Theodore','M','1983-10-25','Son');
Query OK, 1 row affected (2.12 sec)
mysql> insert into dependent(essn,dependent_name,sex,bdate,relationship) values(
'333445555','Joy','F','1958-05-03','Spouse');
Query OK, 1 row affected (0.00 sec)
mysql> insert into dependent(essn,dependent_name,sex,bdate,relationship) values(
'987654321','Abner','M','1942-02-28','Spouse');
Query OK, 1 row affected (0.00 sec)
mysql> insert into dependent(essn,dependent_name,sex,bdate,relationship) values(
'123456789','Michael','M','1988-01-04','Son');
Query OK, 1 row affected (0.00 sec)
mysql> insert into dependent(essn,dependent_name,sex,bdate,relationship) values(
'123456789','Alice','F','1988-12-30','Daughter');
Query OK, 1 row affected (0.00 sec)
mysql> insert into dependent(essn,dependent_name,sex,bdate,relationship) values(
'123456789','Elizabeth','F','1967-05-05','Spouse');
Query OK, 1 row affected (0.00 sec)
mysql> select*from dependent;
+-----------+----------------+------+------------+--------------+
| essn
| dependent_name | sex | bdate
| relationship |
+-----------+----------------+------+------------+--------------+
| 123456789 | Alice
| F
| 1988-12-30 | Daughter
|
| 123456789 | Elizabeth
| F
| 1967-05-05 | Spouse
|
| 123456789 | Michael
| M
| 1988-01-04 | Son
|
| 333445555 | Alice
| F
| 1986-04-05 | Daughter
|
| 333445555 | Joy
| F
| 1958-05-03 | Spouse
|
| 333445555 | Theodore
| M
| 1983-10-25 | Son
|
| 987654321 | Abner
| M
| 1942-02-28 | Spouse
|
+-----------+----------------+------+------------+--------------+
7 rows in set (0.00 sec)