#顯示全部的數據庫mysql
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db | | db_utf | | mysql | | performance_schema | | test | +--------------------+
#建立數據庫sql
MariaDB [(none)]> create database s18; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db | | db_utf | | mysql | | performance_schema | | s18 | | test | +--------------------+
#查看數據庫的建立信息數據庫
MariaDB [(none)]> show create database s18; +----------+----------------------------------------------------------------+ | Database | Create Database | +----------+----------------------------------------------------------------+ | s18 | CREATE DATABASE `s18` /*!40100 DEFAULT CHARACTER SET latin1 */ | +----------+----------------------------------------------------------------+ MariaDB [(none)]> create database s18_2 character set utf8; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db | | db_utf | | mysql | | performance_schema | | s18 | | s18_2 | | test | +--------------------+ MariaDB [(none)]> show create database s18_2; +----------+----------------------------------------------------------------+ | Database | Create Database | +----------+----------------------------------------------------------------+ | s18_2 | CREATE DATABASE `s18_2` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+----------------------------------------------------------------+
#修改數據庫字符集編碼ide
MariaDB [(none)]> alter database s18 character set utf8; Query OK, 1 row affected (0.01 sec)
#刪除數據庫函數
MariaDB [(none)]> drop database s18_2; Query OK, 0 rows affected (0.00 sec)
#使用數據庫編碼
MariaDB [(none)]> use s18; Database changed
#查看當前使用的數據庫spa
MariaDB [s18]> select database(); +------------+ | database() | +------------+ | s18 | +------------+
數據表操做code
#建立表orm
MariaDB [s18]> create table c1( -> name varchar(20), -> age int); Query OK, 0 rows affected (0.01 sec)
#查看錶結構blog
MariaDB [s18]> desc c1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
#查看全部字段
MariaDB [s18]> select * from c1; Empty set (0.00 sec)
#查看當前數據庫下的全部表
MariaDB [s18]> show tables; +---------------+ | Tables_in_s18 | +---------------+ | c1 | +---------------+
#查看數據庫的建立表信息
MariaDB [s18]> show create table c1\G *************************** 1. row *************************** Table: c1 Create Table: CREATE TABLE `c1` ( `name` varchar(20) DEFAULT NULL, `age` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
#添加新字段
MariaDB [s18]> alter table c1 add salary float NOT NULL; Query OK, 0 rows affected (0.01 sec) MariaDB [s18]> desc c1; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | age | int(11) | YES | | NULL | | | salary | float | NO | | NULL | | +--------+-------------+------+-----+---------+-------+
完整性約束
1.主鍵約束
primary key:非空(not null)且惟一(unique)
MariaDB [s18]> alter table c1 add id int first; Query OK, 0 rows affected (0.01 sec) MariaDB [s18]> desc c1; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | age | int(11) | YES | | NULL | | | salary | float | NO | | NULL | | +--------+-------------+------+-----+---------+-------+
#修改字段
MariaDB [s18]> alter table c1 modify id int primary key auto_increment first; Query OK, 0 rows affected (0.01 sec) MariaDB [s18]> desc c1; +--------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | int(11) | YES | | NULL | | | salary | float | NO | | NULL | | +--------+-------------+------+-----+---------+----------------+
#更改表名
MariaDB [s18]> alter table c1 change name names varchar(20); Query OK, 0 rows affected (0.01 sec) MariaDB [s18]> desc c1; +--------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | names | varchar(20) | YES | | NULL | | | age | int(11) | YES | | NULL | | | salary | float | NO | | NULL | | +--------+-------------+------+-----+---------+----------------+
#刪除表字段
MariaDB [s18]> alter table c1 drop names; Query OK, 0 rows affected (0.01 sec) MariaDB [s18]> desc c1; +--------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | age | int(11) | YES | | NULL | | | salary | float | NO | | NULL | | +--------+---------+------+-----+---------+----------------+
#刪除表
drop table table_name;
表記錄操做
MariaDB [s18]> create table emp( -> id int primary key auto_increment, -> name varchar(20), -> gender tinyint, -> bir date, -> salary float(6,2), -> dep varchar(20)); Query OK, 0 rows affected (0.01 sec) MariaDB [s18]> desc emp; +--------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | gender | tinyint(4) | YES | | NULL | | | bir | date | YES | | NULL | | | salary | float(6,2) | YES | | NULL | | | dep | varchar(20) | YES | | NULL | | +--------+-------------+------+-----+---------+----------------+
1.增長表記錄
MariaDB [s18]> insert emp (id,name,gender,bir,salary,dep) values -> (1,'tom',1,'1989-12-12',99.99,'安保部'); MariaDB [s18]> select * from emp; +----+------+--------+------------+--------+-----------+ | id | name | gender | bir | salary | dep | +----+------+--------+------------+--------+-----------+ | 1 | tom | 1 | 1989-12-12 | 99.99 | 安保部 | +----+------+--------+------------+--------+-----------+ MariaDB [s18]> insert emp (id,name,gender,bir,salary,dep) values -> (2,'jerry',0,'1986-12-12',199.99,'安保部'), -> (3,'peter',1,'1988-12-12',2299.99,'高管部'); Query OK, 2 rows affected (0.00 sec) MariaDB [s18]> select * from emp; +----+-------+--------+------------+---------+-----------+ | id | name | gender | bir | salary | dep | +----+-------+--------+------------+---------+-----------+ | 1 | tom | 1 | 1989-12-12 | 99.99 | 安保部 | | 2 | jerry | 0 | 1986-12-12 | 199.99 | 安保部 | | 3 | peter | 1 | 1988-12-12 | 2299.99 | 高管部 | +----+-------+--------+------------+---------+-----------+
#使用set也能夠插入記錄
MariaDB [s18]> insert emp set name='prog',gender=1,bir='1990-04-12',salary=8000,dep='教學部'; MariaDB [s18]> select * from emp; +----+-------+--------+------------+---------+-----------+ | id | name | gender | bir | salary | dep | +----+-------+--------+------------+---------+-----------+ | 1 | tom | 1 | 1989-12-12 | 99.99 | 安保部 | | 2 | jerry | 0 | 1986-12-12 | 199.99 | 安保部 | | 3 | peter | 1 | 1988-12-12 | 2299.99 | 高管部 | | 4 | prog | 1 | 1990-04-12 | 8000.00 | 教學部 | +----+-------+--------+------------+---------+-----------+
2.修改表記錄
MariaDB [s18]> update emp set salary=300 where name='peter'; MariaDB [s18]> select * from emp; +----+-------+--------+------------+---------+-----------+ | id | name | gender | bir | salary | dep | +----+-------+--------+------------+---------+-----------+ | 1 | tom | 1 | 1989-12-12 | 99.99 | 安保部 | | 2 | jerry | 0 | 1986-12-12 | 199.99 | 安保部 | | 3 | peter | 1 | 1988-12-12 | 300.00 | 高管部 | | 4 | prog | 1 | 1990-04-12 | 8000.00 | 教學部 | +----+-------+--------+------------+---------+-----------+
3.刪除表記錄
MariaDB [s18]> delete from emp where id=2; MariaDB [s18]> select * from emp; +----+-------+--------+------------+---------+-----------+ | id | name | gender | bir | salary | dep | +----+-------+--------+------------+---------+-----------+ | 1 | tom | 1 | 1989-12-12 | 99.99 | 安保部 | | 3 | peter | 1 | 1988-12-12 | 300.00 | 高管部 | | 4 | prog | 1 | 1990-04-12 | 8000.00 | 教學部 | +----+-------+--------+------------+---------+-----------+
#清空表記錄
1.delete from table_name
2.truncate table_name --> 適用於數據量很是大的表
MariaDB [s18]> truncate emp; MariaDB [s18]> select * from emp; Empty set (0.00 sec)
4.查詢表記錄
MariaDB [s18]> drop table emp; MariaDB [s18]> create table emp( -> id int primary key auto_increment, -> name varchar(20), -> gender enum('male','female','other'), -> age tinyint, -> dep varchar(20), -> city varchar(20), -> salary double(7,2)); Query OK, 0 rows affected (0.00 sec) MariaDB [s18]> insert into emp (name,gender,age,dep,city,salary) values -> ('tom','male',24,'教學部','河北省',8000), -> ('jerry','male',34,'保安部','山東省',8000), -> ('peter','male',28,'保安部','山東省',10000), -> ('景麗陽','female',22,'教學部','北京',9000), -> ('張三','male',24,'教學部','河北省',6000), -> ('李四','male',32,'保安部','北京',12000), -> ('王五','male',38,'教學部','河北省',7000), -> ('趙六','male',28,'保安部','河北省',9000), -> ('豬七','female',24,'保潔部','北京',9000); Query OK, 9 rows affected (0.01 sec) MariaDB [s18]> select * from emp; +----+-----------+--------+------+-----------+-----------+----------+ | id | name | gender | age | dep | city | salary | +----+-----------+--------+------+-----------+-----------+----------+ | 1 | tom | male | 24 | 教學部 | 河北省 | 8000.00 | | 2 | jerry | male | 34 | 保安部 | 山東省 | 8000.00 | | 3 | peter | male | 28 | 保安部 | 山東省 | 10000.00 | | 4 | 景麗陽 | female | 22 | 教學部 | 北京 | 9000.00 | | 5 | 張三 | male | 24 | 教學部 | 河北省 | 6000.00 | | 6 | 李四 | male | 32 | 保安部 | 北京 | 12000.00 | | 7 | 王五 | male | 38 | 教學部 | 河北省 | 7000.00 | | 8 | 趙六 | male | 28 | 保安部 | 河北省 | 9000.00 | | 9 | 豬七 | female | 24 | 保潔部 | 北京 | 9000.00 | +----+-----------+--------+------+-----------+-----------+----------+ MariaDB [s18]> select name,salary from emp; +-----------+----------+ | name | salary | +-----------+----------+ | tom | 8000.00 | | jerry | 8000.00 | | peter | 10000.00 | | 景麗陽 | 9000.00 | | 張三 | 6000.00 | | 李四 | 12000.00 | | 王五 | 7000.00 | | 趙六 | 9000.00 | | 豬七 | 9000.00 | +-----------+----------+ MariaDB [s18]> select name as 姓名,salary as 薪水 from emp; # as 能夠省略 MariaDB [s18]> select name 姓名,salary 薪水 from emp; +-----------+----------+ | 姓名 | 薪水 | +-----------+----------+ | tom | 8000.00 | | jerry | 8000.00 | | peter | 10000.00 | | 景麗陽 | 9000.00 | | 張三 | 6000.00 | | 李四 | 12000.00 | | 王五 | 7000.00 | | 趙六 | 9000.00 | | 豬七 | 9000.00 | +-----------+----------+ MariaDB [s18]> select name from emp where age>20; +-----------+ | name | +-----------+ | tom | | jerry | | peter | | 景麗陽 | | 張三 | | 李四 | | 王五 | | 趙六 | | 豬七 | +-----------+ MariaDB [s18]> select name,age from emp where age>20; +-----------+------+ | name | age | +-----------+------+ | tom | 24 | | jerry | 34 | | peter | 28 | | 景麗陽 | 22 | | 張三 | 24 | | 李四 | 32 | | 王五 | 38 | | 趙六 | 28 | | 豬七 | 24 | +-----------+------+ MariaDB [s18]> select name,age from emp where age between 20 and 30; +-----------+------+ | name | age | +-----------+------+ | tom | 24 | | peter | 28 | | 景麗陽 | 22 | | 張三 | 24 | | 趙六 | 28 | | 豬七 | 24 | +-----------+------+ MariaDB [s18]> select name,age from emp where name like '張%'; +--------+------+ | name | age | +--------+------+ | 張三 | 24 | +--------+------+ MariaDB [s18]> insert emp set name='張無忌'; MariaDB [s18]> select name,age from emp where name like '張%'; +-----------+------+ | name | age | +-----------+------+ | 張三 | 24 | | 張無忌 | NULL | +-----------+------+ 一個_表明一個符號 MariaDB [s18]> select name,age from emp where name like '張_'; +--------+------+ | name | age | +--------+------+ | 張三 | 24 | +--------+------+ 1 row in set (0.00 sec) MariaDB [s18]> select name,age from emp where name like '張__'; +-----------+------+ | name | age | +-----------+------+ | 張無忌 | NULL | +-----------+------+ 1 row in set (0.00 sec) MariaDB [s18]> select name,age from emp where gender='male' and age>25; +--------+------+ | name | age | +--------+------+ | jerry | 34 | | peter | 28 | | 李四 | 32 | | 王五 | 38 | | 趙六 | 28 | +--------+------+
分組查詢group by
MariaDB [s18]> delete from emp where name='張無忌'; MariaDB [s18]> select * from emp group by dep; +----+--------+--------+------+-----------+-----------+---------+ | id | name | gender | age | dep | city | salary | +----+--------+--------+------+-----------+-----------+---------+ | 2 | jerry | male | 34 | 保安部 | 山東省 | 8000.00 | | 9 | 豬七 | female | 24 | 保潔部 | 北京 | 9000.00 | | 1 | tom | male | 24 | 教學部 | 河北省 | 8000.00 | +----+--------+--------+------+-----------+-----------+---------+ 此表是顯示的每一個部門的第一我的的信息,沒有用處 MariaDB [s18]> select dep from emp group by dep; +-----------+ | dep | +-----------+ | 保安部 | | 保潔部 | | 教學部 | +-----------+ MariaDB [s18]> select count(name) from emp group by dep; +-------------+ | count(name) | +-------------+ | 4 | | 1 | | 4 | +-------------+ MariaDB [s18]> select count(*) from emp group by dep; # 均可以 使用聚合函數查看每一個部門的總的人數 MariaDB [s18]> select dep,count(*) from emp group by dep; +-----------+----------+ | dep | count(*) | +-----------+----------+ | 保安部 | 4 | | 保潔部 | 1 | | 教學部 | 4 | +-----------+----------+ 每一個部門具體的人數 MariaDB [s18]> select dep,max(salary) from emp group by dep; +-----------+-------------+ | dep | max(salary) | +-----------+-------------+ | 保安部 | 12000.00 | | 保潔部 | 9000.00 | | 教學部 | 9000.00 | +-----------+-------------+ 查看每一個部門的最高工資 MariaDB [s18]> select gender,count(*) from emp group by gender; +--------+----------+ | gender | count(*) | +--------+----------+ | male | 7 | | female | 2 | +--------+----------+ 統計男生和女生的人數 MariaDB [s18]> select city,avg(salary) from emp group by city; +-----------+--------------+ | city | avg(salary) | +-----------+--------------+ | 北京 | 10000.000000 | | 山東省 | 9000.000000 | | 河北省 | 7500.000000 | +-----------+--------------+ 統計每一個省的員工的平均工資 MariaDB [s18]> select city,avg(salary) from emp group by city having avg(salary) > 8000; +-----------+--------------+ | city | avg(salary) | +-----------+--------------+ | 北京 | 10000.000000 | | 山東省 | 9000.000000 | +-----------+--------------+ 查詢平均工資大於8000的省份
查詢條件順序
where
group by
having ---> 對平均分組後的內容進行再過濾
order by
limit
MariaDB [s18]> select count(*) from emp; +----------+ | count(*) | +----------+ | 9 | +----------+ 查詢公司全部員工的人數 MariaDB [s18]> select avg(salary) from emp; +-------------+ | avg(salary) | +-------------+ | 8666.666667 | +-------------+ 求整個公司的平均薪水 MariaDB [s18]> select * from emp order by salary; +----+-----------+--------+------+-----------+-----------+----------+ | id | name | gender | age | dep | city | salary | +----+-----------+--------+------+-----------+-----------+----------+ | 5 | 張三 | male | 24 | 教學部 | 河北省 | 6000.00 | | 7 | 王五 | male | 38 | 教學部 | 河北省 | 7000.00 | | 1 | tom | male | 24 | 教學部 | 河北省 | 8000.00 | | 2 | jerry | male | 34 | 保安部 | 山東省 | 8000.00 | | 4 | 景麗陽 | female | 22 | 教學部 | 北京 | 9000.00 | | 8 | 趙六 | male | 28 | 保安部 | 河北省 | 9000.00 | | 9 | 豬七 | female | 24 | 保潔部 | 北京 | 9000.00 | | 3 | peter | male | 28 | 保安部 | 山東省 | 10000.00 | | 6 | 李四 | male | 32 | 保安部 | 北京 | 12000.00 | +----+-----------+--------+------+-----------+-----------+----------+ MariaDB [s18]> select * from emp limit 2,4; +----+-----------+--------+------+-----------+-----------+----------+ | id | name | gender | age | dep | city | salary | +----+-----------+--------+------+-----------+-----------+----------+ | 3 | peter | male | 28 | 保安部 | 山東省 | 10000.00 | | 4 | 景麗陽 | female | 22 | 教學部 | 北京 | 9000.00 | | 5 | 張三 | male | 24 | 教學部 | 河北省 | 6000.00 | | 6 | 李四 | male | 32 | 保安部 | 北京 | 12000.00 | +----+-----------+--------+------+-----------+-----------+----------+ 從第2條下面開始取,往下取4條 MariaDB [s18]> select dep,group_concat(name) from emp group by dep; +-----------+-----------------------------+ | dep | group_concat(name) | +-----------+-----------------------------+ | 保安部 | jerry,peter,李四,趙六 | | 保潔部 | 豬七 | | 教學部 | tom,景麗陽,張三,王五 | +-----------+-----------------------------+ 查看每一個組的員工姓名
多表查詢
MariaDB [s18]> create table emp( -> id int primary key auto_increment, -> name varchar(20), -> salary double(7,2), -> dep_id int); Query OK, 0 rows affected (0.00 sec) MariaDB [s18]> insert emp (name,salary,dep_id) values ('張三',8000,2), -> ('李四',12000,1), -> ('王五',5000,2), -> ('趙六',8000,3), -> ('豬七',9000,1), -> ('周八',7000,4), -> ('蔡九',7000,2); Query OK, 7 rows affected (0.01 sec) MariaDB [s18]> create table dep( -> id int primary key auto_increment, -> name varchar(20) -> ); Query OK, 0 rows affected (0.01 sec) MariaDB [s18]> insert into dep (name) values ('教學部'), -> ('銷售部'), -> ('人事部'); Query OK, 3 rows affected (0.01 sec) MariaDB [s18]> select * from emp; +----+--------+----------+--------+ | id | name | salary | dep_id | +----+--------+----------+--------+ | 1 | 張三 | 8000.00 | 2 | | 2 | 李四 | 12000.00 | 1 | | 3 | 王五 | 5000.00 | 2 | | 4 | 趙六 | 8000.00 | 3 | | 5 | 豬七 | 9000.00 | 1 | | 6 | 周八 | 7000.00 | 4 | | 7 | 蔡九 | 7000.00 | 2 | +----+--------+----------+--------+ MariaDB [s18]> select * from dep; +----+-----------+ | id | name | +----+-----------+ | 1 | 教學部 | | 2 | 銷售部 | | 3 | 人事部 | +----+-----------+
MariaDB [s18]> select * from emp,dep where emp.dep_id=dep.id; +----+--------+----------+--------+----+-----------+ | id | name | salary | dep_id | id | name | +----+--------+----------+--------+----+-----------+ | 1 | 張三 | 8000.00 | 2 | 2 | 銷售部 | | 2 | 李四 | 12000.00 | 1 | 1 | 教學部 | | 3 | 王五 | 5000.00 | 2 | 2 | 銷售部 | | 4 | 趙六 | 8000.00 | 3 | 3 | 人事部 | | 5 | 豬七 | 9000.00 | 1 | 1 | 教學部 | | 7 | 蔡九 | 7000.00 | 2 | 2 | 銷售部 | +----+--------+----------+--------+----+-----------+ MariaDB [s18]> select dep.name,emp.name from emp,dep where emp.dep_id=dep.id and emp.name='張三'; +-----------+--------+ | name | name | +-----------+--------+ | 銷售部 | 張三 | +-----------+--------+
查詢張三所在部門的名稱
(此種方法並非之後常常使用的方法,有專門的語法 inner join--內鏈接查詢)
MariaDB [s18]> select * from emp inner join dep on emp.dep_id=dep.id; +----+--------+----------+--------+----+-----------+ | id | name | salary | dep_id | id | name | +----+--------+----------+--------+----+-----------+ | 1 | 張三 | 8000.00 | 2 | 2 | 銷售部 | | 2 | 李四 | 12000.00 | 1 | 1 | 教學部 | | 3 | 王五 | 5000.00 | 2 | 2 | 銷售部 | | 4 | 趙六 | 8000.00 | 3 | 3 | 人事部 | | 5 | 豬七 | 9000.00 | 1 | 1 | 教學部 | | 7 | 蔡九 | 7000.00 | 2 | 2 | 銷售部 | +----+--------+----------+--------+----+-----------+ MariaDB [s18]> select * from emp inner join dep on emp.dep_id=dep.id where emp.name='張三'; +----+--------+---------+--------+----+-----------+ | id | name | salary | dep_id | id | name | +----+--------+---------+--------+----+-----------+ | 1 | 張三 | 8000.00 | 2 | 2 | 銷售部 | +----+--------+---------+--------+----+-----------+
左外鏈接查詢left join(以左邊的表爲準)
MariaDB [s18]> select * from emp left join dep on emp.dep_id=dep.id; +----+--------+----------+--------+------+-----------+ | id | name | salary | dep_id | id | name | +----+--------+----------+--------+------+-----------+ | 1 | 張三 | 8000.00 | 2 | 2 | 銷售部 | | 2 | 李四 | 12000.00 | 1 | 1 | 教學部 | | 3 | 王五 | 5000.00 | 2 | 2 | 銷售部 | | 4 | 趙六 | 8000.00 | 3 | 3 | 人事部 | | 5 | 豬七 | 9000.00 | 1 | 1 | 教學部 | | 6 | 周八 | 7000.00 | 4 | NULL | NULL | | 7 | 蔡九 | 7000.00 | 2 | 2 | 銷售部 | +----+--------+----------+--------+------+-----------+ 比起inner join,多了 | 6 | 周八 | 7000.00 | 4 | NULL | NULL |
右外鏈接查詢right join(以右邊的表爲準)
MariaDB [s18]> select * from emp right join dep on emp.dep_id=dep.id; +------+--------+----------+--------+----+-----------+ | id | name | salary | dep_id | id | name | +------+--------+----------+--------+----+-----------+ | 1 | 張三 | 8000.00 | 2 | 2 | 銷售部 | | 2 | 李四 | 12000.00 | 1 | 1 | 教學部 | | 3 | 王五 | 5000.00 | 2 | 2 | 銷售部 | | 4 | 趙六 | 8000.00 | 3 | 3 | 人事部 | | 5 | 豬七 | 9000.00 | 1 | 1 | 教學部 | | 7 | 蔡九 | 7000.00 | 2 | 2 | 銷售部 | +------+--------+----------+--------+----+-----------+
完整性約束
1.惟一約束--unique
惟一約束能夠有多個但索引列的值必須惟一,索引列的值容許有空值
MariaDB [s18]> create table t1 (name varchar(20) unique); MariaDB [s18]> insert t1 values ('tom'); Query OK, 1 row affected (0.00 sec) MariaDB [s18]> insert t1 values ('tom'); ERROR 1062 (23000): Duplicate entry 'tom' for key 'name' 建立惟一約束後就不能插入相同的內容了
MariaDB [s18]> alter table t1 drop index name; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [s18]> insert t1 values ('tom'); Query OK, 1 row affected (0.01 sec) MariaDB [s18]> insert t1 values ('tom'); Query OK, 1 row affected (0.01 sec) 刪除惟一索引後就能夠插入重複數據了
MariaDB [s18]> alter table t1 add age int; Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 MariaDB [s18]> alter table t1 add constraint UK_t1_age unique (age); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
2.自增約束
MySQL每張表只能有1個自動增加字段,這個自動增加字段一般做爲主鍵,也能夠用做
非主鍵使用,可是將自動增加字段看成非主鍵使用時必須爲其添加惟一索引,不然系統報錯
MariaDB [s18]> create table t2 ( -> id int not null, -> name varchar(20), -> age int auto_increment); ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key 改成如下: MariaDB [s18]> create table t2 ( -> id int not null, -> name varchar(20), -> age int unique auto_increment); Query OK, 0 rows affected (0.01 sec)
3.主鍵約束
非空且惟一
a.一張表不必定有主鍵
b.一張表最多隻能有一個主鍵
c.表中若是沒有設置主鍵,默認設置not null的字段爲主鍵,此外,表中若是有多個not null的字段爲主鍵,則
按順序將第一個設置爲not null的字段設爲主鍵
d.主鍵必定是非空且惟一,但非空且惟一的字段不必定是主鍵
e.主鍵類型不必定必須是整型
MariaDB [s18]> create table t3(id int not null unique,name varchar(20)); Query OK, 0 rows affected (0.01 sec) MariaDB [s18]> desc t3; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
添加主鍵
alter table table_name add primary key(字段名稱,...)
刪除主鍵
alter table table_name drop primary key;
4.外鍵約束
表與表的關係是兩張表的關係
一對多
關聯字段要設在在數據多的那張表
主表:沒有關聯字段
子表:有關聯字段
多對多
只能經過第三張表來創建關係
一對一
關聯字段能夠設在任何一張表上
將關聯字段設置爲unique
複合主鍵
主鍵含有一個以上的字段
若是一列不能惟一區分一張表裏的記錄,能夠用多個組合起來達到區分表記錄的惟一性
MariaDB [s18]> create table sc ( -> studentid int, -> courseid int, -> score int, -> primary key (studentid,courseid) -> ); 修改 alter table table_name add primary key (字段1,字段2...);