1巷蚪、MySQL常用命令:
啟動MYSQL服務(wù)
net start mysql
停止MYSQL服務(wù)
net stop mysql
連接mysqld
mysql -h localhost -u root -p
修改當(dāng)前用戶密碼
update user set password=password(”xueok654123″) where user=’root’;
創(chuàng)建用戶
CREATE USER username1 identified BY ‘password’ , username2 IDENTIFIED BY ‘password’….
授權(quán)用戶
grant all privileges on *.* to user@localhost identified by ’something’ with grant option;
刷新數(shù)據(jù)庫
flush privileges
顯示當(dāng)前的user
SELECT USER();
顯示當(dāng)前mysql版本和當(dāng)前日期
select version(),current_date;
顯示數(shù)據(jù)庫狀態(tài)
status;
顯示所有數(shù)據(jù)庫
show databases;
創(chuàng)建數(shù)據(jù)庫
create database [if not exists] name;
刪除數(shù)據(jù)庫
drop database [if exists] name;
選擇數(shù)據(jù)庫
use databasename;
顯示當(dāng)前選擇的數(shù)據(jù)庫
select database();
顯示表
show tables;
創(chuàng)建表
create table [if not exists] name;
刪除表
drop table [if exists] name;
表的詳細(xì)描述
describe tablename;
創(chuàng)建表的詳細(xì)描述
show create table tablename;
修改表結(jié)構(gòu)
alter table t1 rename t2;
添加數(shù)據(jù)
insert into 表名 [(字段1 , 字段2 , ….)] values (值1 , 值2 , …..);
查詢所有數(shù)據(jù)
select * from table_name;
查詢指定字段的數(shù)據(jù)
select 字段1 , 字段2 from table_name;
更新指定數(shù)據(jù) , 更新某一個(gè)字段的數(shù)據(jù)
update table_name set 字段名=’新值’ [, 字段2 =’新值’ , …..][where id=id_num] [order by 字段 順序]
刪除整個(gè)表中的信息
delete from table_name;
刪除表中指定條件的語句
delete from table_name where 條件語句 ; 條件語句如 : id=3;
表結(jié)構(gòu)的修改
增加一個(gè)字段格式:
alter table table_name add column (字段名 字段類型);
指定字段插入的位置:
alter table table_name add column 字段名 字段類型 after 某字段病毡;
刪除一個(gè)字段:
alter table table_name drop字段名;
修改字段名稱/類型
alter table table_name change 舊字段名 新字段名 新字段的類型;
改表的名字
alter table table_name rename to new_table_name;
一次性清空表中的所有數(shù)據(jù)
truncate table table_name;
更改字符集
alter table users character set GBK;
增加主鍵,外鍵屁柏,約束啦膜,索引
約束(主鍵Primary key、唯一性Unique淌喻、非空Not Null)
自動增張 auto_increment
外鍵Foreign key-----與reference table_name(col_name列名)配合使用僧家,建表時(shí)單獨(dú)使用
刪除多個(gè)表中有關(guān)聯(lián)的數(shù)據(jù)----設(shè)置foreign key 為set null ---具體設(shè)置參考幫助文檔
查詢:
查詢ID號大于用戶lyh1的ID號的所有用戶
select a.id,a.nikename,a.address from users a,users b where b.nikename='lyh1' and a.id>b.id;
------也可寫成
select id,nikename,address from users where id>(select id from users where nikename='lyh1');
顯示年齡比領(lǐng)導(dǎo)還大的員工:
select a.name from users a,users b where a.managerid=b.id and a.age>b.age;