标签:
1. 根据上述表信息创建员工表employee
创建employee表的SQL语句如下:
CREATE TABLE employee( id INT(4), NAME VARCHAR(20) NOT NULL, gender CHAR(1) DEFAULT ‘M‘, birth DATE, salary DOUBLE(6,2), comm DOUBLE(6,2), job VARCHAR(30), manager INT(4), deptno INT(2) );
2. 修改员工表
1)修改employee表的表名为myemp;
RENAME TABLE employee TO myemp;
ALTER TABLE employee RENAME myemp;
2)向myemp表增加一列hiredate,并设置默认值为当前日期;
ALTER TABLE myemp ADD (hiredate DATE);
ALTER TABLE myemp ADD hiredate2 TIMESTAMP DEFAULT NOW();
3) 修改myemp表中的job列的长度为40,并增加默认值的设置,默认值为‘CLERK’;
ALTER TABLE myemp MODIFY job VARCHAR(40) DEFAULT ‘CLERK‘;
4)删除myemp表中的hiredate列。
ALTER TABLE myemp DROP hiredate2;
5). 显示表结构
DESC myemp;
3. 插入数据到表中。
1.)插入数据
向myemp表的列id、name、job、salary插入数据,这些列的数据分别为 1001、 ‘rose‘、‘PROGRAMMER‘、5500,SQL语句如下所示:
INSERT INTO myemp (id, NAME, job, salary) VALUES(1001, ‘rose‘, ‘PROGRAMMER‘, 5500);
2.)插入日期
INSERT INTO myemp (id, NAME, job,birth) VALUES(1002, ‘martha‘, ‘ANALYST‘, ‘1983-11-23 00:00:00‘);
4. 更改员工表
1)将员工表中姓名为rose的员工的工资(salary)更改为8500。
UPDATE myemp SET salary = 8500 WHERE NAME = ‘rose‘;
2)将员工表中id为1002的员工的工资(salary)更改为6500,职位改为“ANALYST”。
UPDATE myemp SET salary = 6500, job = ‘ANALYST‘ WHERE id = 1002;
3.) 查看数据库变化
SELECT * FROM myemp;
5. 删除数据库数据
1)删除员工表中职位(job)为空的员工记录;
DELETE FROM myemp WHERE job IS NULL;
2)删除员工表中名字(name)为“rose”的员工。
DELETE FROM myemp WHERE NAME =‘ROSE‘;
3.)查看数据的变化。
SELECT * FROM myemp WHERE NAME =‘ROSE‘;
标签:
原文地址:http://www.cnblogs.com/liuhongfeng/p/4799132.html