{"id":15,"date":"2005-10-14T16:27:58","date_gmt":"2005-10-14T23:27:58","guid":{"rendered":"http:\/\/www.halcyonrepose.com\/?p=15"},"modified":"2008-06-06T22:15:10","modified_gmt":"2008-06-07T05:15:10","slug":"mysql-notes","status":"publish","type":"post","link":"http:\/\/www.halcyonrepose.com\/?p=15","title":{"rendered":"MySQL Notes"},"content":{"rendered":"<p>Connect to MySQL from the command line:<\/p>\n<p>   msql -u *username* -p<\/p>\n<p>Connect to the database (schema):<\/p>\n<p>   USE *database name*<\/p>\n<p>Check to see what database you  are currently set to:<\/p>\n<p>   SELECT DATABASE();<\/p>\n<p>NOTES  on SCFC Database<\/p>\n<p>CREATE TABLE users (<br \/>\nID INT PRIMARY KEY,<br \/>\nFirstName varchar(30) NOT NULL,<br \/>\nLastName varchar(30) NOT NULL,<br \/>\nEmail varchar(50) NOT NULL,<br \/>\nPhone varchar(20),<br \/>\nPassword varchar(20) NOT NULL,<br \/>\nCoachID INT Not NULL<br \/>\n);<\/p>\n<p>CREATE TABLE Lesson_Slots(<br \/>\nID int primary key,<br \/>\nInstructorID int,<br \/>\nStartTime datetime,<br \/>\nStudentID INT DEFAULT 0,<br \/>\nWeapon INT DEFAULT 0<br \/>\n);<\/p>\n<p>insert into users Values (1, &#8216;Jaime&#8217;, &#8216;Wood&#8217;, &#8216;jaime@southcoastfencing.com&#8217;, &#8216;(714) 836-9505&#8242;,&#8217;temp&#8217;,0);<\/p>\n<p>insert into Lesson_Slots values (1, 1, &#8216;2005-10-21 19:00:00&#8217;, 1, 1);<br \/>\ninsert into Lesson_Slots values (2, 1, &#8216;2005-10-21 19:20:00&#8217;, 1, 1);<br \/>\ninsert into Lesson_Slots values (3, 1, &#8216;2005-10-22 19:00:00&#8217;, 1, 1);<br \/>\ninsert into Lesson_Slots values (4, 1, &#8216;2005-10-22 19:20:00&#8242;, 1, 1);<\/p>\n<p>select date_format(StartTime,&#8217;%M %d, %Y &#8211; %a&#8217;) from Lesson_Slots;<\/p>\n<p>PERL Code;<br \/>\n#Initialize<br \/>\nuse DBI;<\/p>\n<p>my $dsn = &#8216;DBI:mysql:southcoa_scfc:localhost&#8217;;<br \/>\nmy $db_user_name = &#8216;southcoa_scfc&#8217;;<br \/>\nmy $db_password = &#8216;RealFencing&#8217;;<br \/>\nmy ($id, $password);<br \/>\nmy $dbh = DBI->connect($dsn, $db_user_name, $db_password);<\/p>\n<p># Query (using arguments<br \/>\n my $rs = $dbh->prepare(&#8216;SELECT firstname, lastname from users<br \/>\n                          WHERE upper(firstname) = upper(?)<br \/>\n                          and   upper(lastname) = upper(?)<br \/>\n                          and   password = ?<br \/>\n                          and   CoachID > -1&#8217;);<\/p>\n<p> $rs->execute($in_fname, $in_lname, $in_code);<br \/>\n my ($sFirstName, $sLastName) = $rs->fetchrow_array();<\/p>\n<p> $rs->finish();<\/p>\n<p># Number of rows returned<br \/>\n$rs->rows()<br \/>\n&#8212; Table to hold each individual class<br \/>\nDROP TABLE class_dates;<br \/>\nCREATE TABLE class_dates(<br \/>\nclass_id integer(5) not null primary key auto_increment,<br \/>\nsession_id integer(5) not null,<br \/>\nclass_date date not null,<br \/>\nclass_start time not null,<br \/>\nclass_end time not null,<br \/>\nclass_type integer(2) not null,<br \/>\nclass_title varchar(100) not null,<br \/>\norder_id integer(2) not null,<br \/>\ndeleted<br \/>\n);<br \/>\nALTER TABLE class_dates ADD INDEX(session_id);<\/p>\n<p>&#8212; Table to hold sessions (class groupings)<br \/>\nDROP TABLE class_session;<br \/>\nCREATE TABLE class_session(<br \/>\nsession_id integer(5) not null primary key auto_increment,<br \/>\nsession_title varchar(100) not null,<br \/>\nstart_date date not null,<br \/>\npost_date date not null<br \/>\n);<br \/>\nALTER TABLE class_session ADD INDEX(post_date);<\/p>\n<p>&#8212; Table to hold email bodies<br \/>\nDROP TABLE mail_body;<br \/>\nCREATE TABLE mail_body(<br \/>\nMail_ID integer(5) not null primary key auto_increment,<br \/>\nMail_Type varchar(255),<br \/>\nMail_Body text<br \/>\n);<\/p>\n<p>&#8212; Table to hold all the class templates<br \/>\nDROP TABLE class_template;<br \/>\nCREATE TABLE class_template(<br \/>\nTemplate_ID integer(5) not null primary key auto_increment,<br \/>\nClass_Title  varchar(100) not null,<br \/>\nClass_Type int(2) not null,<br \/>\nClass_Notes text,<br \/>\nClass_DayTime varchar(255),<br \/>\nClass_Length varchar(20),<br \/>\nClass_Cost varchar(10),<br \/>\nClass_Day int(1),<br \/>\nClass_Default int(1) default 0,<br \/>\nDeleted int(1) default 0<br \/>\n);<br \/>\nALTER TABLE class_template ADD INDEX(Template_ID);<\/p>\n<p>&#8212; Table to hold each individual who registers for class<br \/>\nDROP TABLE class_registration;<br \/>\nCREATE TABLE class_registration(<br \/>\nRegistration_ID integer(5) not null primary key auto_increment,<br \/>\nTemplate_ID integer(5) not null,<br \/>\nSession_ID integer(5) not null,<br \/>\nClass_Date date not null,<br \/>\nDeleted int(1) default 0<br \/>\n);<br \/>\nALTER TABLE class_registration ADD INDEX(Registration_ID);<\/p>\n<p>&#8212; User Table<br \/>\nDROP TABLE scfc_users;<br \/>\nCREATE TABLE scfc_users(<br \/>\nUser_ID integer(5) not null primary key auto_increment,<br \/>\nFirst_Name varchar(25),<br \/>\nLast_Name varchar(25),<br \/>\nEmail varchar(50),<br \/>\nAddress text,<br \/>\nPhone varchar(15),<br \/>\nRelease binary(1),<br \/>\nNotes text,<br \/>\nUsername varchar(25),<br \/>\nPassword varchar(32)<br \/>\n);<br \/>\nALTER TABLE scfc_users ADD INDEX(First_Name, Last_Name);<br \/>\nALTER TABLE scfc_users ADD INDEX(Username);<\/p>\n<p>&#8212; User Class Registration<br \/>\nDROP TABLE class_users;<br \/>\nCREATE TABLE class_users(<br \/>\nUser_ID integer(5) not null,<br \/>\nClass_ID integer(5) not null,<br \/>\nPaid binary(1),<br \/>\nComments text<br \/>\n);<br \/>\nALTER TABLE class_users ADD INDEX (Class_ID);<\/p>\n<p>&#8212; Instructor Table<br \/>\nDROP TABLE Instructor;<br \/>\nCREATE TABLE Instructor(<br \/>\nInstructor_ID         integer(5) not null primary key auto_increment,<br \/>\nInstructor_Name  varchar(30)<br \/>\n);<\/p>\n<p>&#8212; Private Lesson Templates<br \/>\nDROP TABLE Lesson_Template;<br \/>\nCREATE TABLE Lesson_Template(<br \/>\nTemplate_ID integer(5) not null primary key auto_increment,<br \/>\nInstructor_ID integer(5) not null,<br \/>\nTime time not null,<br \/>\nDay integer(2) not null,<br \/>\nStudent_ID integer(5) default NULL,<br \/>\nStatus integer(2) default 0<br \/>\n);<br \/>\nALTER TABLE Lesson_Template ADD INDEX (Instructor_ID);<br \/>\nALTER TABLE Lesson_Template ADD INDEX (Day);<\/p>\n<p>&#8212; Private Lesson Table<br \/>\nDROP TABLE Private_Lesson;<br \/>\nCREATE TABLE Private_Lesson(<br \/>\nLesson_ID integer(5) not null primary key auto_increment,<br \/>\nInstructor_ID integer(5) not null,<br \/>\nLesson_Date DateTime not null,<br \/>\nStudent_ID integer(5),<br \/>\nStatus integer(2) default 0<br \/>\n);<br \/>\nALTER TABLE Private_Lesson ADD INDEX (Instructor_ID);<br \/>\nALTER TABLE Private_Lesson ADD INDEX (Lesson_Date);<\/p>\n<p>&#8212; For use on the web page, gets the next session that has not already passed<br \/>\nselect min(post_date) from class_session where post_date > now();<\/p>\n<p>SQL Scraps<\/p>\n<p> select cd.class_id, ct.type_desc, date_format(cd.class_date, &#8216;%W&#8217;),<br \/>\n  date_format(cd.class_date,&#8217;%M %D&#8217;), date_format(cd.class_start,&#8217;%l:%i&#8217;),<br \/>\n  date_format(cd.class_end,&#8217;%l:%i %p&#8217;), cd.class_title<br \/>\nfrom class_dates cd, class_type ct<br \/>\nwhere cd.session_id = ?<br \/>\norder by ct.type_id, cd.order_id;<\/p>\n<p>select min(session_id) from class_session<br \/>\nwhere start_date < CURDATE()\n  and post_date > CURDATE();<\/p>\n<p>select session_id from class_session<br \/>\nwhere start_date = (SELECT max(start_date) from class_session);<\/p>\n<p>(my $Class_ID, $Class_Type, $Class_Date, $Class_Start, $Class_end, $Class_Title) = $rs->fetchrow_array();<\/p>\n<p>select date_format(class_end,&#8217;%l:%i %p&#8217;)<br \/>\nfrom class_dates;<\/p>\n<p>&#8212; Class List<br \/>\nSELECT cr.Registration_ID, ct.Class_Title, ct.Class_Notes, ct.Class_DayTime,<br \/>\n       cr.Class_Date, ct.Class_Length, ct.Class_Cost<br \/>\n  FROM class_template ct, class_registration cr<br \/>\n WHERE cr.Session_ID = 14<br \/>\n   AND cr.Template_ID = ct.Template_ID<br \/>\n   AND cr.Deleted = 0;<\/p>\n<p>DROP TABLE Whats_New;<br \/>\nCREATE TABLE Whats_New(<br \/>\nID int not null primary key auto_increment,<br \/>\nPostDate datetime,<br \/>\nPostSubject varchar(255),<br \/>\nPostBody text,<br \/>\nDeleted int(1) default 0,<br \/>\nVisible int(1) default 1,<br \/>\nDisplayOrder int(1) default 0<br \/>\n);<br \/>\nALTER TABLE Whats_New ADD INDEX(ID);<br \/>\nALTER TABLE Whats_New ADD INDEX(PostDate);<\/p>\n<p>DROP TABLE Image_Rotate;<br \/>\nCREATE TABLE Image_Rotate(<br \/>\nID int not null primary key auto_increment,<br \/>\nImage_Location varchar(100),<br \/>\nImage_Comment varchar(75),<br \/>\nDisplay_Order int(2) default 0,<br \/>\nDate_Created timestamp<br \/>\n);<br \/>\nALTER TABLE Image_Rotate ADD INDEX(ID);<\/p>\n<p>&#8211;Payment Table<br \/>\nDROP TABLE Payment;<br \/>\nCREATE TABLE Payment(<br \/>\nID int not null primary key auto_increment,<br \/>\nUser_ID integer(5) not null,<br \/>\nPayment_Amt float(4,2),<br \/>\nPay_Start datetime,<br \/>\nPay_End datetime,<br \/>\nPayment_Type integer(1),<br \/>\nPayment_Note TEXT,<br \/>\nPayment_Currency integer(1),<br \/>\nDate_Entered datetime,<br \/>\nEntered_By integer(5)<br \/>\n);<\/p>\n<p>ALTER TABLE Payment ADD INDEX(Pay_Start, Pay_End);<br \/>\nALTER TABLE Payment ADD INDEX(User_ID);<\/p>\n<p>&#8212; Credit Table<br \/>\nDROP TABLE Payment_Credit;<br \/>\nCREATE TABLE Payment_Credit(<br \/>\nID int not null primary key auto_increment,<br \/>\nUser_ID integer(5) not null,<br \/>\nCredit_Amt float(4,2),<br \/>\nCredit_Remain float(4,2),<br \/>\nCredit_Note TEXT,<br \/>\nDate_Entered datetime,<br \/>\nEntered_By integer(5)<br \/>\n);<\/p>\n<p>ALTER TABLE Payment_Credit ADD INDEX(User_ID);<\/p>\n<p>&#8212; Payment Types<br \/>\nDROP TABLE Payment_Type;<br \/>\nCREATE TABLE Payment_Type(<br \/>\nID int not null primary key auto_increment,<br \/>\nPayment_Type varchar(25)<br \/>\n);<\/p>\n<p>insert into Payment_Type values (1,&#8217;Dues&#8217;);<br \/>\ninsert into Payment_Type values (2,&#8217;Class&#8217;);<br \/>\ninsert into Payment_Type values (3,&#8217;Equipment&#8217;);<\/p>\n<p>&#8212; Payment Types<br \/>\nDROP TABLE Payment_Currency;<br \/>\nCREATE TABLE Payment_Currency(<br \/>\nID int not null primary key auto_increment,<br \/>\nCurrency varchar(25)<br \/>\n);<\/p>\n<p>insert into Payment_Currency values (1,&#8217;Cash&#8217;);<br \/>\ninsert into Payment_Currency values (2,&#8217;Check&#8217;);<br \/>\ninsert into Payment_Currency values (3,&#8217;Credit Card&#8217;);<br \/>\ninsert into Payment_Currency values (4,&#8217;SCFC Credit&#8217;);<\/p>\n<p>Links:<\/p>\n<p><a href='http:\/\/www.danchan.com\/feature\/2000\/10\/16\/mysql\/mysql3.htm'>Tutorial<\/a><br \/>\n<a href='http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/date-and-time-functions.html'>Date and Time Functions <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Connect to MySQL from the command line: msql -u *username* -p Connect to the database (schema): USE *database name* Check to see what database you are currently set to: SELECT DATABASE(); NOTES on SCFC Database CREATE TABLE users ( ID INT PRIMARY KEY, FirstName varchar(30) NOT NULL, LastName varchar(30) NOT NULL, Email varchar(50) NOT NULL, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"http:\/\/www.halcyonrepose.com\/index.php?rest_route=\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.halcyonrepose.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.halcyonrepose.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.halcyonrepose.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.halcyonrepose.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15"}],"version-history":[{"count":0,"href":"http:\/\/www.halcyonrepose.com\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.halcyonrepose.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.halcyonrepose.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.halcyonrepose.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}