資料庫MySQL編寫 資料庫MySQL的建立

2021-10-18 08:45:14 字數 1193 閱讀 2102

#如果存在資料庫school,則刪除。否則建立資料庫

drop database if exists `school`;

#建立資料庫

create database `school`;

use `school`;

#建立乙個學校係表:系號(主鍵,自增),系辦公地點,人數

drop table if exists `tb_dept`;

create table `tb_dept`

`id` int(11) not null auto_increment primary key,

`local` varchar(32) not null,

`dnum` int(4) not null

#建立乙個學生班級表:班級id(主鍵,自增),專業名,系名(外來鍵),人數

drop table if exists `tb_class`;

create table `tb_class`

`id` int(11) not null auto_increment primary key ,

`cname` varchar(32) not null,

`cdeptid` int(11) not null ,

`cnum` int(4) not null,

constraint `fk_stuid` foreign key(`cdeptid`) references `tb_dept`(`id`)

#建立乙個學生資訊表:學生id(自增,主鍵),姓名,年齡,性別,所屬班級id(外來鍵),宿舍區。

drop table if exists `tb_student`;

create table `tb_student`

`id` int(11) not null auto_increment primary key,

`sname` varchar(32) not null,

`age` int default 0,check(`age`>0 and `age`<=100),

`gender` boolean default 0,check(`gender`=0 or `gender`=1),

`classid` int(11) not null ,

constraint `fk_stuid1` foreign key(`classid`) references `tb_class`(`id`)

mysql資料庫效能資料 MYSQL資料庫效能優化

1.選取最適用的字段屬性 表中字段的寬度設得盡可能小 char 的上限為 255 位元組 固定占用空間 varchar 的上限 65535 位元組 實際占用空間 text 的上限為 65535。盡量把字段設定為 not null,執行查詢的時候,資料庫不用去比較 null 值。2.使用連線 join...

MySQL資料庫 資料庫管理

建立使用者,指定明文密碼 create user rose localhost identified by rosepwd 檢視使用者是否建立成功 select user,host from mysql.user 建立使用者,不設定密碼 create user rose01 localhost se...

資料庫 MySQL資料庫(一)

一 mysql資料庫系統 mysql資料庫系統就是用來對資料庫 資料的一些管理 二 資料庫系統 1.資料庫 就是用來儲存各種資料的 2.資料庫管理系統 就是用來管理各種資料庫的資料的乙個系統 三 常見的一些資料庫系統 mysql db2 oracle sql server maradb 四 資料庫 ...