MySql 有關資料庫 表定義及約束的基本語法

2021-08-20 11:38:17 字數 4916 閱讀 8563

create  database demo ;/*建立資料庫*/

show databases;/*顯示資料庫*/

drop database if exists demo;/*刪除資料庫*/

use sqls;/*使用資料庫*/

show tables;/*顯示所有的表*/

/*建立資料庫*/

create table students(

stid int ,

stname varchar(20),

stbirth date

);desc students;/*顯示表結構*/

show create table students;/*檢視定義語句*/

alter table students rename student;/*修改表名稱*/

alter table student add stuscore float(5,1);/*追加列在末尾*/

alter table student drop stuscore;/*刪除指定列*/

/*在指定列後面追加列*/

alter table student add stuscore float (5,1) after stid;

/*在第一列新增列*/

alter table student add stuscore float (5,1) first;

alter table student modify stuscore int;/*修改列型別*/

/*修改列名稱和型別*/

alter table student change stuscore score float(5,1);

create table students select * from student;/*快速複製表*/

create table students select * from student where 1=1;/*快速複製表*/

/*只複製表結構*/

create table students select * from student where 2=1;

create table students like student;/*只複製表結構*/

/*---------------實體完整性--------------------*/

/*primary key --- 主鍵

1.不能為null,不能重複

2.表可以不使用主鍵約束,如果使用只能新增一次

3.新增一次主鍵可以是乙個列,或者 同時多個列

*/drop table if exists student;

create table student (

stid int primary key,/* 主鍵*/

stname varchar(10),

stscore float(5,1)

);create table student (

stid int ,

stname varchar(10),

stscore float(5,1),

primary key (stid,stname)/* 宣告兩個屬性共同組成主鍵*/

);

/*auto_increment --- 自增長

1.只能作用於數值列

2.預設從1開始,每次自增1

unique key --- 唯一約束

1.可以為null ,但不能重複

*/create table student(

stid int primary key auto_increment,/* 自增*/

stname varchar(10),

stscore float(5,1),

stphone char(11) unique key/* 唯一鍵,值不可重複*/

)auto_increment=1000;/* 從1000開始自增,增量為1*/

drop table if exists student;

create table student(

stid int,

stname varchar(20),

stphone char(11)

);alter table student add constraint primary key(stid);/* 追加主鍵*/

alter table student drop primary key ;/* 刪除主鍵*/

/* 追加唯一主鍵*/

alter table student add constraint uq_phone unique key(stphone);

alter table student drop index uq_phone;/* 刪除唯一鍵*/

/*--------------域完整性(domain)--------------------*/

/* null --- 可以為空,預設值

not null --- 該列資料不允許為null

default --- 預設值

*/drop table if exists students;

create table students (

stid int not null,

stname varchar(20),

staddress varchar(200) default'位址不詳'

);alter table students alter staddress drop default;/*刪除預設*/

/*新增預設*/

alter table students alter staddress set default '位址不詳';

/*--------------------參照完整性 ---------------*/

drop table if exists students;

drop table if exists classes;

/*寫法一*/

create table students(

stid int primary key,

stname varchar(10)

)engine = innodb;

create table classes(

cid int primary key,

stid int,

foreign key(stid) references students(stid)

)engine = innodb;

insert into students values (111,'張三');

insert into classes values (222,111);

/*寫法二*/

create table students (

stid int primary key,

stname varchar(10)

)engine = innodb;

create table classes(

cid int primary key,

stid int

)engine = innodb;

/*建立外來鍵約束*/

alter table classes add constraint fk_stid

foreign key (stid) references students (stid);

insert into students values (333,'張三');

insert into classes values (444,333);

/*刪除外來鍵約束*/

alter table classes drop foreign key fk_stid;

/*---------------------級聯刪除------------------*/

drop table if exists students;

drop table if exists classes;

create table students(

stid int primary key,

stname varchar(10)

)engine = innodb;

create table classes(

cid int primary key,

stid int,

foreign key (stid) references students(stid) on delete cascade

)engine = innodb;

insert into students values(1111,'張三');

insert into classes values(1,1111);

delete from students where stid=1111;

/*------------------級聯更新-----------------*/

drop table if exists classes;

drop table if exists students;

create table students(

stid int primary key,

stnaem varchar(10)

)engine = innodb;

create table classes(

cid int primary key,

stid int,

foreign key(stid) references students (stid) on update cascade

)engine = innodb;

insert into students values(1111,'張三');

insert into classes values(1,1111)

update students set stid=2222;

下列有關mysql資料庫 有關MySQL資料庫命令

phpstudy使用最終端開啟資料庫 第一次開啟預設的密碼是 root。進入後對資料可以進行增刪查改。show databases 是檢視資料庫的指令 注意 分號是資料庫的結束符,沒有加分號即使按回車,也代表這個命令沒有結束。create database 資料庫名 是建立資料庫 drop data...

mysql資料庫及表的操作

注 windows與linux的資料庫命令一致 注 mysql自帶的指令不區分大小寫 啟動mysql服務 在windows中啟動及關閉服務 啟動 net start mysql 關閉 net stop mysql 在linux中啟動服務,預設開啟 啟動 service mysql start 關閉 ...

mysql資料庫 匯入表及資料步驟

1.安裝mysql客戶端,我用的是mysql workbench 2.安裝mysql服務端,我用的是解壓版的 mysql 5.6.27 winx64 3.開啟解壓的 mysql 5.6.27 winx64的bin檔案中的mysqld.exe執行 會一閃而過 在執行mysql.exe 4.輸入dos命...