總結:1、資料庫操作
:建立庫:
create database db_name;
查詢庫:
show databases; //顯示所有的資料庫
show create databases db_name; //顯示特定的資料庫
刪除庫:
drop database db_name;
修改庫:
alter database db_name [modify command];
alter database db_name character set gbk;
2、表操作
:建立表:
use db_name;
create table tbl_name (column_structure) [tbl_option];
如:create table tbl_name(
variable_name1 varchar(10 )
variable_name2 int );
或 create table db_name.tbl_name(
variable_name1 varchar(10)
variable_name2 int );
查詢表:
檢視有哪些表
:
show tables;
或show tables like 'pattern_%';
檢視表的建立資訊:
show create table tbl_name;
或show create table tbl_name \g
檢視表的結構:
describe tbl_name;
或desc tbl_name;]
刪除表:
drop table [if exists] tbl_name;
修改表:
修改表名:
單個:rename table old_tbl_name to new_tbl_name;
多個:rename table old_tbl_name1 to new_tbl_name1, old_tbl_name2 to new_tbl_name2;
跨資料庫:rename table old_tbl_name to db_name.new_tbl_name;
修改列定義:
增加新列定義:add
alter table tbl_name add new_column_name data_type;
如alter table exam_student add height int;
修改列的定義(新的屬性或者資料型別):modify
alter table tbl_name modify column_name new_data_type;
刪除乙個列:drop
alter table tbl_name drop column_name;
重新命名乙個列:
change
alter table tbl_name change old_column_name new_column_name new_data_type;
修改表選項:
alter table tbl_name new_tbl_option;
alter table tbl_name character set utf8;
3、資料操作
:建立資料:
insert into tbl_name (fields_list) values (values_list);
如:insert into exam_student (stu_name, stu_no) values ('xiaoming', 'php030_01');
若插入所有字段值:
insert into exam_student values ('xiaoming', 'php030_01', 98);
查詢資料:
檢視字段資訊:
select * from tbl_name;
參看具體字段列表資訊
:
select fields_list from tbl_name where condition;
如:預設狀態下select stu_name, stu_no from exam_student; 或:
select stu_name, stu_no from exam_student where 1;
有條件查詢:
select * from tbl_name where condition;
如select * from exam_student where fenshu >= 60;
刪除資料:
delete from tbl_name condition;
如delete from exam_student where fenshu <= 50;
修改資料:
update tbl_name set field = new_value where condition;
如:update exam_student set fenshu =100 where fenshu >= 97;
4、主鍵:primary
如:法一:
create table teacher(
t_id int primary key [auto_increment],
t_name varchar(5),
class_name varchar(6),
t_days tinyint unsigned
);法二:
create table teacher(
t_id int,
t_name varchar(5),
class_name varchar(6),
t_day tinyint unsigned,
primary key (t_id)
);insert into teacher values (1, '王老師', '0225', 23);
在已建立表中新增主鍵操作:
alter table tbl_name add primary key (field) ;
alter table tbl_name modify colunm_name data_type not null primary key auto_increment;
自動增長:auto_increment
alter table tbl_name auto_increment initial_value;
5、外來鍵:foreign key
如:主表:
create table itcast_class(
class_id int primary key auto_increment,
class_name varchar(10) not null default 'itcast_php' comment '班級名稱'
) character set utf8;
從表:create table itcast_student(
stu_id int primary key auto_increment,
stu_name varchar(10) not null default '',
class_id int,
foreign key (class_id)
references itcast_class (class_id)
) character set utf8;
插入資料的順序:先主表,後從表
insert into itcast_class values (null, 'php0331');
insert into itcast_student values (null, '張三', 1);
刪除操作:
alter table itcast_student drop foreign key itcast_student_ibfk_1;
alter table itcast_student add foreign key (class_id) references itcast_class (class_id)
on delete set null;/*主表裡刪除某個字段,從表若對應外來鍵則變成null*/
或者on delete cascade;/*主表刪除乙個字段,從表對應的外來鍵則會刪除相應的值*/
又或者on update restrict;/*不允許主表進行更新操作*/
delete from itcast_class where class_id = 1;
6、最大值
select max(field) from tbl_name;
7、連線
內連線:
select tbl1_name.field, tbl2_name.field1, tbl2_name.field2... from tbl1_name inner join tbl2_name on tbl1_name.id = tbl2_name.id where condition;
外連線:
select tbl1_name.fields, tbl2_name.fields from tbl1_name left outer join tbl2_name on tbl1_name.id = tbl2_name.id;
8、更改資料庫密碼方法:
進入mysql中後,在命令列中mysql>
輸入:set password for
root@localhost = password('123456'
);
MYSQL常用操作指令
最近到研究php跟mysql的開發環境搭配,測試過win2003 iis6.0 x86作業系統下比較好的軟體php5foriis6v2.0.exe主要包括php5.25 mysql5.045 zend3.3a phpmyadmin 3.3.0等軟體。win2008 iis7.0 x64位系統下比較好...
MySql常用操作指令
1 使用show語句找出在伺服器上當前存在什麼資料庫 mysql show databases 2 2 建立乙個資料庫mysqldata mysql create database mysqldata 3 選擇你所建立的資料庫 mysql use mysqldata 按回車鍵出現database c...
mysql常用指令 Mysql常用指令
mysql常用指令2021 01 19 23 40 45 作用 去除select 查詢出來的結果中重複的資料,重複資料只展示一列.關鍵字 distinct 用法 select distinct source from student table source 去重的字段條件 student tabl...