01 創(chuàng)建數(shù)據(jù)庫
(1)創(chuàng)建數(shù)據(jù)庫istester
????????????createdatabase istester;
(2)查看數(shù)據(jù)庫創(chuàng)建是否成功
????????????showdatabases;
02 創(chuàng)建表
(1)創(chuàng)建表istester
????? ????a、use istester
? ? ????? b砚亭、createtable istester(
??????????????? id INT(10) PRIMARY KEYAUTO_INCREMENT,
??????????? ????uname VARCHAR(20) NOT NULL,
??????????? ????sex VARCHAR(4),
??????????????? birth YEAR,
??????????? ????department VARCHAR(20),
??????????? ????address VARCHAR(50),
??????????????? idoxu VARCHAR(20));
(2)創(chuàng)建表idoxu
? ? ? ? ? ?a灯变、useistester
????????????b殴玛、createtable idoxu(
? ? ? ? ? ? ? ? ? id INT(10)NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT,
????????????????????stu_idINT(10) NOT NULL,
????? ????????????? c_nameVARCHAR(20),
????? ????????????? istesterVARCHAR(50),
????? ????? ????????gradeINT(10));
03 復(fù)制表
( 1)復(fù)制表結(jié)構(gòu)并創(chuàng)建新表
????復(fù)制istester表結(jié)構(gòu),創(chuàng)建新表istester2(兩種方式)
????createtable istester2 like istester;
????createtable istester3 as select * from istester where 2=1;
(2)復(fù)制表結(jié)構(gòu)和數(shù)據(jù)并創(chuàng)建新表
????復(fù)制idoxu表結(jié)構(gòu)和數(shù)據(jù)添祸,創(chuàng)建新表idoxu2
????createtable idoxu2 as select * from idoxu
(3)復(fù)制表結(jié)構(gòu)以及個別字段且創(chuàng)建新表
????? 復(fù)制idoxu表結(jié)構(gòu)的id滚粟,stu_id,istester三個字段刃泌,創(chuàng)建新表idoxu3
? ? ? ?create????table idoxu3 as select id,stu_id,istester from idoxu where 1<>1;
04 刪除表
(1)刪除單個表
????刪除表istester2
????drop table istester2;
(2)刪除多個表
????同時刪除表idoxu2和idoxu3
????droptable idoxu2,idoxu3;
(3)查看表
???show????tables凡壤;
?05修改表
(1)添加字段
????????修改istester表,添加一個字段istester6(字符類型VARCHAR耙替,長度100亚侠,不允許為空)
????? ALTER table istester add column istester6VARCHAR(100)NOT NULL;
(2)查看字段
????查看字段istester6是否添加成功
????? desc istester
(3)刪除字段
????修改istester表,刪除字段istester6
????ALTERtable istester drop istester6
(4)查看字段
????確認(rèn)字段istester6是否刪除成功
????descistester
06 插入數(shù)據(jù)
?(1)向表istester插入單條數(shù)據(jù)數(shù)據(jù)
(id = 1俗扇,uname=idoxu, idoxu = 2020)
insertinto istester(id,uname,idoxu)values(1,"idoxu",2020);
(id = 2硝烂,uname=idoxu2, idoxu = 2020)
insertinto istester(id,uname,idoxu)values(2,"idoxu2",2020);
(2)插入多條數(shù)據(jù)
?????? (id =11,uname=idoxu3, sex=1? ??
??????????? id = 12铜幽,uname=idoxu4,sex=2
??????????? id = 13滞谢,uname=idoxu5,sex=1
??????????? id = 14,uname=idoxu6,sex=2)
insertinto istester (id,uname,sex) values?
???(11,"idoxu3",1),(12,"idoxu4",2),(13,"idoxu5",1),(14,"idoxu6",2);
???? 向表idoxu插入數(shù)據(jù)
? ? ? ? ?(id = 4除抛,stu_id=11, c_name = idoxu, grade = 90
??????????? id = 5狮杨,stu_id=12, c_name =lin, grade = 100
??????????? id = 6,stu_id=33, c_name =istester, grade = 20)
insertinto idoxu(id,stu_id,c_name,grade)
values(4,11,"idoxu",90),(5,12,"lin",100),(6,33,"istester",20);
07-select
(1)查看istester表 id = 1的內(nèi)容
????? select * from istester where id =1;
(2)查詢idoxu表grade=100的內(nèi)退
select* from idoxu where grade =100;
(3)查找idoxu表镶殷,名稱(c_name)包含“i”的數(shù)據(jù)
????? select * from idoxu where c_name like'%i%';
(4)查找istester表禾酱,id包含“1”的數(shù)據(jù)
?????? select * from????istester where id like '%1%';
(5)查找istester表,id包含“1”的數(shù)據(jù)绘趋,按id降序
?????? select * fromistester where id like '%1%' order by?iddesc;
(6)查找istester表,id包含“1”的數(shù)據(jù)颗管,取id最大的三個
?????? select * fromistester where id like '%1%'order by id desc limit 3;