数据库创建
蕪湖
1.創建數據庫的命令
create database 數據庫名稱?
使用的:use 數據庫名稱
創建表
?create table test(id int(11),name varchar(50));
desc table名(可以顯示表)
(命名規則,字母數字下劃線組成的字符串,開頭不要是數字)
舉例:create table sc(son char(11))
創建? 表 ?表名(屬性名 類型(長度))? (基本是以這個格式來創建)
2.查看數據庫 (要用復數)
show databases
3.查看字符串
show variables like 'character%'
4.查看端口號
show variables like 'port'
5.查看數據存儲路徑
show variables like 'datadir'
創建
course_name
過程:
使用數據庫demo1? ? ? use demo1?
創建test表? ? ? ? ? ? ? ? ? create table test(id int(11),name varchar(50));?
顯示test表? ? ? ? ? ? ? ? ? desc test;?
創建student表????????????create table student(sno char(11) primary key,sname varchar(20) not null);
顯示student表????????????desc student;
創建course表? ? ? ? ? ? ?create table course
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(?
課程號?(主鍵)? ? ? ? ?cno varchar(20) primary key,①
表名(不能為空)? ? ? course_name varchar(50) not null,
先修課程號? ? ? ? ? ? ? ? ?cpno varchar(20),
成績(可以有小數)? ?course_credit decimal(4,1)②
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??);
顯示course表? ? ? ? ? ? ??desc course;
顯示所有的表? ? ? ? ? ? ??show tables;
①主鍵約束學號son,char? 主鍵不能為空,不能重復(not null)
②decimal (總長度(4),小數位數(1))~~位數為4,小數點后為1位
//更改表名的方法
方法一:? ? ? alter table test? ? ? ? ? ? 修改表結構
? ? ? ? ? ? ? ? ? ??rename to test2? ? ? ? ? 重命名為test2
? ? ? ? ? ? ? ? ? ? ;
方法二:(再改回來)??rename table 原表名?to?新表名 ;
? ? ? ? ? ? ? ? ? ?rename table test2 to test;
兩種方式都可以
//增加列
?alter table student? ? ? ? ? ? ? ? ? ? ? ? ?修改表結構
?add ssex char(2) not null;? ? ? ? ? ? ?增加列性別
?desc student;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 顯示student表
?alter table student? ? ? ? ? ? ? ? ? ? ? ? ?修改表結構
?alter ssex set default'男'? ? ? ? ? ? ?(增加一個值 男)default? 添加默認值
;
//把一個列的數據類型修改
alter table student? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
modify ssex enum('male','female') not null; ? ??
modify?只能修改字段屬性
desc student;
? ?
//添加主鍵? id
alter table test? ? ? ? ? ? ? ? ??
add primary key(id);?
//添加列? age? 且默認值為20??
alter table test
add age int(3) not null default 20;
新加表
create table sc(sno char(11),
cno varchar(20),
grade decimal(6.2));
添加外鍵
alter table sc
add foreign key(sno) references student(sno);
要和student做外鍵 ? ? ? ? ? ? ?參考 ?表。。。。。。
總結
- 上一篇: 利用DOSBox运行汇编超详细步骤
- 下一篇: c# mysql executenonq