1、登录到mysql:
mysql -hlocalhost -uroot -p
2、创建数据库:
create database;
3、使用数据库:
use database;
4、创建表:
人员:qa
create table qa
(
id int not null auto_increment primary key,
age int not null,
name char(16) not null,
address varchar(80),
mobile char(11) ,
sex char(2) not null,
workid char(16) not null
);
5、设置索引:
ALTER TABLE qa ADD INDEX index_name (workid);
6、创建关联表
电脑(在使用中的):computerinuse
create table computerinuse
(
id int(10) not null auto_increment primary key,
model char(18) not null,
brand char(8) not null,
price float(8,1) not null,
userid char(16) not null,
foreign key(userid) references qa(workid) on delete cascade on update cascade
);
7、向表中插入数据
insert into qa values(0,24,"邵家波","济南市","13212345566","男性","00001021");
insert into computerinuse values(0,"k870","联想",6600.67,"00001021");
insert into qa(age,name,sex,workid) values(24,"孙甜","女性","00001025");
8、查询表中的数据(已经从excel导入数据)
qa表和computerinuse表
查询地址为北京市并且性别为男性的:SELECT * FROM `qa` where address ='北京市' and sex ='男性';
统计人数前三名的城市:SELECT address,COUNT(address) as num FROM `qa` GROUP BY address ORDER BY num DESC limit 3;
查询不同城市的数量:SELECT count(DISTINCT address) as num FROM `qa` ;
查询年龄大于等于20小于30的人,并按照年龄从小到大排序:SELECT * FROM `qa` WHERE age >=20 and age <30 ORDER BY age;
查询所有姓王的人:SELECT * FROM `qa` WHERE `name` like '王%';
8、更新表数据
update qa set address='保定市' where name ='王新';
update qa set sex='男性' where name ='陈羽凡';
9、删除表中的数据
delete from qa where address='香港';
10、修改表
添加列:alert table qa add position char(10);
修改列:alter table qa change address city char(20);
重命名表:alter table qa rename ceshi;
删除列:alter table qa drop position;
删除表:drop table qa;
删除数据库:drop database zdx;