mysql常用命令

添加用戶

CREATE USER 'john'@'192.168.189.71' IDENTIFIED BY "123";

授權

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;


如何登陸數據庫

    登陸本機
    mysql -u <username> -p

    登陸本機某個數據庫
    mysql -u <username> -D <database_name> -p

    登陸遠程某個數據庫
    mysql -h <hostname> -u <username> -D <database_name> -p
    
    登陸遠程某個數據庫,從特定端口
    mysql -h <hostname> -P <port> -u <username> -D <database_name> -p

如何執行sql腳本
    mysql > source <scriptname.sql>

如何參看有哪些庫
    show databases
    
如何切換數據庫
    use <database_name>

如何參看庫中有哪些表
    show tables
    
如何查看正在使用哪個數據庫

    status

查看數據狀態,參數

show status

修改mysql密碼
mysqladmin -u root -p password {new_password}

允許mysql遠程連接
update user set host = '%' where user = 'root';

查詢表定義
show create table {table_name};

show columns from {table_name};

describe {table_name};



創建數據庫
    create database {databas_name}

刪除數據庫
    drop database {databas_name}

新建表
    create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

    create table table1(
    -> col1 int primary key,
    -> col2 int,
    -> col3 varchar(30),
    -> )
    -> ;

    create table Teacther
    (
    tNo int primary key,
    tName varchar(50) not null,
    tSex  varchar(2) not null check (tsex in ('男','女')),
    tBirthDate datetime not null,
    tSalary decimal(18,2),
    tHairDate datetime,
    depNo int
    foreign key(depNo) references Depart(depNo)
    )

根據已有的表創建新表
    create table {new_table_name} like {old_table_name}
    create table {new_table_name} as select {[col1_name],[col2_name}, …} from {old_table_name} definition only

插入數據
    insert into {table_name} (field1,field2) values(value1,value2);
    insert into table1 (col1,col2,col3) values(1,1,"aaa");

更新數據
    update {table_name} set {field1}={value1} where {condition};
    update table1 set col3="zzz" where col1=1;

查詢數據
    select {field1,field2} from {table_name} where {condition};
    select * from table1;

    select distinct {column} from {table_name} 顯示唯一值

    select {column} from {table_name} limit {num} 限制檢索的行數

    select {column} from {table_name} limit {num1, num2} 從第num1開始的num2個行

    select {column} from {table_name} order by {column} desc; 按降序排列
    select {column} from {table_name} where {column} between {num1} and {num2} 範圍查找

    select {column} from {table_name} where {column} is null 查找空值

    select from where group by having order by limit
刪除數據
    delete from {table_name} where {condition};
    delete from table1 where col1=2;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章