1.啟動(dòng)MySQL服務(wù)
啟動(dòng):net start mysql;
關(guān)閉:net stop mysql;
登錄數(shù)據(jù)庫:mysql -uroot -p123
2.查看數(shù)據(jù)庫:show databases;
3.使用test數(shù)據(jù)庫:use test;
4.創(chuàng)建test數(shù)據(jù)庫:create database test;
create database if not exits test;(如果數(shù)據(jù)庫存在也不報(bào)錯(cuò))
5.刪除test數(shù)據(jù)庫:drop database test;
drop database if exits test;(如果數(shù)據(jù)庫不存在也不報(bào)錯(cuò))
6.修改數(shù)據(jù)庫編碼為UTF-8:alter database test character set utf8;(utf-8的-要省略)
7.查看當(dāng)前數(shù)據(jù)庫中的表:show tables;
8.查看test表的創(chuàng)建語句:show create table test;
9.查看test表的表結(jié)構(gòu):desc test;
10.刪除test表:drop table test;
11.創(chuàng)建表user:
? primary key(主鍵):主鍵定義為int,定義為varchar會(huì)報(bào)錯(cuò);
? auto_increment(自增長(zhǎng)):只有主鍵能設(shè)置自增長(zhǎng),其他字段不能;
not null(非空):非空約束能多個(gè)存在
unique(唯一):唯一約束能多個(gè)存在
? create table user(
? ? ?? id ? int(10) ? ?? primary key? auto_increment,
? ? ?? password? varchar(10) ?? not null? unique,
? ? ?? age? int(5)
? ? ?? );
create table user(
? ? ? id? int(10),
? ? ? password? varchar(10),
? ? ? age? int(5),
? ? primary key(id)
? ? ? );
12.test表中添加列id
alter table test add (id int(5));
13.修改test表的id列的類型為char
alter table test modify id char(5);
14.修改test表的id列名為age(可同時(shí)修改數(shù)據(jù)類型)
alter table test change id age int(5);
15.test表刪除name列
alter table test drop name;
16.重命名test表為user
alter table test rename to user;
17.查看user表中數(shù)據(jù)(*表示所有字段)
select * from user;
select id ,name? from user;
18.去除user表中重復(fù)記錄
insert into user values(1,'123');
insert into user values(1,'123');
insert into user values(2,'123');
insert into user values(1,'1234');
select distinct * from user;

select distinct id from user;

select distinct name from user;

19.查看emp表sal和comm之和
因?yàn)閟al和comm兩列的類型都是數(shù)值類型,所以可以做加運(yùn)算。如果sal或comm中有一個(gè)字段不是數(shù)值類型,那么會(huì)出錯(cuò)。
select *,sal+comm from emp;
comm列有很多記錄的值為NULL,因?yàn)槿魏螙|西與NULL相加結(jié)果還是NULL,所以結(jié)算結(jié)果可能會(huì)出現(xiàn)NULL。下面使用了把NULL轉(zhuǎn)換成數(shù)值0的函數(shù)IFNULL:
select *,sal+ifnull(comm,0) from emp;
20.給id列添加sfz 別名(as可省略)
select id as sfz from user;
select id sfz from user;
21.退出數(shù)據(jù)庫
exit