1、net start mysql 首先要啓動MySQL服務,所以得用這個命令;net stop mysql 關閉mysql服務。
2、msyql -h localhost -u root -p (-h後面參數是服務器的主機地址,輸入localhost或者127.0.0.1;-u指的是登錄數據庫的用戶名 -p指的是用戶密碼)
3、show databases; 用於查看存在的數據庫
create database database_name;用於創建數據庫
drop database database_name;用於刪除數據庫
4、創建表
create table table_name(
字段名1 ,數據類型【約束】【默認值】,
字段名2 ,數據類型【約束】【默認值】,
字段名3 ,數據類型【約束】【默認值】,
........
【表級別約束函數】
);
5、使用主鍵約束(單字段主鍵、多字段聯合主鍵)
A.在定義列時同時制定主鍵
字段名 數據類型 primary key [默認值]
B. 在定義完所有的列再指定主鍵
[constraint <約束名>] primary key [字段名]
6、使用非空約束、唯一性約束(要求該列唯一,允許爲空,但是隻能出現一個空值)
字段名 數據類型 not null
字段名 數據類型 unique 或者 在最後 [constraint <約束名> unique <字段名>
7、默認約束
字段名 數據類型 default 默認值
8、表的屬性值自動增加
字段名 數據類型 atuo_increment
9、查看數據表結構
DESCRIBE table_name;
DESC table_name;
10、查看錶的詳細結構
Show create table table_name \G; (其中\G 參數是爲了使顯示的結構更加直觀,易於觀察)
11、修改數據表
修改:
1.(修改表名)
alter table table_name1 rename table_name2;
2.(修改字段的數據類型)
Alter table table_name modify 字段名 數據類型;
3. (修改字段名)
Alter table table_name change 舊字段名 新字段名 數據類型;
補充:change也可以只修改數據類型,和modify達到相同的效果,將新舊字段名設爲一樣,只修改數據類型。
添加:
4. 添加無完整性約束條件字段
Alter table table_name add managerId int (20);(add後面爲自定義的添加字段)
5. 添加有完整性約束條件字段
Alter table table_name add column1 varchar(23) not null;(add後面爲自定義的添加字段和約束)
6.在表的第一列添加一個字段
Alter table table_name add column2 int(11)first;
7.在表指定列之後添加一個字段
Alter table table_name add column3 int(11) after column2;
8.刪除字段
Alter table table_name drop 字段名
9. 修改字段的排列位置
Alter table table_name modify 字段1 數據類型 first|after 字段2;
10. 更改表的引擎
Alter table table_name engine=存儲引擎名;
11. 刪除表的外鍵操作
Alter table table_name drop foreign key 外鍵約束名
12. 刪除沒有被關聯的表
Drop table [if exists] table1,table2,table3.....tablen;
13. 刪除被關聯的表
如果有關聯約束,如外鍵約束,則先把外鍵約束刪掉,再重新過來把表刪除掉
Create database company;
Use company;
Create table offices(
officeCode int(10) primary key not null unique,
city varchar(50) not null,
address varchar(50),
country varchar(50) not null,
postalCode varchar(15) unique
);
實例一:在進行表的修改全過程可以採用desc table_name;進行查看是否變化)
Create table employees(
employeeNumber int(11) primary key not null unique auto_increment,
lastName varchar(50) not null,
fistName varchar(50) not null,
mobile varchar(25) unique,
officeCode int(10) not null,
jobTitle varchar(50) not null,
Birth datetime not null,
Note varchar(255),
Sex varchar(5) ,
Constraint fk_emp_off foreign key (officeCode) references offices(officeCode)
);
Alter table employees modify mobile varchar(25) after officeCode;
Alter table employees change Birth employee_birth datetime;
Alter table employees modify sex char(1) not null;
Alter table employees drop note;
Alter table employees add favorite_activity varchar(100);
Alter table employees drop foreign key fk_emp_off ;
Drop table offices;
解惑:
1、表刪除時需謹慎
2、每個表不一定要有主鍵,但是多表進行連接操作時需要用到主鍵。
3、並不是每個表都可以選擇存儲引擎。外鍵約束是用來保證參照完整性,如果表需要關聯外鍵,卻指定了不同的存儲引擎,這些表之間是不能創建外鍵約束。簡言之,存儲引擎不同的表示不能創建外鍵約束的。
4、Auto_increment 約束的字段默認是從1開始的,可以指定第一條插入記錄自增字段的值。
實例二:
Create database Market;
Use Market;
Create table customers(
c_num int(11) primary key not null unique auto_increment,
c_name varchar(50),
c_contact varchar(50),
c_city varchar(50),
c_birth datetime not null
);
Alter table customers modify c_contact varchar(50) after c_birth;//修改字段順序
Alter table customers modify c_name varchar(70);//修改字段數據類型
Alter table customers change c_contact c_phone varchar(50);//修改字段名字
Alter table customers add c_gender char(1);//添加字段
Alter table customers rename customers_info;//修改表名
Alter table customers_info drop c_city varchar(50);//刪除字段
Alter table customers_info engine=MyISAM;//修改引擎
Create table orders(
o_num int(11) primary key not null unique auto_increment,
o_date date,
c_id varchar(50),
Constraint fk_em_or foreign key(o_num) references customers_info(c_num)//創建外鍵約束
);
Alter table orders drop foreign key fk_em_or;//刪除外鍵約束
Drop table customers_info;//刪除表