注:可以使用navicat for mysql進(jìn)行管理mysql
1似炎、數(shù)據(jù)庫:person
創(chuàng)建 create databases person辛萍;
查看 show databases;
指定使用? use person羡藐;
刪除 drop databases person;
2悯许、表:person
creat table person(
列名1 數(shù)據(jù)類型描述仆嗦,
列名2 數(shù)據(jù)類型描述,
列名3 數(shù)據(jù)類型描述先壕,
列名4 數(shù)據(jù)類型描述瘩扼,
)谆甜;
creat table person(
id int unsigned not null auto_increment primary key,
name char(10) not null,
age tinyint not null,
sex varchar(5) not null,
tel char(13) null default "-"
);
[unsigned 無符號(hào), not null 非空集绰, auto_increment 遞增规辱, primary key 主鍵]
顯示表:show tables;
查看表結(jié)構(gòu):
describe person ;
或者? show columns from/ in person;
根據(jù)舊表創(chuàng)建新表
create table student like person栽燕;
create table student as select id罕袋,name from person;
復(fù)制整個(gè)表
create table student as select * from person碍岔;
[as 可以省略]
刪除表:
drop table person浴讯;
重新命名表:
rename table person to person1
或者
alter table person rename person1
表名:person
字段:id,name蔼啦,age
1 張三 20
2 李四 22
3 王五 23
查詢:
select id榆纽,name,age from person捏肢;
select * from person
select id from person where name="李四"
selext id from person where name like "%王%"? 名字中帶有王字的id
select name from person where age is null
select name from person where id>=2 order by desc? ? 降序
select name from person where id>=2? ? ? ? ? ? ? ? ? 默認(rèn)升序
select name from person where id>=2 order by asc? ? 升序
select top 2 name from person? ? ? ? ? ? ? mysql顯示name的前二行
select * from person? where rownum < 2? ? oracle 顯示name的前二行
select name '成都' as 地址 from person? ? 查詢name列 添加地址列 值為成都
select name as 姓名 from person where id=2 選出表中id為2的行奈籽,顯示name列,并將name列改為姓名顯示
刪除:delete from person where id=1;
修改:update person set name="劉德華" where id=2;
增加:insert into person(id鸵赫,name唠摹,age) values(4,'劉能',24);
? ? ? 或者不要列名,按表定義的順序填寫
? ? ? insert into person values(4,'劉能',24);
另外一張表:
student
insert into student(id,name,age) select id,name,age from person
刪除整個(gè)表的數(shù)據(jù)
truncate table person
添加列:
表的最后添加
alter table? person add? sex char(3) not null奉瘤;
在指定的列后面添加:
alter table person add address cha(5) after age勾拉;
在表的最前面添加
alter table person add address cha(5) first;
修改列的列名:
alter table person change address address1 char(10) null default "-";
修改列的數(shù)字類型:
alter table person change name name char(20) not null;
刪除列:
刪除表person 中sex列盗温;
alter table person drop sex藕赞;
求某列數(shù)據(jù)總數(shù)(不包含缺省)
select count(age) as totalcount from person;
統(tǒng)計(jì)表的總行數(shù):
select conut(*) as totalcount from person;
求某列數(shù)據(jù)的總和卖局;
select sum(id) as idsum from person;
求某列數(shù)據(jù)的平均值斧蜕;
select avg(age) as ageavg from person;
求某列數(shù)據(jù)的最大最小值:
slect max(age) as maxage from person砚偶;
slect min(age) as minage from person批销;