天天看點

MySQL基本用法MySQL基本用法

MySQL基本用法

登陸資料庫
MySQL -uroot -p

檢視資料庫
show databases;

選擇資料庫
use xx;

查詢資料庫中的表
show tables;

---------------------------------
MySQL基本操作建立表
create table test_table
(
    tid     int()         not null,
    name    varchar()    not null,
    date    datetime        not null    default '0000-00-00',
    primary key             (tid)
);
---------------------------------

檢視表結構
desc test_table;

添加列
alter table test_table 
add (age int());

修改原表結構
alter table test_table 
modify age int() not null;

修改列的預設值
alter table test_table 
alter age set default '0';

去掉列的預設值
alter table test_table 
alter age drop default;

删除列
alter table test_table 
drop column age;

插入資料
insert into test_table (tid, name, tdate) 
value(,'yangjuqi','2008-03-21');

查詢資料
select * 
from test_table;

模糊查詢
select * 
from test_table 
where name like '%楊%';

修改資料
update test_table 
set name = '張三' 
where tid = '2';

MySQL基本操作删除資料
delete from test_table 
where tid = '2';

删除表
drop table test_table;

重命名表
alter table test_table 
rename test_tablebak;

分頁查詢(limit 起始行,取多少行)
select * 
from test_tablebak 
limit ,;

重新整理資料庫
flush privileges;

顯示資料庫版本
select version();

顯示目前時間
select current_date;

修改使用者密碼
MySQLadmin -uroot -p  password 

将查詢出的資料寫入檔案
select * 
from test_tablebak 
into outfile "test_table.txt" 
fields terminated by ",";

檢視資料庫狀态
status;

MySQL基本操作檢視所有編碼
show variables like 'character_set_%';

導入sql檔案指令
MySQL>source MySQL.sql;