一、数据库表的操作
右键新建数据库,双击打开数据库。
1、进入查询执行下列sql语句,插入表
CREATE TABLE staff_list(id INT,姓名 VARCHAR(20),性别 VARCHAR(20),工号 VARCHAR(10),入职时间 INT,体重 FLOAT);//创建数据表staff_list
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(1,\'李小二\',\'男\',\'1001\',3,60.0);
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(2,\'李小三\',\'男\',\'1002\',3,70.0);
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(3,\'王明\',\'男\',\'1003\',5,72.3);
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(4,\'王紫\',\'女\',\'1004\',3,50.2);
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(5,\'张雪\',\'女\',\'1005\',3,45.9);
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(6,\'张明\',\'男\',\'1006\',7,88.0);
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(7,\'赵磊\',\'男\',\'1007\',3,72.5);
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(8,\'赵花\',\'女\',\'1008\',3,60.0);
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(9,\'郑王\',\'男\',\'1009\',7,65.4);
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(10,\'杨名\',\'女\',\'1010\',3,50.0);
SELECT * FROM staff_list;
右键关闭数据库,刷新表,就可以在表中看到新建的表
2、对数据库表进行操作
可以如上图直接增添表内容。也可打开命令列界面,进行操作:
通过下列命令操作:
SELECT * FROM staff_list;//查询学生表所有内容,*代表查询表格中所有内容
SELECT * FROM staff_list where 体重 > 70 and 性别 = \'男\';//查询条件
SELECT * FROM staff_list where 姓名 like \'王%\';
SELECT * FROM staff_list where 姓名 like \'%小%\';//包含
INSERT INTO staff_list(id,姓名,性别,工号,入职时间,体重) VALUES(11,\'赵思\',\'男\',\'1011\',5,60.5);//向表中添加数据
INSERT INTO staff_list VALUES(12,\'王石\',\'男\',\'1012\',1,62.5);
delete from staff_list where id = 7;
update staff_list set 工号 = \'1013\',体重 = 53 where id = 6;//修改表中数据
3、多表操作
staff_list表右键打开设计表,增加一列lead_id,可以再增加一些表数据;新建表lead_list如下:
通过下列命令进行操作
SELECT * FROM staff_list,lead_list where staff_list.lead_id = lead_list.id;
//通过staff_list表格的lead_id进行连接多表查询
SELECT * FROM staff_list,lead_list where staff_list.lead_id = lead_list.id and lead_list.name = \'Tom\';
SELECT * FROM staff_list,lead_list where staff_list.lead_id = lead_list.id and lead_list.name = \'Tom\' order by staff_list.入职时间;
//asc正序排列(可省略),desc倒序
SELECT * FROM staff_list,lead_list where staff_list.lead_id = lead_list.id and lead_list.name = \'Tom\' order by staff_list.入职时间 asc;
SELECT * FROM staff_list,lead_list where staff_list.lead_id = lead_list.id and lead_list.name = \'Tom\' order by staff_list.入职时间 desc;