小編髮現這東西長時間不用就所有忘記了,因此仍是常常翻開看或者寫博客總結養成這個習慣是有必要的!數據庫
建立及數據庫:create database 數據庫名;函數
查看數據庫:show databases;索引
選擇數據庫:use database;博客
刪除數據庫:drop database 數據庫名;table
在dos窗口中查詢數據庫中的數據的經常使用命令步驟:test
a:show databases;date
b:use test;(test庫名)select
c:show tables;數據類型
d:desc student;(student爲表名)im
e:進行增刪改查操做
例如查詢:
select * from student;
---------------------------------------------------------------------
建立表:
create table [if not exists] 表名(
字段1 數據類型 [字段屬性|約束] [索引] [註釋]
字段2 數據類型 [字段屬性|約束] [索引] [註釋]
.......................
) [表類型] [表字符集] [註釋]
1:單字段主鍵
create table [if not exists] 表名 (
字段1 數據類型 primary key;
............................
);
例如:
create table student(
student int(4) primary key,
.........................
);
2:多字段聯合主鍵
create table [if not exists] 表名(
primary key [字段1,字段2...........]
);
例如:
create table tb_temp(
'id' int(4),
'name' varchar(11),
.....................
primary key('id','name')
);
查看錶:show table;
刪除表:drop table [if not exists] 表名;
例如:drop table student;
---------------------------------修改表----------------------------------
修改表名:
alter table 舊錶名rename 新表名
例如:
ALTER TABLE easybuy_comment RENAME TO easybuy_com
添加字段:
alter table 表名 add 字段名 數據類型 [屬性]
例如:ALTER TABLE easybuy_comment ADD easybuy_num INT(10) NOT NULL
修改字段:
alter table 表名 change 原字段名 新字段名 數據類型 [屬性]
ALTER TABLE easybuy_comment CHANGE easybuy_num easybuy_sss INT(10) NOT NULL
刪除字段:
alter table 表名 drop 字段名
alter table easybuy_comment drop easybuy_sss
添加主鍵約束:
alter table 表名 add constraint 主鍵名 primary key 表名 (主鍵字段)
添加外鍵約束:
alter table 表名 add constraint 外鍵名 foreign key (外鍵字段) references 關聯表名 (關聯字段)
------------------------------使用DML插入數據---------------------------
1:插入單行數據
insert into 表名(字段名列表) values (列表值)
例如:
insert into student (id,pass,usename) values (1,‘aa’,‘哈哈1’)
2:插入多行數據
insert into 新表 (字段列表) values (列表值1),(列表值2),(列表值3),.....(列表值n)
例如:
insert into student (id,pass,usename)values(1,‘aa’,‘哈哈1’),(2,‘bb’,‘哈哈2’),(3,‘cc’,‘哈哈3’)
3:將查詢結果插入到新表
create table 新表(select studentName,phone, from student)
-----------------------------使用DML更新數據-----------------------------------------------
update 表名 set 列名=更新值 where 更新條件
例如:
update student set sex=‘女’
update student set address=‘北京’ where address=‘甘肅’
-----------------------使用DML刪除數據---------------------------------------------
delete from 表名 where 刪除條件
例如:
delete from student where studentName=‘王寶寶’
刪除表中全部記錄
truncate table student
--------------------------使用DML查詢數據---------------------------------------------------
select 列名|表達式|函數|常量 from 表名 where 查詢條件 order by asc|desc