Linux安裝MySQL
1.查看是否安裝MySQL數(shù)據(jù)庫
rpm -qa | grep mysql
2.查看MySQL的安裝版本
yum list | grep mysql
3.安裝MySQL
yum install mysql mysql-server mysql-devel
1)mysql:客戶端
2)mysql-server:服務(wù)端
3)mysql-devel:開發(fā)的服務(wù)包
4.設(shè)置MySQL超級管理賬號root密碼為root
mysqladmin -u root password 'root'
5.登錄MySQL
mysql -uroot -p
mysql -uroot -proot
6.強(qiáng)制卸載MySQL
rpm -e --nodeps mysql
7.MySQL服務(wù)
① MySQL服務(wù)啟動
service mysqld start
② MySQL服務(wù)關(guān)閉
service mysqld stop
③ MySQL服務(wù)關(guān)閉
service mysqld restart
8.MySQL設(shè)置開機(jī)啟動
① 查看MySQL的開機(jī)啟動
chkconfig --list | grep mysql
② 設(shè)置MySQL開機(jī)啟動
chkconfig mysqld on
③ MySQL服務(wù)關(guān)閉
chkconfig mysqld off
9.修改密碼
mysqladmin -u root
10.連接數(shù)據(jù)庫
授權(quán)
把端口加入防火墻
MySQL字段類型
1.int:數(shù)字類型
2.char:字符串類型
3.varchar
4.bit:比特,默認(rèn)是1
5.float:單精度浮點(diǎn)類型;float(M, D):精度到一位小數(shù),M代表顯示的最大位數(shù),D是小數(shù)點(diǎn)后面的數(shù),沒有四舍五入
① 例:float(5, 2),如果輸入的數(shù)字是321.321,數(shù)據(jù)庫顯示是321.32
6.double:雙精度浮點(diǎn)類型;double(M, D):精度比float高
① 例:float(5, 2),如果輸入的數(shù)字是321.327,數(shù)據(jù)庫顯示是321.33,會四舍五入進(jìn)行計(jì)算
7.longtext:長文本類型
8.text:長文本類型
9.date:日期類型
10.datetime
11.timestamp
數(shù)據(jù)庫
create database 數(shù)據(jù)庫名:創(chuàng)建數(shù)據(jù)庫
show database 數(shù)據(jù)庫名:查看創(chuàng)建數(shù)據(jù)庫的語句
show databases:查看所有的數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名:刪除數(shù)據(jù)庫
alter database 數(shù)據(jù)庫名 character utf8:修改數(shù)據(jù)庫的字符集為utf8
創(chuàng)建表
use 數(shù)據(jù)庫名:進(jìn)入制定的數(shù)據(jù)庫
create table 表名(字段名 類型(長度), 字段名 類型(長度)):創(chuàng)建表
show tables;:查數(shù)據(jù)庫下所有的表
show create table 表名;:查看表的創(chuàng)建語句
desc 表名:查看表結(jié)構(gòu)
desc mytable 查看表mytable的結(jié)構(gòu)

field 字段名稱
type 字段的類型
null 是否為空
key 唯一約束
insert into 表名 values(值1, 值2):向表中插入數(shù)據(jù)
select * from 表名:查看表數(shù)據(jù)
修改表
rename table 舊表名 to 新表名:修改表名
drop table 表名:刪除表
alter table 表名 change column 舊字段名 新字段名 字段類型(長度):修改字段名稱
alter table 表名 change column 舊字段名 舊字段名 字段新類型(長度):修改字段類型
alter table 表名 change column 舊字段名 就字段名 字段新類型(新長度):修改字段的長度
alter table 表名 change column 舊字段名 新字段名 字段新類型(新長度):同時修改字段名和字段類型
alter table 表名 add column 字段名 字段類型(長度):添加字段
alter table 表名 drop 字段名:刪除字段
數(shù)據(jù)表的增刪改查
update 表名 set 字段名=值:修改表數(shù)據(jù)
update 表名 set 字段名=值 where 字段=值:修改指定的某個數(shù)據(jù)
delete from 表名:刪除表數(shù)據(jù)
delete from 表名 where 字段名=值:刪除表中指定某個數(shù)據(jù)
wanglaoshipizhun
insert into 表名 values(值1, 值2), (值1, 值2):向表中插入多行數(shù)據(jù)
select * from 表名:查看表中所有的數(shù)據(jù)
select * from 表名 where 字段名=值:按條件進(jìn)行查詢數(shù)據(jù)
select 字段 from 表名:查看表中的數(shù)據(jù)的指定的字段
數(shù)據(jù)表的排序
select * from 表名 order by 字段名 desc/asc:排序(desc降序,asc默認(rèn)升序)
select 字段名 as 別名 from 表名:設(shè)置字段的別名
select t.字段名 as 別名 from 表名 t:設(shè)置表的別名
select * from 表名 where 字段名=值 and 字段名=值:and查詢
select * from 表名 where 字段名=值 or 字段名=值:or查詢
select * from student where not(age is NULL):not查詢①
select * from student where age is not NULL:not查詢②
select * from student where age>20:大于
select * from student where age<20:小于
select * from student where id in(1, 2, 3, 4):in 在某某里面
select * from student where age between 20 and 30
select * from student where age between age>=20 and age<=30:between and 在某某之間(20<=age<=30)
select * from student where name like '邱%'
select * from student where name like '%邱%'
select * from student where name like '%邱':模糊查詢
select distinct id from student:去除重復(fù)數(shù)據(jù)
truncate student快速刪除數(shù)據(jù)