create database db1; create database db1 default charset gbk; create database if not exists db1 default character set utf8;
show create database db1;
alter database db1 default character set gbk;
drop database db1;
通配符:java
% 匹配0個或任意多個字符
_ 匹配一個字符
= 精確匹配
like 模糊匹配
regexp (. 任意單個字符 *前導字符出現0次或連續屢次 .* 任意長度字符.....) 使用正則表達式來匹配
排序:mysql
order by 排序,默認升序
asc 升序排列結果
desc 降序排列結果
group by 聚合
distinct 去除重複的行
查看mysql支持字符加密函數:正則表達式
select password('123'); select md5('123'); select sha1('123'); select encrypt('123'); 基本上不用了
使用select來調度mysql中的常見函數:sql
select version(); 當前數據庫版本
select current_user(); 當前用戶
select current_time(); 當前時間
select current_date(); 當前日期
select now(); 當前日期時間
表是數據庫存儲數據的基本單位,由若干個字段組成,主要用來存儲數據記錄。表的操做
包括建立表、查看錶、修改表和刪除表。數據庫
語法:ide
create table 表名(
字段名1 類型[(寬度) 約束條件],
字段名2 類型[(寬度) 約束條件],
字段名3 類型[(寬度) 約束條件]
)[存儲引擎 字符集];
說明:
auto_increment表示自增加
primary key表示主鍵
not null表示不爲空
==在同一張表中,字段名是不能相同 ==寬度和約束條件可選 ==字段名和類型是必須的
表school.student1函數
表school.student1 字段 字段 字段 id name sex age 1 tom male 23 記錄 2 jack male 21 記錄 3 alice female 19 記錄 mysql> CREATE DATABASE school; //建立數據庫school mysql> use school; mysql> create table student1( -> id int, -> name varchar(50), -> sex enum('m','f'), -> age int -> ); Query OK, 0 rows affected (0.03 sec)
mysql> show tables; //查看錶(當前所在庫) +------------------+ | Tables_in_school | +------------------+ | student1 | +------------------+ 1 row in set (0.00 sec)
mysql> select * from student1; //查詢表中全部字段的值 Empty set (0.00 sec) mysql> select name,age from student1; //查詢表中指定字段的值 Empty set (0.00 sec)
向表中插入內容
語法:加密
insert into 表名(字段1,字段2...) values(字段值列表...);
表school.student2spa
字段名 數據類型 編號 id int 姓名 name varchar(50) 出生年份 born_year year 生日 birthday date 上課時間 class_time time 註冊時間 reg_time datetime mysql> create table student2( -> id int, -> name varchar(50), -> born_year year, -> birthday date, -> class_time time, -> reg_time datetime -> ); mysql> desc student2; mysql> insert into student2 values(1,'tom',now(),now(),now(),now()); mysql> insert into student2 values(2,'jack',1982,19821120,123000,20140415162545);
表school.student33d
id id int 姓名 name varchar(50) 性別 sex enum('male','female') 愛好 hobby set('music','book','game','disc') mysql> create table student3( -> id int, -> name varchar(50), -> sex enum('male','female'), -> hobby set('music','book','game','disc') -> ); mysql> desc student3; mysql> show create table student3\G mysql> insert into student3 values (1,'tom','male','book,game'); mysql> insert into student3 values (2,'jack','male','film'); mysql> select * from student3;
DESCRIBE查看錶結構
DESCRIBE 表名;
DESC 表名;
SHOW CREATE TABLE查看錶詳細結構
SHOW CREATE TABLE 表名;
做用:用於保證數據的完整性和一致性
約束條件 說明 PRIMARY KEY (PK) 標識該字段爲該表的主鍵,能夠惟一的標識記錄,不能夠爲空 UNIQUE + NOT NULL FOREIGN KEY (FK) 標識該字段爲該表的外鍵,實現表與表(父表主鍵/子表1外鍵/子表2外鍵)之間的關聯 NOT NULL 標識該字段不能爲空 UNIQUE KEY (UK) 標識該字段的值是惟一的,能夠爲空,一個表中能夠有多個UNIQUE KEY AUTO_INCREMENT 標識該字段的值自動增加(整數類型,並且爲主鍵) DEFAULT 爲該字段設置默認值 UNSIGNED 無符號,正數 ZEROFILL 使用0填充,例如0000001 說明: 1. 是否容許爲空,默認NULL,可設置NOT NULL,字段不容許爲空,必須賦值 2. 字段是否有默認值,缺省的默認值是NULL,若是插入記錄時不給字段賦值,此字段使用默認值 sex enum('male','female') not null default 'male' age int unsigned NOT NULL default 20 必須爲正值(無符號) 不容許爲空 默認是20 3. 是不是key 主鍵 primary key 外鍵 forengn key 索引 (index,unique...)
表school.student4
mysql> create table school.student4( -> id int not null, -> name varchar(50) not null, -> sex enum('m','f') default 'm' not null, -> age int unsigned default 18 not null, hobby set('music','disc','dance','book') default 'book,dance');
mysql> insert into student4 values(1,'jack','m',20,'book'); Query OK, 1 row affected (0.00 sec) mysql> insert into student4(id,name) values(2,'robin'); Query OK, 1 row affected (0.00 sec)
表company.department
mysql> create table company.department( -> dept_id int, -> dept_name varchar(30) unique, -> comment varchar(50));
primary key 字段的值是不容許重複,且不容許不NULL(UNIQUE + NOT NULL)
單列作主鍵
多列作主鍵(複合主鍵)
單列作主鍵
表school.student6 方法一
mysql> create table student6( -> id int primary key not null auto_increment, -> name varchar(50) not null, -> sex enum('male','female') not null default 'male', -> age int not null default 18); Query OK, 0 rows affected (0.20 sec) mysql> insert into student6 values (1,'alice','female',22); mysql> insert into student6(name,sex,age) values -> ('jack','male',19), -> ('tom','male',23); mysql> select * from student6; +----+-------+--------+-----+ | id | name | sex | age | +----+-------+--------+-----+ | 1 | alice | female | 22 | | 2 | jack | male | 19 | | 3 | tom | male | 23 | +----+-------+--------+-----+ 3 rows in set (0.00 sec)
表school.student7 方法二
mysql> create table student7( -> id int auto_increment not null, -> name varchar(50) not null, -> sex enum('male','female') not null default 'male', -> age int not null default 18, -> primary key(id)); Query OK, 0 rows affected (0.21 sec)
複合主鍵
表school.service
host_ip 存儲主機IP service_name 服務名 port 服務對應的端口 allow(Y,N) 服務是否容許訪問 主鍵: host_ip + port = primary key mysql> create table service( -> host_ip varchar(15) not null, -> service_name varchar(10) not null, -> port varchar(5) not null, -> allow enum('Y','N') default 'N', -> primary key(host_ip,port) -> ); Query OK, 0 rows affected (0.00 sec)
mysql> insert into service values ('192.168.2.168','ftp','21','Y'); Query OK, 1 row affected (0.09 sec) mysql> insert into service values ('192.168.2.168','httpd','80','Y'); Query OK, 1 row affected (0.03 sec)
表company.department3
CREATE TABLE department3 ( dept_id INT PRIMARY KEY AUTO_INCREMENT, dept_name VARCHAR(30), comment VARCHAR(50) );
父表company.employees
mysql> create table employees( -> name varchar(50) not null, -> mail varchar(20), -> primary key(name)) engine=innodb;
子表company.payroll
mysql> create table payroll( -> id int not null auto_increment, -> name varchar(50) not null, -> payroll float(10,2) not null, -> primary key(id), -> foreign key(name) -> references employees(name) -> on update cascade -> on delete cascade -> )engine=innodb;
mysql> select * from employees; +------+--------------+ | name | mail | +------+--------------+ | tom | 11111@qq.com | +------+--------------+ 1 row in set (0.00 sec) mysql> select * from payroll; +----+------+---------+ | id | name | payroll | +----+------+---------+ | 1 | tom | 11.00 | +----+------+---------+ 1 row in set (0.00 sec) mysql> update employees set name='tomaaa' where name='tom'; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from payroll; +----+--------+---------+ | id | name | payroll | +----+--------+---------+ | 1 | tomaaa | 11.00 | +----+--------+---------+ 1 row in set (0.00 sec) mysql> select * from employees; +--------+--------------+ | name | mail | +--------+--------------+ | tomaaa | 11111@qq.com | +--------+--------------+ 1 row in set (0.00 sec) mysql> delete from employees where name='tomaaa'; Query OK, 1 row affected (0.04 sec) mysql> select * from employees; Empty set (0.00 sec) mysql> select * from payroll; Empty set (0.00 sec)
mysql> update employees set name='tomaaa' where name='tom'; mysql> delete from employees where name='alice';
結論:
當父表中某個員工的記錄修改時,子表也會同步修改
當父表中刪除某個員工的記錄,子表也會同步刪除
語法:
ALTER TABLE 表名 RENAME 新表名;
ALTER TABLE 表名 ADD 字段名 數據類型 [完整性約束條件…], ADD 字段名 數據類型 [完整性約束條件…];
ALTER TABLE 表名 ADD 字段名 數據類型 [完整性約束條件…] FIRST;
ALTER TABLE 表名 ADD 字段名 數據類型 [完整性約束條件…] AFTER 字段名
ALTER TABLE 表名 DROP 字段名;
ALTER TABLE 表名 MODIFY 字段名 數據類型 [完整性約束條件…];
ALTER TABLE 表名 CHANGE 舊字段名 新字段名 舊數據類型 [完整性約束條件…];
ALTER TABLE 表名 CHANGE 舊字段名 新字段名 新數據類型 [完整性約束條件…];
實例:
1. 修改存儲引擎 mysql> alter table service -> engine=innodb; //engine=myisam|memore|.... 2. 添加字段 mysql> create table student10 (id int); mysql> alter table student10 -> add name varchar(20) not null, -> add age int not null default 22; mysql> alter table student10 -> add stu_num int not null after name; //添加name字段以後 mysql> alter table student10 -> add sex enum('male','female') default 'male' first; //添加到最前面 3. 刪除字段 mysql> alter table student10 -> drop sex; mysql> alter table service -> drop mac; 4. 修改字段類型modify mysql> alter table student10 -> modify age tinyint; mysql> alter table student10 -> modify id int not null primary key ; //修改字段類型、約束、主鍵 5. 增長約束(針對已有的主鍵增長auto_increment) mysql> alter table student10 modify id int not null primary key auto_increment; //錯誤,該字段已是primary key ERROR 1068 (42000): Multiple primary key defined mysql> alter table student10 modify id int not null auto_increment; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 6. 增長複合主鍵 mysql> alter table service2 -> add primary key(host_ip,port); 7. 增長主鍵 mysql> alter table student1 -> add primary key(id); 8. 增長主鍵和自動增加 mysql> alter table student1 -> modify id int not null primary key auto_increment; 9. 刪除主鍵[primary key auto_increment] a. 刪除自增約束 mysql> alter table student10 modify id int not null; b. 刪除主鍵 mysql> alter table student10 -> drop primary key;
mysql> create table new_service select * from service;
mysql> create table new1_service select * from service where 1=2; //條件爲假,查不到任何記錄
mysql> create table t4 like employees;