mysql创建表和数据库
創(chuàng)建數(shù)據(jù)庫(kù),創(chuàng)建數(shù)據(jù)庫(kù)表,例子。MySQL語(yǔ)句
1.創(chuàng)建數(shù)據(jù)庫(kù): ? ?創(chuàng)建的代碼:create ?數(shù)據(jù)庫(kù)的代碼:database ? 數(shù)據(jù)庫(kù)表名:隨便起,只要自己記住就行。test
create database test;?
?
2.刪除數(shù)據(jù)庫(kù): 刪除的代碼:drop ?數(shù)據(jù)庫(kù)代碼:database ?要?jiǎng)h除哪一個(gè)數(shù)據(jù)庫(kù):數(shù)據(jù)庫(kù)名:test
?
drop database test;?
?
3.創(chuàng)建表: ? 數(shù)據(jù)庫(kù)建好后該往里創(chuàng)建表了;例下 ?創(chuàng)建: create ? 表的代碼: table ? 表名:隨便取 ? ceshi
create table class (code varchar(20) primary key,name varchar(20) not null ); create table ceshi (ids int auto_increment primary key,uid varchar(20),name varchar(20),class varchar(20),foreign key (class) references class(code) );?
注:自增長(zhǎng)代碼代表:auto_increment
主建的代碼代表:primary key
外鍵的代碼代表公式:foreign key (列名) ?references ?主表名 (列名)
? ? ? fornign key+(列名) ?代表給哪一個(gè)加外鍵 references 要引用哪個(gè)表里的列
? ? ? 是否為空: 不為空的代碼:not null
?
4.刪除: ? ? ?刪除代碼的代表:drop ?刪除的是表: table ?要?jiǎng)h的那個(gè)表名:ceshi
drop table ceshi;?
?
?
?
代碼寫(xiě)創(chuàng)建數(shù)據(jù)庫(kù)是注意:
1.類型包含長(zhǎng)度的,在類型后面加(括號(hào)),括號(hào)里面寫(xiě)長(zhǎng)度
2.上一列寫(xiě)完加逗號(hào)
3.最后一列不要寫(xiě)逗號(hào)
4.在每一條SQL語(yǔ)句寫(xiě)完之后要加分號(hào);
5.如果有外鍵關(guān)系,先創(chuàng)建主表
?
例子:
創(chuàng)建表: create table class (code varchar(20) primary key,name varchar(20) ); create table student (code varchar(20) primary key,name varchar(20),sex bit,age int,class varchar(20),foreign key (class) references class(code) ); create table kecheng (code varchar(20) primary key,name varchar(20) ); create table teacher (code varchar(20) primary key,name varchar(20) ); create table chengji ( ids int auto_increment primary key,scode varchar(20),kcode varchar(20),degree float,foreign key (scode) references student(code),foreign key (kcode) references kecheng(code) ); create table tkecheng (ids int auto_increment primary key,tcode varchar(20),kcode varchar(20),foreign key (kcode) references kecheng(code),foreign key (tcode) references teacher(code) );轉(zhuǎn)載于:https://www.cnblogs.com/Generalwake/p/9230650.html
總結(jié)
以上是生活随笔為你收集整理的mysql创建表和数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JVM----Java内存区域
- 下一篇: 剑指offer 面试3题